In this article, we will explore the different commands and tools you can use to list and view users on a Linux system.
Using the cut
command with the etc/passwd
file
The /etc/passwd
file in Linux is a crucial system file that stores basic user account information, essential for the login process. It’s a plain text file located in the /etc
directory, which houses various system configuration files.
cd /etc/passwd
To extract specific information from the /etc/passwd
file, you can use the cut
command. For example, to display only usernames and their corresponding home directories, you can run:
cut -d: -f1,6 /etc/passwd
This command uses the colon (:
) as a delimiter and selects the first and sixth fields (username and home directory) from the /etc/passwd
file. Here is the sample output of the above command;
root:/root
John:/home/John
Jane:/home/Jane
The output shows the username followed by their respective home directories.
Using the combination of cat
and cut
commands
If you want to list only usernames without other user information and directories, you can use a combination of the cat
and cut
Linux commands as seen below;
cat /etc/passwd | cut -d: -f1
The above method uses the cat command to print the contents of the /etc/passwd
file and the cut
command is used to extract the first field (usernames) from each line in the /etc/passwd
file, which stores user information. Here is the sample output of the above commands;
root
John
Jane
List Users with awk
Command
Utilize the awk
command to display only the usernames from the /etc/passwd file, extracting the first field on each line by specifying the colon as the field separator.
awk -F':' '{ print $1}' /etc/passwd
The output of the above command will be as follows;
root@sample-server:~# awk -F':' '{ print $1}' /etc/passwd
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy
Listing Users with the getent
command
The getent
command retrieves entries from databases, including user information. To list all users, you can use getent
with the passwd database:
getent passwd
This command provides the same information as the /etc/passwd
file but queries the system’s user database dynamically.
The sample output will be as follows;
root@sample-server:~# 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
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
Using the cut
command with /etc/group
Similar to the /etc/passwd
file, the /etc/group
file contains information about user groups. To display a list of groups and their members, you can use the cut
command:
cut -d: -f1,4 /etc/group
This command selects the group name (field 1) and the list of users in the group (field 4) using the colon (:
) as a delimiter. See the sample output below;
root:
daemon:
bin:
sys:
adm:syslog,bibtv-panel,yellowtent
tty:
disk:
lp:
Viewing logged-in users with who
and w
commands
To see a list of users currently logged into the system along with additional details such as terminal, login time, and IP address, use the who
command:
who
For a more detailed view, you can use the w
command:
w
The w
command in Ubuntu provides information about currently logged-in users and their activities. The output typically looks like this:
root@sample-server:w
23:45:12 up 1 day, 3:30, 1 user, load average: 0.08, 0.15, 0.17
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
ugacomp tty1 :0 22:14 1:34m 0.05s 0.05s -bash
Conclusion
Linux provides a variety of tools and commands to list and view users, catering to different needs and preferences. Whether you prefer parsing system files or using dedicated commands, mastering these techniques will empower you to efficiently manage user accounts on your Linux system. Choose the method that best suits your requirements and workflow, and enhance your Linux user management skills.