How To List Users in Linux

0

ADVERTISEMENT

The very first task that any Linux administrator performs when a user says unable to login to the system. Yes, to know how to list Users in Linux is must to troubleshoot any login issue.

I hope you have seen commands to create a user, delete a user, modifying a user, list logged in users. Here, we will see how to list users in Linux.

List Users in Linux Using /etc/passwd file

If you remember, when you create a user the useradd command puts an entry in /etc/passwd file about the user. With the help of /etc/passwd file, we can list the local users present in the system.

User cat or less or more command to list users.

cat /etc/passwd

Output:

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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
webmaster:x:1999:1001::/opt/webadmin:/bin/bash
user_name:x:2000:2000:sudo user:/home/user_name:/bin/bash

Each line in /etc/passwd file represents a single user.

The /etc/passwd file has seven fields delimited by a colon.

  1. User Name
  2. Encrypted Password (x represent that the password is stored in the /etc/shadow file)
  3. User’s UID (User Identification Number)
  4. User’s GID (Group Identification Number)
  5. GECOS field (User Full Name or Comments)
  6. User’s Home Directory
  7. Login Shell
A regular user has UID greater or equal to 1000 (Newer OS) or 500 (Older OS). Users with UID <1000 (or <500) are system users.

If you want to display only the list of users name in the system, you can simply filter the output.

cat /etc/passwd | awk -F: '{ print $1}'

Output:

root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
postfix
chrony
sshd
webmaster
user_name

List Users in Linux Using getent

The getent command will list users by queries the databases configured in /etc/nssswitch.conf. The database includes both /etc/passwd (local users) and LDAP. So, getent will display users from both /etc/passwd and LDAP.

To get a list of Linux users, run the following command.

getent passwd

Output:

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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
webmaster:x:1999:1001::/opt/webadmin:/bin/bash
user_name:x:2000:2000:sudo user:/home/user_name:/bin/bash
The gatent command’s output will be similar to the content of /etc/passwd file. You would additionally see users from LDAP if the system is configured with LDAP.

Use awk just to list the usernames.

getent passwd | awk -F: '{ print $1}'

Output:

root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
postfix
chrony
sshd
webmaster
user_name

Conclusion

In this post, you have learned to list users in your Linux system. Listing users in Linux is very simple and all you have to watch out command’s output for the information you need for troubleshooting user login issues.

ADVERTISEMENT

You might also like