How to list users in Linux
Managing users is an essential aspect of Linux system administration. Whether you’re running a personal Linux desktop or maintaining a large server, it’s crucial to understand how to list users effectively. This blog post will explore various ways to list users in Linux, along with practical examples.
There are different ways of listing users in linux
‘/etc/passwd’ file
The ‘/etc/passwd’ file is a critical component of user management in Linux. It stores information about user accounts, including usernames, user IDs (UID), group IDs (GID), home directories, and more. By using the ‘cat’ command, we can easily access this treasure of user data.
List users using the ‘cat’ command
List all Users
Below command will display the entire ‘/etc/passwd’ file, presenting information about all users on our system
cat /etc/passwd
root@cloudscope:~# 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:/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-network:x:100:102:systemd Network Management,,,:/run/systemd:/usr/sbin/nologin
systemd-resolve:x:101:103:systemd Resolver,,,:/run/systemd:/usr/sbin/nologin
messagebus:x:102:105::/nonexistent:/usr/sbin/nologin
systemd-timesync:x:103:106:systemd Time Synchronization,,,:/run/systemd:/usr/sbin/nologin
syslog:x:104:111::/home/syslog:/usr/sbin/nologin
_apt:x:105:65534::/nonexistent:/usr/sbin/nologin
tss:x:106:112:TPM software stack,,,:/var/lib/tpm:/bin/false
uuidd:x:107:113::/run/uuidd:/usr/sbin/nologin
tcpdump:x:108:114::/nonexistent:/usr/sbin/nologin
sshd:x:109:65534::/run/sshd:/usr/sbin/nologin
pollinate:x:110:1::/var/cache/pollinate:/bin/false
landscape:x:111:116::/var/lib/landscape:/usr/sbin/nologin
fwupd-refresh:x:112:117:fwupd-refresh user,,,:/run/systemd:/usr/sbin/nologin
ec2-instance-connect:x:113:65534::/nonexistent:/usr/sbin/nologin
_chrony:x:114:121:Chrony daemon,,,:/var/lib/chrony:/usr/sbin/nologin
ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash
lxd:x:999:100::/var/snap/lxd/common/lxd:/bin/false
To extract specific user details or perform more refined searches, various options and additional commands can be combined with ‘cat’
List Usernames
If you only need a list of usernames, the ‘cut’ command can be combined with ‘cat’:
cat /etc/passwd | cut -d: -f1
root@cloudscope:~# cat /etc/passwd | cut -d: -f1
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy
www-data
backup
list
irc
gnats
nobody
systemd-network
systemd-resolve
messagebus
systemd-timesync
syslog
_apt
tss
uuidd
tcpdump
sshd
pollinate
landscape
fwupd-refresh
ec2-instance-connect
_chrony
ubuntu
lxd
List Home Directories
For those interested in the home directories of each user, you can utilize ‘awk’ in combination with the ‘cat’ command:
cat /etc/passwd | awk -F: '{print $1, $6}'
root@cloudscope:~# cat /etc/passwd | awk -F: '{print $1, $6}'
root /root
daemon /usr/sbin
bin /bin
sys /dev
sync /bin
games /usr/games
man /var/cache/man
lp /var/spool/lpd
mail /var/mail
news /var/spool/news
uucp /var/spool/uucp
proxy /bin
www-data /var/www
backup /var/backups
list /var/list
irc /run/ircd
gnats /var/lib/gnats
nobody /nonexistent
systemd-network /run/systemd
systemd-resolve /run/systemd
messagebus /nonexistent
systemd-timesync /run/systemd
syslog /home/syslog
_apt /nonexistent
tss /var/lib/tpm
uuidd /run/uuidd
tcpdump /nonexistent
sshd /run/sshd
pollinate /var/cache/pollinate
landscape /var/lib/landscape
fwupd-refresh /run/systemd
ec2-instance-connect /nonexistent
_chrony /var/lib/chrony
ubuntu /home/ubuntu
lxd /var/snap/lxd/common/lxd
List a Specific user
To focus on a particular user, replace ‘username’ with your desired username
cat /etc/passwd | grep username
root@cloudscope:~# cat /etc/passwd | grep ubuntu
ubuntu
:x:1000:1000:Ubuntu:/home/ubuntu
:/bin/bash
Export User List to a File
Saving the user list to a file for future reference or processing is simple using the ‘cat’ and ‘cut’ commands:
cat /etc/passwd | cut -d: -f1 > user_list.txt
This will create a ‘user_list.txt’ file containing the list of usernames.
List users using the ‘getent’ command
List All Users
To list all users on your Linux system, use the ‘getent’ command with the ‘passwd’ database
getent passwd
List Usernames
If you only require a simple list of usernames, use the ‘cut’ command in combination with ‘getent’
getent passwd | cut -d: -f1
This will produce a clean and concise list of usernames.
List Home Directories
To see the home directories of each user, utilize ‘awk’ in conjunction with ‘getent’:
getent passwd | awk -F: '{print $1, $6}'
This command will display a list of usernames along with their respective home directories.
List Specific Users
To focus on a particular user, replace ‘username’ with the desired username
getent passwd username
This command will display detailed information about the specified user
Export User List to a File
You can save the user list to a file for future reference or processing using ‘getent’ and ‘cut’ commands
getent passwd | cut -d: -f1 > user_list.txt
This creates a ‘user_list.txt’ file containing the list of usernames.
Conclusion
In this tutorial we saw different ways of listing users, groups and filtering them as per the requirement.