Linux List All Users in The System - Nixcraft
Linux List All Users in The System - Nixcraft
The /etc/passwd file contains one line for each Linux user
account, with seven fields delimited by colons. This is a text file. You can easily list
users under Linux using the cat command or other commands such as grep
command/egrep command and more. This page describes various Linux
commands to list all users on the Linux operating system, including Ubuntu,
Debian, RHEL, Arch, Fedora, CentOS, and other distros.
Tutorial requirements
Table of contents
» Get a list of all users using /etc/passwd
» List users in Linux using pagers
» List user names only
» getent command
» Find out whether a user account exists
» Count user accounts
» Find system and general Linux users
» Conclusion
FEATURED ARTICLES
$ cat /etc/passwd
6 40 Linux Server Hardening Security Tips
Sample outputs:
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
9 Top 25 Nginx Web Server Best Security
Practices
....
..
... 10 My 10 UNIX Command Line Mistakes
vnstat:x:131:137:vnstat daemon,,,:/var/lib/vnstat:/usr/sbin/nologin
Each line in the file has seven fields as follows. For example, consider the
following line:
vnstat:x:131:137:vnstat daemon,,,:/var/lib/vnstat:/usr/sbin/nologin
Where,
vnstat daemon – GECOS. It may includes user’s full name ﴾or application
name, if the account is for a program﴿, building and room number or contact
person, office telephone number, home telephone number and any other
contact information.
/usr/sbin/nologin – Login shell for the user. Pathnames of valid login shells
comes from the /etc/shells file.
$ more /etc/passwd
$ less /etc/passwd
Sample outputs:
All fields are separated by a colon ﴾:﴿ symbol. Total seven fields exists. The first
field is username. It is used when user logs in. It should be between 1 and 32
characters in length. Of course we can limit outputs using the head command
and tail command as follows:
tail ‐5 /etc/passwd
head ‐5 /etc/passwd
Sample outputs:
root
daemon
bin
sys
sync
games
man
lp
mail
news
....
..
..hplip
vivek
bind
haldaemon
sshd
mysql
radvd
$ getent passwd
$ getent passwd | grep tom
## get a list all users ##
$ getent passwd | cut ‐d: ‐f1
## count all user accounts using the wc ##
$ getent passwd | wc ‐l
One can use the compgen command on Linux to list users and other resources
too:
$ compgen ‐u
$ compgen ‐u | wc ‐l
$ getent passwd | wc ‐l
A Note About System and General Users
Each user has numerical user ID called UID. It is defined in /etc/passwd file. The
UID for each user is automatically selected using /etc/login.defs file when you use
useradd command. To see current value, enter:
Sample outputs:
UID_MIN 1000
#SYS_UID_MIN 100
To see maximum values for automatic uid selection in the useradd command,
enter:
Sample outputs:
UID_MAX 60000
In other words, all normal system users must have UID >= 1000 ﴾MIN﴿ and UID
/etc/shells file. Here is an updated code to get details:
## use awk to print if UID >= $MIN and UID <= $MAX ##
awk ‐F':' ‐v "min=${l##UID_MIN}" ‐v "max=${l1##UID_MAX}" '{ if ( $3 >= min && $3 <= max ) print $0}'
Sample outputs:
vivek:x:500:500::/home/vivek:/bin/bash
raj:x:501:501::/home/raj:/bin/ksh
ash:x:502:502::/home/ash:/bin/zsh
jadmin:x:503:503::/home/jadmin:/bin/sh
jwww:x:504:504::/htdocs/html:/sbin/nologin
wwwcorp:x:505:505::/htdocs/corp:/sbin/nologin
wwwint:x:506:506::/htdocs/intranet:/bin/bash
scpftp:x:507:507::/htdocs/ftpjail:/bin/bash
rsynftp:x:508:508::/htdocs/projets:/bin/bash
mirror:x:509:509::/htdocs:/bin/bash
jony:x:510:510::/home/jony:/bin/ksh
amyk:x:511:511::/home/amyk:/bin/ksh
#!/bin/bash
# Name: listusers.bash
# Purpose: List all normal user accounts in the system. Tested on RHEL / Debian Linux to List All Users on Linux
# Author: Vivek Gite <www.cyberciti.biz>, under GPL v2.0+
# ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
_l="/etc/login.defs"
_p="/etc/passwd"
## get mini UID limit ##
l=$(grep "^UID_MIN" $_l)
## use awk to print if UID >= $MIN and UID <= $MAX and shell is not /sbin/nologin ##
awk ‐F':' ‐v "min=${l##UID_MIN}" ‐v "max=${l1##UID_MAX}" '{ if ( $3 >= min && $3 <= max && $7 != "/sbin/nologin" ) "$_p"
Sample outputs:
vivek:x:500:500::/home/vivek:/bin/bash
raj:x:501:501::/home/raj:/bin/ksh
ash:x:502:502::/home/ash:/bin/zsh
jadmin:x:503:503::/home/jadmin:/bin/sh
wwwint:x:506:506::/htdocs/intranet:/bin/bash
scpftp:x:507:507::/htdocs/ftpjail:/bin/bash
rsynftp:x:508:508::/htdocs/projets:/bin/bash
mirror:x:509:509::/htdocs:/bin/bash
jony:x:510:510::/home/jony:/bin/ksh
amyk:x:511:511::/home/amyk:/bin/ksh
#!/bin/bash
# Name: listusers.bash
# Purpose: List all normal user and system accounts in the system. Tested on RHEL / Debian Linux
# Author: Vivek Gite <www.cyberciti.biz>, under GPL v2.0+
# ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
_l="/etc/login.defs"
_p="/etc/passwd"
## use awk to print if UID >= $MIN and UID <= $MAX and shell is not /sbin/nologin ##
echo "‐‐‐‐‐‐‐‐‐‐[ Normal User Accounts ]‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐"
awk ‐F':' ‐v "min=${l##UID_MIN}" ‐v "max=${l1##UID_MAX}" '{ if ( $3 >= min && $3 <= max && $7 != "/sbin/nologin" ) print $0 }'
echo ""
echo "‐‐‐‐‐‐‐‐‐‐[ System User Accounts ]‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐"
awk ‐F':' ‐v "min=${l##UID_MIN}" ‐v "max=${l1##UID_MAX}" '{ if ( !($3 >= min && $3 <= max && $7 != "/sbin/nologin")) print $0 }'
Sample outputs:
Ad
Conclusion
You learned how to get a list of users in Linux machine. We can use the getent,
cat, more, cut and other commands to fetch list of user accounts on a Linux
system. See how to check list of users in Unix and man pages as follows:
man 5 passwd
man getent
man cut
man awk
help compgen
Related Tutorials
File
cat
Management
Package
apk • apt
Manager
Category List of Unix and Linux commands
I would change it to `cat /etc/passwd | grep ‐v nologin` which gives a clearer view
into which users can actually login and execute.
reply link
Thanks for the feedback! The faq has been updated with more info.
reply link
reply link
“Instead of �cat $file | grep $pattern you should use grep $pattern $file ”
I’m one more mistaken user :﴿
reply link
last
lastb
lastlog
Claudio
reply link
reply link
reply link
The ldap and nis scenarios for centralized login administration are omitted here.
The local passwd file may be just the tip of the login iceberg if either of those is
set in
/etc/nsswitch.conf
For example:
# ypcat passwd
would be the common command if nis is set up.
reply link
#!/bin/bash
# Name: listusers.bash
# Purpose: List all normal user and system accounts in the system. Tested on RHEL / Debian Linux
# Author: Vivek Gite , under GPL v2.0+
# https://www.cyberciti.biz/faq/linux‐list‐users‐command/
# ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
_l="/etc/login.defs"
_p="/etc/passwd"
## use awk to print if UID >= $MIN and UID = min && $3 = min && $3 <= max && $7 != "/sbin/nologin" ) printf "%‐15s %‐5s %‐5s %‐25s %‐10sn",
echo ""
reply link
#!/bin/bash
# Name: listgroups.bash
# Purpose: List all normal user and system groups in the system. Tested on RHEL / Debian Linux
# Author: Vivek Gite , under GPL v2.0+
# https://www.cyberciti.biz/faq/linux‐list‐users‐command/
# ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
_l="/etc/login.defs"
_g="/etc/group"
## use awk to print if GID >= $MIN and GID = min && $3 = min && $3 <= max ) printf "%‐15s %‐5s %‐10sn", $1, $3, $4 }' "$_g"
reply link
hi there,
great script, really helps me a lot, and it’s also very well documented! great work!
however, since i’m a newbie when it comes to shell scripting, how do I direct the
output from stdout into a file?
I’ve found something like this:
2>&1 | tee ‐a users.txt
thanks!
reply link
Redirect the output to a file with the first command and then append the
second command output:
reply link
Find the number of users on your system whose user ids are greater than 8?
reply link
reply link
reply link
reply link
reply link
Note that none of this accounts for systems using an external source for its users.
If you are using ldap or ﴾gasp!﴿ Active Directory to source your users, then listing
/etc/passwd will not yield the desired results, as you won’t see the bulk of your
users.
The command we use, insted of “cat /etc/passwd”, is “getent passwd”, which
returns the combined list of users from /etc/passwd ﴾local users﴿ and other
sources. The getent command will give you a more realistic view of your users,
on any system you encounter.
reply link
Could you show how I would use your command in the final script as posted
by the original poster? I think that is the issue I’m having where it is only
showing the users with Local Authentication, not AD Users ﴾yes ack.. AD﴿.
Thanks,
Bob
reply link
reply link
I configured the VNC as per given step. How to access the same GUI from host
machine as we accessing from VNC client.
reply link
2﴿ with “nologin”
OR ﴾if you like a separate, clean UIDs listing of one, two or three digits﴿:
reply link
I actually improved this a little bit; here are some useful aliases which you can put
in the ~/.bashrc file:
1682056885ff9f4c75d08c_000006
reply link
Dynamic user are created at the service start and destroy at the stop. They exist
only in systemd process and are enable by the “systemd” service in nsswitch.conf.
reply link
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment
Name *
Email *
Website
Post Comment
Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ [email protected]
©2021 nixCraft ⢠Privacy ⢠ToS ⢠Contact/Email ⢠Search ⢠Sponsored by Linode