Solved: How to List All Users on a Linux System

In Linux systems, effective user management is an essential yet challenging task for administrators. Within Linux, all local user information is stored at the path /etc/passwd, with each row representing a single user that contains their name, user ID, directory, and login details.

Several different methods are available to list these users in Linux, and in this tutorial, we will cover some of the popular ones utilizing the command line terminal.

Method 1: List Linux users with Cat Command

The cat command is a Unix utility used to view the contents of a text-based file without needing to open it. This convenient tool can be handy for Linux sysadmins, as it can be leveraged to list all users in the system.

The first example is to list all users on a Linux system using the cat command.

cat /etc/passwd

This will display the contents of the /etc/passwd file, which contains a list of all the users on the system. Each line in the file represents a single user, and the fields on each line are separated by colons (:).

Example output:

Here are two more examples of using the cat command to list all users on a Linux system:

Example cat command 1:

In the first example, the grep command filters the output and only shows users with bash as their shell.

cat /etc/passwd | grep "bash"

Example output:

Example cat command 2:

In the second example, the awk command extracts the first field (the username) from each line of the /etc/passwd file.

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

Example output:

Method 2: List Linux users with “more” or “less”

Utilizing the less and more commands in a UNIX terminal environment provides great flexibility when it comes to listing users from a file. To list all users on a Linux system using the less or more command, you can use the following.

less /etc/passwd

or

more /etc/passwd

This will display the contents of the /etc/passwd file, which contains a list of all the users on the system. Each line in the file represents a single user, and the fields on each line are separated by colons (:).

less and more are paging programs that allow you to view the contents of a text file one page at a time. You can use the up and down arrow keys to scroll through the file and press q to quit.

Here are two examples of using the less command to list all users on a Linux system:

Example less command 1:

In the second example, the grep command filters the output result and only show users with bash their shell.

less /etc/passwd | grep "bash"

Example less command 2:

In the third example, the awk command extracts the first field (the username) from each line of the /etc/passwd file.

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

You can use the more command similarly by replacing less with more in the above commands.

Method 3: List Linux users with AWK Command

In method three, you will now learn about using the AWK command to list Linux users in a system. The AWK command allows us to seek and print specific information from files. In this case, we can use this tool to list users present within the system more efficiently by disregarding other details associated with each user.

List all users on a Linux system using the awk command; you can use the following command:

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

This command will extract the first field (the username) from each line of the /etc/passwd file and print it to the terminal. The -F: option sets the field separator to a colon (:), and the {print $1} action tells awk to print the first field from each line.

You can use additional awk actions to extract and print other fields from the /etc/passwd file or to perform different operations on the data. For example, to print the username and the user ID (UID) for each user, you can use the following command:

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

This will print the first and third fields from each line, separated by a space.

Example output:

To list additional fields, you can add $4, $5, and so on, with commands added between each field.

Method 4: List Linux user” with “getent” Command

The “getent” command is extremely useful in gaining information on users and groups in a given system. It is similar to the “cat” command because it allows us to see complete details of user accounts with technical information included. The getent command also displays all related details regarding user authentication and authorization, giving administrators an all-inclusive way to gain knowledge of specific users and their roles in a network.

List all users on a Linux system using the getent command; you can use the following command:

getent passwd

This will display a list of all the users on the system, similar to the output of the cat /etc/passwd command. Each line in the output represents a single user, and the fields on each line are separated by colons (:).

Example output:

The getent command is a utility that retrieves entries from several system databases, including /etc/passwd, /etc/group, and others. You can use getent to access a wide range of information about the system and its users, groups, and network settings.

For example, to list all the groups on the system, you can use the following command:

getent group

Example output:

To list the network protocols supported by the system, you can use the following command:

getent protocols

Example output:

You can use the getent command with other options and arguments to access specific pieces of information or to perform other tasks. You can consult the getent man page or other online resources for more details.

Conclusion

As a system administrator, it is your job to understand how the system works and to find ways to optimize or secure it. In this tutorial, you have learned four methods using cli commands in a Linux terminal to find Linux users in a listed format. These methods will help you narrow your search and find the information you need quickly and easily.

Your Mastodon Instance
Share to...