In this article, we will discuss how you can get a list of all the users in Linux, along with a brief guide to check whether a user exists on a system or not.

How to Show a List of All Users in Linux

When you create a new user, the username, password, and other details are stored in specific files on a Linux machine. Luckily, Linux allows you to read and modify such files without any restriction. Using these files, you can know information related to users such as their usernames, the user count, and more.

Using the Passwd File

The passwd file is a text file that contains the password records of all the users that are currently present in your system. This file is located in the /etc directory in your local storage and contains the following information:

Usernames Encrypted Passwords User ID User’s Group ID Full name The /home directory of the user User’s login shell

Type cat /etc/passwd or less /etc/passwd in your terminal to read the text file. Opening the /etc/passwd file will generate an output that looks something like this.

The aforementioned output contains seven fields that contain information related to the users. These fields are separated by a delimiter—in this case, colon. Each row in the output denotes a single user.

To get a list of all the usernames with the help of the passwd file:

Awk is a command-line utility that allows Linux users to create simple “one-line” programs that perform quick operations from the terminal. In the above-mentioned code:

-F stands for Field separator. Since the colon character is the delimiter in the /etc/passwd file, we pass the colon as the separator in the awk command. { print $1} instructs the system to print the first field. In this case, the first field is the username of the users. /etc/passwd file contains the data related to the users.

Executing the above command will output the usernames of all users. Since the /etc/passwd file contains system users, the output will include their usernames as well.

You can tweak the awk command slightly in order to print the full names of the users. Type in the following command to show the full names of users in Linux:

Since system users have the same username and full name, you won’t notice any difference in the output. Only the users that you have added to your system will have different usernames and full names.

Alternatively, you can also use cut instead of the awk command. The syntax of cut is quite similar to the awk command.

To print the usernames in Linux using cut:

Here, -d is the delimiter, f1 denotes the first field (username), and /etc/passwd is the text file that contains the data.

To print the first names of users using cut:

Similarly, you can output other fields from the /etc/passwd file by simply replacing f5 with f1-f7.

List Users With the getent Command

The getent command prints the content of important text files that act as a database for the system. Files such as /etc/passwd and /etc/nsswitch.conf contain information related to users and networks respectively and can be read using the getent command.

To print the content of the /etc/passwd file using getent:

The output will contain seven different fields separated by the colon character. Each field is reserved for particular information including the usernames and home directory paths of the users.

You can chain the getent command with awk or cut to get the list of usernames only.

To print the full names of the users:

Check Whether a User Exists or Not

In some situations, you might want to check if a user exists on your Linux system or not. The grep command comes in handy when you want to grab a specific text pattern from a file.

You can use any of the following commands to check the existence of a user.

If the user exists, the login information associated with them will be displayed on the screen. On the other hand, if the user is not present in the system, an error will occur.

To check whether a user exists on a system without using grep:

You can also pipe the getent or compgen command with grep and echo to display custom output.

The command above will print “User found” if the user exists on the system, and “User not found” if it does not.

Count the Number of Users on a System

To count the number of users that exist on a Linux system:

In the above commands, compgen and getent are responsible for displaying the list containing all the users and other information related to them. The wc stands for word count and is used to count the number of words or lines in the output. The -l flag denotes Lines.

Verifying User Accounts in Linux

Every Linux administrator should know how they can manage and administrate other users on a system. Mastering Linux commands that allow you to create, remove, control, and list down other users is a great way to get started with user management.

Getting comfortable with the Linux environment should be your first goal if you are just a beginner. There are certain things that you must do right after installing your first ever Linux distribution. Learning some basic commands is one of them and is essential for performing simple computing tasks on Linux.