Listing All Users In Linux

By | 2020-09-21

Listing all users on Linux is best done with the getent passwd command. This isn’t the only way, but it should work an practically any system. Unlike other methods, it uses your systems /etc/nsswitch.conf configuration to show users from all sources. The format is the format used in the file /etc/passwd.

$ getent 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
# The rest is omitted for brevity

While it is possible to just read /etc/passwd directly, if you have centralized user management using something like LDAP, you will have to use the application specific tool to view those users. getent outputs users from all sources.