×
Linux List Users Commands and Syntax

One of the most important parts of any Linux system admin is to create and manage users and groups. There are several ways to List Users in Linux based operating systems.

In this tutorial, we will show you How to List Linux Users with several examples.

List All Users with the /etc/passwd file

The /etc/passwd file contains information of all users including, system users and general users. This file is available in the most Linux-based operating systems.

Run the following command to list all user:

cat /etc/passwd

You should see the following output:


root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
syslog:x:101:104::/home/syslog:/bin/false
messagebus:x:102:106::/var/run/dbus:/bin/false
rtkit:x:107:114:RealtimeKit,,,:/proc:/bin/false
saned:x:108:115::/home/saned:/bin/false
avahi:x:111:117:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
lightdm:x:112:118:Light Display Manager:/var/lib/lightdm:/bin/false
colord:x:113:121:colord colour management daemon,,,:/var/lib/colord:/bin/false
hplip:x:114:7:HPLIP system user,,,:/var/run/hplip:/bin/false
pulse:x:115:122:PulseAudio daemon,,,:/var/run/pulse:/bin/false
vyom:x:1000:1000:Vyom,,,:/home/vyom:/bin/bash
postgres:x:116:126:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash
mysql:x:117:127:MySQL Server,,,:/var/lib/mysql:/bin/false
sshd:x:118:65534::/var/run/sshd:/usr/sbin/nologin
snmp:x:119:129::/var/lib/snmp:/bin/false

You can also see the sample output in the following screen:

In the above output, you should see that each line has seven fields. Explanation of each field is shown below:

  • Name of the User
  • Encrypted password of a user
  • User ID number (UID)
  • User’s group ID number (GID)
  • Full name of the user
  • User’s home directory
  • Name of the user login shell

You can also list users from /etc/passwd file using the following commands:

more /etc/passwd
less /etc/passwd

You can list only username without any other information by running the following command:

cut -d : -f 1 /etc/passwd

You should see the following output:


root
daemon
bin
sys
sync
games
mail
news
uucp
proxy
www-data
backup
nobody
syslog
messagebus
rtkit
saned
avahi
lightdm
colord
hplip
pulse
vyom
postgres
mysql
sshd
snmp

List All users with Awk command

Awk is a command-line tool that enables you to build commands that define text patterns. With Awk command, you can list all user with other information like, login names, full name and home directory along with the login.

awk -F":" '{print "Login:" $1 "\tName:" $5 "\tHome:" $6}' /etc/passwd

You should see all user in well formatted output:

Login:root Name:root Home:/root
Login:daemon Name:daemon Home:/usr/sbin
Login:bin Name:bin Home:/bin
Login:sys Name:sys Home:/dev
Login:sync Name:sync Home:/bin
Login:games Name:games Home:/usr/games
Login:mail Name:mail Home:/var/mail
Login:news Name:news Home:/var/spool/news
Login:uucp Name:uucp Home:/var/spool/uucp
Login:proxy Name:proxy Home:/bin
Login:www-data Name:www-data Home:/var/www
Login:backup Name:backup Home:/var/backups
Login:nobody Name:nobody Home:/nonexistent
Login:syslog Name: Home:/home/syslog
Login:messagebus Name: Home:/var/run/dbus
Login:rtkit Name:RealtimeKit,,, Home:/proc
Login:saned Name: Home:/home/saned
Login:avahi Name:Avahi mDNS daemon,,, Home:/var/run/avahi-daemon
Login:lightdm Name:Light Display Manager Home:/var/lib/lightdm
Login:colord Name:colord colour management daemon,,, Home:/var/lib/colord
Login:hplip Name:HPLIP system user,,, Home:/var/run/hplip
Login:pulse Name:PulseAudio daemon,,, Home:/var/run/pulse
Login:vyom Name:Vyom,,, Home:/home/vyom
Login:postgres Name:PostgreSQL administrator,,, Home:/var/lib/postgresql
Login:mysql Name:MySQL Server,,, Home:/var/lib/mysql
Login:sshd Name: Home:/var/run/sshd
Login:snmp Name: Home:/var/lib/snmp

You can also see the sample output in the following screen:

List All Users with Getent Command

The getent command do the same job like cut command. It will fetch user information from the databases then shows only the entries that match the option will be displayed.

To list all users, run the following command:

getent passwd

You should see the following output:


root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
syslog:x:101:104::/home/syslog:/bin/false
messagebus:x:102:106::/var/run/dbus:/bin/false
rtkit:x:107:114:RealtimeKit,,,:/proc:/bin/false
saned:x:108:115::/home/saned:/bin/false
avahi:x:111:117:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
lightdm:x:112:118:Light Display Manager:/var/lib/lightdm:/bin/false
colord:x:113:121:colord colour management daemon,,,:/var/lib/colord:/bin/false
hplip:x:114:7:HPLIP system user,,,:/var/run/hplip:/bin/false
pulse:x:115:122:PulseAudio daemon,,,:/var/run/pulse:/bin/false
vyom:x:1000:1000:Vyom,,,:/home/vyom:/bin/bash
postgres:x:116:126:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash
mysql:x:117:127:MySQL Server,,,:/var/lib/mysql:/bin/false
sshd:x:118:65534::/var/run/sshd:/usr/sbin/nologin
snmp:x:119:129::/var/lib/snmp:/bin/false

You can also list only username without any additional information with the following command:

getent passwd | cut -d: -f1

You should see the following output:


root
daemon
bin
sys
sync
games
mail
news
uucp
proxy
www-data
backup
nobody
syslog
messagebus
rtkit
saned
avahi
lightdm
colord
hplip
pulse
vyom
postgres
mysql
sshd
snmp

You can see the sample output in the following screen:

List System and General Users

There are two types of users in Linux-based operating system, General user and System user. Each user has a unique UID defined in /etc/passwd file. All general users have UID >= 1000 and only those are allowed to login into the system if a shell is defined in /etc/shells file.
While all system users have UID <= 999.

You can separate System users and General users by creating the following shell script:

nano test.sh

Add the following code:


#!/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 "----------[ 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"

Save and close the file when you are finished.

Next, run this script with the following command:

bash test.sh

You should see the all users in the following output:


----------[ Normal User Accounts ]---------------
vyom:x:1000:1000:Vyom,,,:/home/vyom:/bin/bash
----------[ System User Accounts ]---------------
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
syslog:x:101:104::/home/syslog:/bin/false
messagebus:x:102:106::/var/run/dbus:/bin/false
rtkit:x:107:114:RealtimeKit,,,:/proc:/bin/false
saned:x:108:115::/home/saned:/bin/false
avahi:x:111:117:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
lightdm:x:112:118:Light Display Manager:/var/lib/lightdm:/bin/false
colord:x:113:121:colord colour management daemon,,,:/var/lib/colord:/bin/false
hplip:x:114:7:HPLIP system user,,,:/var/run/hplip:/bin/false
pulse:x:115:122:PulseAudio daemon,,,:/var/run/pulse:/bin/false
postgres:x:116:126:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash
mysql:x:117:127:MySQL Server,,,:/var/lib/mysql:/bin/false
sshd:x:118:65534::/var/run/sshd:/usr/sbin/nologin
snmp:x:119:129::/var/lib/snmp:/bin/false

You can also see the sample output in the following screen:

Conclusion

In the above tutorial, we learned how to list all users with different method in Linux-based operating system.