Table of Contents
You’re a new Linux system administrator and you’re unable to find the command to list all users on your Linux server. What is the command to list users under Linux operating systems? How do I list users in Linux?
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 post describes various Linux commands for Linux to list all users and options on the Linux operating system, including Ubuntu, Debian, RHEL, Arch, Fedora, CentOS, and other distros.
Linux list all users account using the /etc/passwd file
cat /etc/passwd
[root@vps01 ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:20:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
...
Each line in the file has seven fields as follows. For example, consider the following line:
daemon:x:2:20:daemon:/sbin:/sbin/nologin
vnstat:x:131:137:vnstat daemon,,,:/var/lib/vnstat:/usr/sbin/nologin
- : The username or login name.
- : Encrypted password is stored in the /etc/shadow file.
- : UID (user ID number)
- : Primary GID (group ID number)
- : Home directory for the user.
- : Login shell for the user. Pathnames of valid login shells comes from the /etc/shells file.
Of course we can use pagers such as more/less commands as follows to view the /etc/passwd file:
more /etc/passwd
less /etc/passwd
[root@vps01 ~]# more /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
...
Linux list usernames only, to list only usernames, type the following awk command:
awk -F':' '{ print $1}' /etc/passwd
[root@vps01 ~]# awk -F':' '{ print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
...
Get a list of all users using the getent command
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
How to count user accounts in the Linux server
Want to get user accounts count on your system? Try the wc command as follows:
[root@vps01 ~]# compgen -u | wc -l
34
[root@vps01 ~]# getent passwd | wc -l
34
[root@vps01 ~]#
System and General Users
#!/bin/bash
_l="/etc/login.defs"
_p="/etc/passwd"
## get mini UID limit ##
l=$(grep "^UID_MIN" $_l)
## get max UID limit ##
l1=$(grep "^UID_MAX" $_l)
## 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 }' "$_p"
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 }' "$_p"
[root@vps01 ~]# chmod +x list.sh
[root@vps01 ~]# ./list.sh
----------[ Normal User Accounts ]---------------
cyberpanel:x:1000:1000::/home/cyberpanel:/bin/false
docker:x:1001:1001::/home/docker:/bin/bash
ftpuser:x:2001:2001:pureftpd user:/bin/null:/bin/false
vmail:x:5000:5000::/home/vmail:/bin/bash
lscpd:x:5001:5001::/usr/local/lscp:/bin/bash
store6519:x:5002:5002::/home/store.soft365.vn:/bin/bash
----------[ System User Accounts ]---------------
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
...
opendkim:x:993:991:OpenDKIM Milter:/var/run/opendkim:/sbin/nologin
saslauth:x:992:76:Saslauthd user:/run/saslauthd:/sbin/nologin
redis:x:991:990:Redis Database Server:/var/lib/redis:/sbin/nologin