How to List User in Linux with Command Line

How to List User in Linux with Command Line

Fortunately, with the advancement of technology, several users can connect to a single Linux system and perform their tasks using the information available in that system. This feature has played an important role in the efficiency, and speed of carrying out projects and administrative tasks; but on the other hand, security has been weakened by the access of several users to a single system, that Usually, the system manager undertakes the task of improving and maintaining the security of the system.

In order to perform this vital task, in addition to identifying users who have permission to access the system, Linux system administrators can block user access to certain sections or delete the user and create a new user with new privileges to connect to the Linux system.

One of the solutions to protect the privacy of users who will have access to a single Linux system is to list Linux users, which is also effective in increasing security. Sometimes the Linux system administrator makes changes in the privileges of users to access and control the system. In this way, the administrator can extend or specify a privilege for a user who is in charge of a specific task, and also, can violate the privilege of another user in accessing the system. in this way, the administrator can control the access of users and improve security.

Therefore, administrators use various methods to increase the privacy security of users who connect to a Linux system, and listing Linux users through the command line is one of the most effective methods. You can use this feature in the graphical user interface or the terminal environment according to the operating system you have. In this tutorial, we describe how to list Linux users in the command line in several ways. So follow us until the end of the article.

prerequisite

To be able to list the users connected to a Linux system by the commands that we will explain below, you must first have a system that runs the Linux operating system or you should buy Linux VPS and then manage the users through the commands that we will teach.

  • Linux system
  • Access to the command line

How to list users in Ubuntu, Debian, CentOS

There are two types of users in Linux systems: regular users and system users.

System users: Some users are created by the system to run processes that need to run in the background. These users are non-interactive and do not need to communicate with other users to perform their tasks. For example, the root user who has administrative permissions is known as the system user.

Normal users: are human users that the root user has created by granting the necessary permissions. Normal users have a home directory and a login shell to store their files.

In Linux, the /etc/passwd file contains user information, including user UID (identification number), user name, and information about the login shell and the main directory. To manage users, this file must be accessed, and then administrators can list the users based on the information in The /etc/passwd file.

We will guide you through different commands to list the users connected to a Linux system, in this tutorial we will use the following commands:

  • The “cat” command
  • The “awk” command
  • The “compgen” command
  • The less and more command
  • The “getent” command
  • The” grep” command

First Method : Using the “cat” command to list users

This method is the easiest way to access the contents of the/etc/passwd file and list users.

First, enter the terminal environment of your Linux system and execute the “cat” command to access comprehensive information about passwords and usernames stored in the /etc/passwd file.

$ cat /etc/passwd

By executing this command, you will get a list of passwords and usernames along with complete information.

List Linux User in Command Line

You can use the following command to view the number of users:

cat /etc/passwd | wc –l

The wc command provides you with the statistics of the number of users, and you can find out the number of users by counting the lines you receive in the output.

Second method: using the “awk” command to list users

In this method, the “awk” command displays usernames without additional explanations and more details. This command is useful for administrators who do not need more information about users that is provided in the output of the “cat” command. The syntax of the “awk” asks for the first field in each output line in the /etc/passwd file because the data fields in the /etc/passwd file are separated from each other by the colon symbol.

Therefore, enter the terminal environment and run the following command to view usernames:

$ awk –F: ‘{ print $1}’ /etc/passwd

List Linux User in Command Line

Also, the following command to view the results page by page can be useful for better management:

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

The third method: using the “compgen” command to list users

The output of this command is the same as the “awk” command, and you can access only the usernames stored in the /etc/passwd file by executing this command. Therefore, to view usernames, type the following command in the command line:

$ compgen –u

Fourth method: List users using Terminal Pagers

This method is used for situations where the administrator has to control many users and limit the output of the /etc/passwd file and better review the contents of the /etc/passwd file; In this situation, the command terminal pager (Less and more) is the most efficient way to manage a large number of users.

If you want to use the less command to review the contents of the /etc/passwd file, you must enter the following command:

less /etc/passwd

The output will contain the first page of the file, which you can navigate in the file using the keyboard and view the available data.

The more command also displays the same result as the command above, but this command has more limitations in functions:

more /etc/passwd

Fifth method: List users using the “getent” command

By executing the “getent” command, like the “cat” command, you can access more details.It usually checks system database entries and displays complete information about system database entries by looking up the database stored in the /etc/nsswitch.conf file, which by default The passwd database is saved.

Enter the following command to list the information in the passwd database:

getent passwd

The output of the above command is almost identical to the output of the “cat” command, which provides complete details of the users on your Linux system.

List Linux User in Command Line

The “getent” command can also help you find specific users. For this purpose, type the following command:

getent passwd [username]

The display of the corresponding passwd input line command indicates the existence of the desired user.

Also, in the passwd database, you can search for the UID by executing the getent command:

getent passwd [UID]

In the output, the user input with the specified UID is displayed.

If you want to search for users in a specific range using the getent command, enter the following command:

getent passwd {[first-UID]..[last-UID]}

The output of the above command displays the users with UIDs that are in our defined range.

 sixth method: List users using the grep command

As we introduced you to the types of Linux system users at the beginning of the article, you know that normal and system users each have their unique UID so that you can distinguish them from each other.

The UID of system users is in the range of 0 (root user) to 999 and from 1000 onwards belongs to normal users and new users are given the smallest UID that has not been used yet.

If you want to access the /etc/login.defs file to check the UID range of normal users, enter the grep command:

grep -E '^UID_MIN|^UID_MAX' /etc/login.defs

With the help of the output of the above command, you can find out the UID range for a normal user because it displays the smallest and largest UID that a normal user can get.

FAQ

The who command provides all the information of the users who are logged in and are performing operations.

The information of all system users is stored in the "/etc/passwd" file. This information includes the user's name, UID, group ID (GID), the user's main directory, and the user's shell.

Conclusion

In this tutorial, we have provided several methods to list Linux users in the command line so that you can use them according to your needs. Some methods, in addition to listing the users of the Linux system, provide more information that facilitates the task of administrators.

we hope this tutorial has been useful for listing Linux system users as well as searching for them for more efficient management. If you encounter any problem in any section, let us know in the comments section.

Thank you very much for choosing our article to read.

Leave a Reply

Your email address will not be published. Required fields are marked.