Linux List Users. How to List Users in Linux?


Administrators, developers and users are often faced with the need to display a Linux List Users on their server. The main reasons for searching for a user list are deleting, changing, or creating a new user. In this article we will provide several ways to obtain such a list of users.

The first and most popular would be to list users using the cat command and the /etc/passwd file.


Linux List User with cat command


Linux List-User with cat command

If you want to find a user in Linux you will need to use the /etc/passwd file, which contains a list of all users. Now we will show you how you can get a list of users in Linux using the cat command. Please note that this command may not be applicable to all Linux distributions (CentOs, Ubuntu, Debian etc.).

cat /etc/passwd


cat command

After entering this command you will get a list of strings. Each line contains 7 fields of information about one user.

  1. Username

  2. Encrypted password

  3. User ID number

  4. User's group ID number

  5. Full name of the user

  6. User home directory

  7. Login shell


List Users with less Command


List Users with less Command

Another way to open the /etc/passwd file is to use the less command. Please note that when using the less command, just like when using the cat command, you will receive output in the form of lines containing 7 fields and each line is separated by a (:) character. You can see the output of the fields, the same as with the cat command, in the previous paragraph.

less /etc/passwd


less Command


List Users with getent Command


List Users with getent Command

The getent command is used to work with /etc/nsswitch.conf. This file contains the passwd database. Thanks to this command you can find the data in the passwd file. To display the necessary information, use the command as follows:

getent passwd


getent Command

As with the commands described above, you will receive an output of the contents of the passwd file.

Do you need to check if such a user exists?


To check if a user with the name you need exists, you can use the getent command.

To do this, you need to use getent passwd as in the previous paragraph

getent passwd [username]


If after entering this command you receive a list or line with the name mentioned, then a user with that name exists.

If you did not receive information with that name, then such a user does not exist.


List Users with awk Command


List Users with awk Command

Do you need to find a selection based only on usernames? Then you will need to use the awk command to do this. Using the awk command will allow you to display a list of users without the additional fields that were displayed when using the cat and less commands.

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


awk Command


Conclusion


In this article, we showed several commands with which you can display a list of users on a Linux server, as well as search through the list of users. These commands can be used on CentOs, Debian, Ubuntu and other Linux distributions.