Linux administrators have to be aware of existing users in the Linux system for different reasons, like finding out their roles or privileges.
This all requires knowledge of the commands, which help us list all the current users on the Linux system.
Today, we will learn different ways to list users in Linux, with or without a home directory.
Method 1: Reading the Passwd File
The generic way to list users is by reading the content of the passwd file. For example, the cat command can be used along with a passwd file path, as shown below.
- cat /etc/passwd
Below is the behavior of the above command.
The cat
command also lists all other information related to the users, which might be unnecessary to you.
For that, use the awk
command to list the username from the passwd file, as shown below.
- awk -F ":" '{print $1}' /etc/passwd
Below is the behavior of the above command.
Method 2: Using the Compgen Command
Just like the awk
command, which ignores all the other details except the username, you can use the compgen
command to do the same job without writing a long line of code, as shown below.
- compgen -u
Below is the behavior of the above command.
Method 3: Using the Getent Command
Just like the cat
command, getent
includes all the other details. You can use this command to do the same job without specifying the passwd path, as shown below.
- getent passwd
Below is the behavior of the above command.
Method 4: Filtering Users Based on Home Directory
From the above-mentioned commands, you might be thinking that they were listing all users created manually by you or by services.
It is true that users are frequently unable to distinguish between users that they manually created and those that the services created.
To solve this problem, we can list users with the home directory located at the /home/
path using the awk
command, as shown below.
- awk -F ":" '{if($6 = /home/) print $1}' /etc/passwd
Below is the behavior of the above command.
The above command only lists the users with a home directory created using the adduser
command.
As you can see, I have created two users, "ubuntushell" and "test", manually with their home directory using the adduser
command.
Final Thought
A GUI application is available on the market to do the same job, which I have not listed. After all, Linux without a terminal is insolent work, according to me.