How to List Users in Ubuntu 22.04

In Linux, keeping track of the user’s related information is the key task of an administrator. This user-related information can be used to assign specific grants or perform some operation. Therefore, it is said to be an essential task to manage the Linux system effectively.

In today’s guide, we will demonstrate various methods to list the users in Ubuntu 22.04.

Why is it Important to List Users in Linux?

Before that, let’s consider the importance of getting the user’s related information.

User management: To grant certain users access to specific files or directories or to limit the actions that certain users can perform on the system.

System maintenance: You might want to run a command that affects all users or troubleshoot an issue specific to a particular user.

Resource utilization: You can also understand how the system is being used, which can help optimize resource utilization and ensure that the system runs efficiently.

Security: The user can identify and remove any unauthorized users, and you can also monitor user activity to detect and prevent security breaches.

How to List Users in Ubuntu 22.04?

There are several ways through which the users can be listed.

Let’s have a look at the number of methods and practice them.

Approach #1: cat /etc/passwd Command

The “/etc/passwd” file comprises a series of lines representing user-related information. We can access its content and show it on the terminal using the “cat” command as written below.

$ cat /etc/passwd

The fields can be described as.

‘username’: Represents the username

‘password’: This field directs to the encrypted password of the user, which is stored in the “/etc/shadow” file.

‘user ID’: Shows the unique identifier of the specific user.

‘group ID’: The group unique identifier to which the user belongs to.

‘user ID info’: This field contains the full name of the user.

‘home directory’: The path of the home directory of the specific user where the specific user files are stored.

‘shell’: this shows the path to the user’s default shell.

Approach #2: cat /etc/shadow

Although the “/etc/shadow” file shows the list of encrypted passwords of the specific user with each name of the user. However, this file also lists down the users with the help of the “cat” command.

$ sudo cat /etc/shadow

Text Description automatically generated
Only the first field is relevant, which shows the ‘username’ while others represent the password-related information.

Use more, less, tail, and head Commands With Approach #1 and Approach #2

The content of the “/etc/passwd” and “/etc/shadow” files can be filtered using the “head”, “tail”, “more”, and “less” commands. The head and tail commands will retrieve only the 10 rows from the top and the bottom of the file, respectively.

To fetch only the top 10 user’s list

$ head /etc/passwd

For the last 10 users from the bottom, use the command.

$ tail /etc/passwd

The more command will show the detailed content of the file, let’s practice it on the “/etc/shadow” file.

$ sudo more /etc/shadow

Text Description automatically generated While the less command will get only the limited content on the screen and the further content can be shown by pressing the enter key.

$ sudo less /etc/shadow

By pressing enter, more content can be looked at.

Approach #3: By awk Command

The awk command allows to filter of the data obtained from the “/etc/passwd” and the “/etc/shadow” files. Let’s practice the awk command on both files.

awk Command With /etc/passwd

The “/etc/passwd” file has a large and detailed content which may seem difficult in getting the exact detail, i.e., username. We can apply the “awk” command on that file “/etc/passwd” to get only usernames.

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

Text Description automatically generated
The “$1” will extract the first field which is the username, and the output is shown below.

awk Command With /etc/shadow

Following the awk command, the output of the /etc/shadow file can also be filtered using the $1 with the print keyword.

$ sudo awk -F ':' '{ print $1}' /etc/shadow

Text Description automatically generated
The output shows only the usernames on the terminal window.

While accessing the “/etc/shadow” file, ensure that you are logged in as a root user or the user is using the “sudo” keyword.

Approach #4: getent Command

The getent command can be applied in the terminal to get the list of users. For this, open a terminal and use the following command.

$ getent passwd

The last highlighted line shows the currently logged-in user.

Approach #5: compgen -u Command

The “compgen” is used to list down the commands that are associated with the specific keyword. To get the list of users, you can use the “-u” keyword with the “compgen” as given below.

$ compgen -u

Text Description automatically generated with low confidence
The above methods show the newly created users at the end of the list.

It can be observed that all the methods have the same output, either a refined one or with detailed content. The users can use any of the methods to list the users in Ubuntu.

Final Words

A system administrator must keep an eye on the list of users to perform specific operations, including user management, system maintenance, resource utilization, and security. The list of users can be retrieved from the files “/etc/passwd” and “/etc/shadow”. These methods, with suitable examples, have been practiced in this blog.

Print Friendly, PDF & Email