Linux Tutorial

How to check List users in Linux | Complete Guide for Beginners

how-to-list-users-linux

There are commands to create user in Linux, delete user, add a user to group, and see logged in user but Is there any command to list users in Linux?

No, there is no command to list users in Linux.

Now, what will you do if you want to see available users in your Linux system/server?

This article is all about to list users in Linux using some other commands and tricks.

Get a list users in Linux from /etc/passwd file

User information is saved in /etc/passwd file. this file contains one line for each user account. you can easily get list users from /etc/password file using acombination of cat command and grep command.

$cat /etc/passwd

[email protected]:~$cat /etc/passwd
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
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/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
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
systemd-timesync:x:100:102:systemd Time Synchronization,,,:/run/systemd:/usr/sbin/nologin
systemd-network:x:101:103:systemd Network Management,,,:/run/systemd:/usr/sbin/nologin
systemd-resolve:x:102:104:systemd Resolver,,,:/run/systemd:/usr/sbin/nologin
messagebus:x:103:106::/nonexistent:/usr/sbin/nologin
syslog:x:104:109::/home/syslog:/usr/sbin/nologin
_apt:x:105:65534::/nonexistent:/usr/sbin/nologin
uuidd:x:106:113::/run/uuidd:/usr/sbin/nologin
avahi-autoipd:x:107:115:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/usr/sbin/nologin
usbmux:x:108:46:usbmux daemon,,,:/var/lib/usbmux:/usr/sbin/nologin
rtkit:x:109:116:RealtimeKit,,,:/proc:/usr/sbin/nologin
dnsmasq:x:110:65534:dnsmasq,,,:/var/lib/misc:/usr/sbin/nologin
cups-pk-helper:x:111:118:user for cups-pk-helper service,,,:/home/cups-pk-helper:/usr/sbin/nologin
speech-dispatcher:x:112:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/false
kernoops:x:113:65534:Kernel Oops Tracking Daemon,,,:/:/usr/sbin/nologin
avahi:x:114:120:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/usr/sbin/nologin
saned:x:115:121::/var/lib/saned:/usr/sbin/nologin
nm-openvpn:x:116:122:NetworkManager OpenVPN,,,:/var/lib/openvpn/chroot:/usr/sbin/nologin
whoopsie:x:117:123::/nonexistent:/bin/false
colord:x:118:124:colord colour management daemon,,,:/var/lib/colord:/usr/sbin/nologin
hplip:x:119:7:HPLIP system user,,,:/var/run/hplip:/bin/false
geoclue:x:120:125::/var/lib/geoclue:/usr/sbin/nologin
pulse:x:121:126:PulseAudio daemon,,,:/var/run/pulse:/usr/sbin/nologin
gnome-initial-setup:x:122:65534::/run/gnome-initial-setup/:/bin/false
gdm:x:123:128:Gnome Display Manager:/var/lib/gdm3:/bin/false
vijay:x:1000:1000:Vijay,,,:/home/vijay:/bin/bash
systemd-coredump:x:999:999:systemd Core Dumper:/:/sbin/nologin
cyrage:x:1001:1001:Cyrage,,,:/home/cyrage:/bin/bash
ram:x:1002:1002::/home/ram:/bin/sh
ansh:x:1003:1003::/home/ansh:/bin/sh
nonu:x:1004:1004::/data/project/:/bin/sh
surya:x:1200:1004::/home/surya:/bin/sh
anshika:x:1201:1201::/home/anshika:/bin/sh
hema:x:1202:1202::/home/hema:/bin/sh
amit:x:1203:1203:Amitabh Bachhan:/home/amit:/bin/sh
nitin:x:1204:100::/home/nitin:/bin/sh
[email protected]:~$

This command will show all fields and content of this file, The first field of the line consist username. other fields contain the following information

  • User name
  • Encrypted password (x means that the password is stored in the /etc/shadow file)
  • User ID number (UID)
  • User’s group ID number (GID)
  • Full name of the user (GECOS)
  • User home directory
  • Login shell (defaults to /bin/bash)

If you want to display only first field (usrname) form passwd file then use awk or cut command.

$awk -F: ‘{ print $1}’ /etc/passwd
$cut -d: -f1 /etc/passwd

[email protected]:~$wk -F: '{ print $1}' /etc/passwd
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy
www-data
backup
list
irc
gnats
nobody
systemd-timesync
systemd-network
systemd-resolve
messagebus
syslog
_apt
uuidd
avahi-autoipd
usbmux
rtkit
dnsmasq
cups-pk-helper
speech-dispatcher
kernoops
avahi
saned
nm-openvpn
whoopsie
colord
hplip
geoclue
pulse
gnome-initial-setup
gdm
vijay
systemd-coredump
cyrage
ram
ansh
nonu
surya
anshika
hema
amit
nitin
[email protected]:~$

If you want save users list linux in file then use following command.

This method is useful when your boss Linux to send a list of users of linux system/server. I save the output into users.txt and userlist.txt

To see the data from the file use cat command.

$awk -F: ‘{ print $1}’ /etc/passwd >users.txt
$cut -d: -f1 /etc/passwd >userslist.txt
$cat users.txt

[email protected]:~$awk -F: '{ print $1}' /etc/passwd >users.txt
[email protected]:~$

File is created in current directory name users.txt, if you want to save another location give the full path instead of users.txt

[email protected]:~$ls
Desktop    Downloads         Music     Pictures  resume.doc  users.txt
Documents  examples.desktop  old_data  Public    Templates   Videos
[email protected]:~$

More formated awk command to retrieve the username and other information from /etc/passwd file

$ awk -F”:” ‘{print “Login:” $1 “\tName:” $5 “\tHome:” $6}’ /etc/passwd

[email protected]:~$awk -F":" '{print "Login:" $1 "\tName:" $5 "\tHome:" $6}' /etc/passwd
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:man	Name:man	Home:/var/cache/man
[email protected]:~$

Use getent command to see list users

Getent is a utility used to fetch data entries form databases supported by the Name Service Library. In Linux, this databasese is configured into /etc/nsswitct.conf file including passwd datase.
To get users list linux by type the follwoing syntax.

getent passwd

$getent passwd | awk -F: ‘{ print $1}’
$getent passwd | cut -d: -f1

To Check total users on your system in numbers use the following command

$getent passwd | wc -l

[email protected]:~$getent passwd | wc -l
53
[email protected]:~$

Total users on my systems are 53 only.

Check the existence of a user in the Linux system

If you have very long list of users, some time may be 1000 (It is possible, because big server has 1000s users). a will you know user exists in your system/server or not?

For example, Your hr madam asks “please check john user is exist on your system, having following problem to log in system”

You can use the following command to check whether a user exists in the system or not.

$getent passwd | grep john

[email protected]:~$getent passwd | grep vijay
vijay:x:1000:1000:Vijay,,,:/home/vijay:/bin/bash
[email protected]:~$

If the user exists the command above will print the user’s login information. If there is no output that means the user doesn’t exist means the user doesn’t exist.

Vijay user is exist on this system so it gvie the result. And john user doesn’t exist on this system so no result. See example below:

[email protected]:~$getent passwd | grep john
[email protected]:~$

Normal users and System Users

There is no technical difference between normal user and system users. System user is being created default at the time of installation process and have nologin shell.

In other hand normal user is created by system administrator.

UID is the numeric value for each user, difined in file /etc/passwd. When you run useradd command to create user in linux, user select automatically UID for /etc/login.defs. lol

$ grep “^UID_MIN” /etc/login.defs
$ grep UID_MIN /etc/login.defs

[email protected]:~$grep "^UID_MIN" /etc/login.defs
UID_MIN			 1000
[email protected]:~$
[email protected]:~$grep UID_MIN /etc/login.defs
UID_MIN			 1000
#SYS_UID_MIN		  100
[email protected]:~$

To check the minimume and maximume user ID’s values on your system. You can use following command:

$grep -E ‘^UID_MIN|^UID_MAX’ /etc/login.defs

[email protected]:~$grep -E '^UID_MIN|^UID_MAX' /etc/login.defs
UID_MIN			 1000
UID_MAX			60000
[email protected]:~$

Above result, you can see that all normal users have UID between 1000 and 60000.

Following command will be used to list all normal users in your Linux system:

$getent passwd {1000..60000}

[email protected]:~$getent passwd {1000..60000}
vijay:x:1000:1000:Vijay,,,:/home/vijay:/bin/bash
cyrage:x:1001:1001:Cyrage,,,:/home/cyrage:/bin/bash
ram:x:1002:1002::/home/ram:/bin/sh
ansh:x:1003:1003::/home/ansh:/bin/sh
nonu:x:1004:1004::/data/project/:/bin/sh
surya:x:1200:1004::/home/surya:/bin/sh
anshika:x:1201:1201::/home/anshika:/bin/sh
hema:x:1202:1202::/home/hema:/bin/sh
amit:x:1203:1203:Amitabh Bachhan:/home/amit:/bin/sh
nitin:x:1204:100::/home/nitin:/bin/sh
[email protected]:~$

In the above result, all users have been created by me in last tutorial How to create user in linux.

Some advanced commands and outputs

[email protected]:~$eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)}
vijay:x:1000:1000:Vijay,,,:/home/vijay:/bin/bash
cyrage:x:1001:1001:Cyrage,,,:/home/cyrage:/bin/bash
ram:x:1002:1002::/home/ram:/bin/sh
ansh:x:1003:1003::/home/ansh:/bin/sh
nonu:x:1004:1004::/data/project/:/bin/sh
surya:x:1200:1004::/home/surya:/bin/sh
anshika:x:1201:1201::/home/anshika:/bin/sh
hema:x:1202:1202::/home/hema:/bin/sh
amit:x:1203:1203:Amitabh Bachhan:/home/amit:/bin/sh
nitin:x:1204:100::/home/nitin:/bin/sh
[email protected]:~$
[email protected]:~$eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)} | cut -d: -f1
vijay
cyrage
ram
ansh
nonu
surya
anshika
hema
amit
nitin
[email protected]:~$

Conclusion

In this tutorial, you learned how to list users in the Linux system, filter them using different methods. and what are the main differences between system users and normal Linux users?

The same commands apply for any Linux distribution, including Ubuntu, CentOS, RHEL, Debian and Linux Mint.

If you have any question, feel free comment below. You can contact me through [email protected]

Cheers

Skip to toolbar