| by Arround The Web | No comments

How to List All Users in a Linux System?

In the Linux system, it is a critical administrative task to manage users, add them, remove users, or assign new user privileges. In Linux, numerous users can simultaneously work on the same system. But security measures must be made to stop breaching other users’ private data. Information related to the local users is stored in the path “/etc/passwd”. In which, every row indicates the data of a single user that may contain the name of the user, user Id, directory of the user, and login details. When it comes to the listing of the user in Linux, there are multiple ways to list them, we will discuss some of these in this tutorial.

Prerequisites

To list users, we must have Linux running on our system and also access to the terminal to perform this task.

Method 1: Using the Cat Command

Cat command is the short form of concatenation, it is used to read the data of the file without opening it. In this, we will use the cat command to list all users in Linux. Additionally, it is used to make a new file and put data in it.

Syntax:

The given data below is the syntax to use the “cat” command where file_name represents the name of the file that has to be read.

linux@linux-Virtualbox:~$ cat file_name

To list the user using the “cat” command, we will first launch the terminal.

After that, we will run the command:

linux@linux-Virtualbox:~$ cat /etc/passwd

In the command above, we have used the “cat” command along with the file name “etc/passwd” which is the one in which all of the user’s data is stored. After running this command and by pressing enter, we get the output as shown in the snippet below which includes the list of the users that are stored in the file. As we discussed above, each row indicates the details of the single user.

If we only want to see the number of users that are allowed to use the system, we can just write the following command:

linux@linux-Virtualbox:~$ cat /etc/passwd | wc –l

In the command listed above, we used the “wc” command along with the “cat” command. “wc” is the one that is used to count the line, word, or bytes of the file. In our case, we are going to list the number of rows that are storing the single user data. That is why we have passed “-l” which means the lines are counted. After running the command above, we obtained the number of users whose data is stored in the file that in our case is “47”.

Method 2: Using “less” or “more”

The other method to list the users from any file is “less” or “more”. Less and more are the terminal pagers commands that allow us to read the files line by line or to read them page by page.

Syntax:

Below is the syntax to use the “less” command to list the users from any file.

linux@linux-Virtualbox:~$ less /etc/passwd

In the snippet, we have successfully executed the list of the users that are stored in the /etc/passwd file. The less command can also be used to read the list of users. It will display the users until it reaches the end of the terminal by scrolling the terminal using the down button, we can display the remaining data of the file.

Using the “more” command to list the users, below is the syntax to use the “more” command.

linux@linux-Virtualbox:~$ more /etc/passwd

This command has some limited functionalities. It will display some of the percentages of the file like in the snippet below. It displayed about 47% of the data of the file by pressing the “enter” key we can display the data to some percentage, so we have to press the “enter” key until the 100% of it is not displayed.

Text Description automatically generated

Method 3: awk Command

In this, we will discuss another method to list the users of the system which is an “awk” command. The awk command is useful only when we have to display the name of the user neglecting the other details that are stored in the file related to any user.

Syntax:

Below is the syntax to use the awk command to enlist the user’s name.

linux@linux-Virtualbox:~$ awk –F: ‘{print $1}/etc/passwd

In the syntax, colon “:” is used to separate the input that is given by the awk. After that, it prints the first value of each row where –F is a file along with the argument which is responsible for reading the file and then displaying it as an output. The /etc/passwd is the file name that we want to read.

Text Description automatically generated

If you have multiple pages in a single file and you want to read the file page by page by writing the following command, you can perform this task.

linux@linux-Virtualbox:~$ awk –F: ‘{print $1}/etc/passwd | less

In the above syntax, less will enable us to read the file page by page if multiple pages exist.

Method 4: Using the getent Command

The “getent” command is most similar to the “cat” command as we can display the complete details of the users along with the technical details. The “getent” is also responsible for displaying the complete details of the users.

Syntax:

The syntax for writing the getent command is given below. In this syntax, we just passed the name of the file along with the “getent” command.

linux@linux-Virtualbox:~$ getent passwd

After running the command, the whole file data is executed as shown in the below screenshot.

Text Description automatically generated

Conclusion

In this guide, we introduced the listing of the users that are using the same system at the same time. It is an administrative task to manage multiple users at a time. We also examined some of the techniques that are employed to enlist the users on Ubuntu 20.04. As we know it is necessary to manage the users but first, it is important to know the users who are using the system so it would be made secure for others to prevent data breaches between all of the users.

Share Button

Source: linuxhint.com

Leave a Reply