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 in the Linux system.
Today, we will learn different ways to list users in Linux, with or without a home directory.
Table of Contents
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 ignoring 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
include 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 command, you might be thinking that they were listing all users created manually by you or by services.
It is true, and many times, users are unable to figure out whether users were manually created by them or by the services.
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 in the market to do the same job, which I have not listed. After all, Linux without using a terminal is insolent work, according to me.
Let me know your thoughts and opinions in the comment section.
Also Read: Add Regular User to Sudo Group using Usermod Command
Also Read: Execute Specific Command Without Sudo Password In Linux
Listing All Users in a Linux System