How to List Users in Linux?

Posted in

How to List Users in Linux?
maazbinasad

Maaz Bin Asad
Last updated on April 24, 2024

    Linux is a multi-user operating system. It allows many users to work and interact on the same system simultaneously. The admin or the root user who has sudo privileges sets the permissions for reading or writing resources for these users or a group of users. Managing such a vast number of users in a single system along with their group information, permissions, etc., becomes quite difficult. Listing users in Linux is a frequently used operation by the root users. Here, we will discuss how to list users in Linux. The information related to users is available in a file called /etc/passwd . It has a list of all the users along with other sensitive information. We can leverage this file to display detailed information regarding all the users in our system. Please note that to use the commands that we are going to mention in this article, you will need access to sudo privileges. Also, it’s important to understand that working with sudo privileges is quite risky as it might lead to uninvited changes in system files that might crash your system. Hence, you need to take utmost care while working with sudo privileges.

    How to List Users in Linux?

    In this guide, we will look at a few examples of how to list users using different commands in Linux.

    1. Displaying users using the /etc/passwd file

    We can simply print the details of the /etc/passwd file to print the details of the user.

    $ cat /etc/passwd

     $ cat /etc/passwd

    You can see that we have several details of all the users in our system. You will get the below-mentioned details if you use the cat or less command to print this file:

    • Username.
    • The value x denotes the encrypted password. This is stored in a file called shadow inside the etc. directory.
    • The User ID.
    • The primary group contains the user.
    • GECOS (complete name).
    • Home directory.
    • The shell that is being used.

    You can also use the less command instead of the cat command to print the details of the /etc/passwd file. The list is quite exhaustive because it displays the list of system users as well. If you want to figure out the normal users from the complete list of system users, you can check for those values that have a UID greater than 1000. Also, those users that have ‘nologin’ written at the end are called pseudo users.

    2. Display the username only

    If you want to display only a list of all the usernames, you can either use the awk command or the cut command, along with some specific options. Let’s use the awk command to print the leading column of the file that stores the username of the users.

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

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

    Instead of the awk command, you can also use the cut command to display only the first file of the /etc/passwd file.

    $ cut -d: -f1 /etc/passwd

     $ cut -d: -f1 /etc/passwd

    3. Using the getent command

    The getent command can also be used to display a list of users the same way as printing the passwd file. However, the getent command uses information from the nsswitch libraries that are located in the path /etc/nsswitch.conf . The syntax of getent is:

    $ getent [option] [database]

    To display a list of all the users, we can use the following command:

    $ getent passwd

     $ getent passwd

    To find all the groups of the system and normal users in your machine, you can use the getent command with the group subcommand.

    $ getent group

     $ getent group

    If you want to display a list of only the username through the getent command, you can combine getent with awk to display only the first field from the passwd database.

    $ getent passwd | awk -F: '{ print $1}'

     $ getent passwd | awk -F: '{ print $1}'

    Alternatively, instead of the awk command, you can use the cut command as well.

    $ getent passwd | cut -d:   -f1

     $ getent passwd | cut -d: -f1

    4. How to check for an existing user?

    If you want to find out whether a particular user exists in the system or not, you can use the getent command on the passwd database along with the grep command to filter out results.

    $ getent passwd | grep test

     $ getent passwd | grep test

    5. How to list active users?

    You can also find out a list of all the users that are currently active or have a session running on your Linux system. This can be achieved using the who command. The syntax of the who command is:

    $ who [option] [file-name]

     $ who [option] [file-name]

    If we do not provide any option along with the who command, we will get the above result. The first field will tell you the username of the user who is actively using the system. The next field will tell you the connection type. It can be GUI, command-line driver, SSH, and so on. The next column specifies the timestamp and the last column specifies the IP address.

    6. Listing all the normal users

    Predominantly, the users in Linux can be categorized into two different types. These are system users and normal users. Normal users are created by the system users. A typical example of a system user is the root user, which is created as soon as we install the Linux distribution. You can also create other system users. The system users can create other users called normal users that have their own home directory, ID, login shell, and so forth. The user ID has a minimum and maximum value. You can find out these extremes using the following command.

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

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

    You can see that in our case, the minimum UID for a normal user is 1000, and the maximum one is 60000. Now, we can list all the users whose UID lies in this range so that we can get a list of all the normal users.

    $ getent passwd {1000..60000}

     $ getent passwd {1000..60000}

    Wrapping Up!

    In this comprehensive guide, we discussed users in Linux. We saw how and where the Linux system stores the information related to users. Also, we discussed many examples that will list the details of users, such as UID, username, Group, GECOS, timestamps, and so on. We also discussed how to filter out certain specific fields from this detailed information using commands like awk, cut, etc. We saw alternative ways to display a list of users using commands like getent, who, etc. Finally, we discussed the differences between the system and normal users and used a command to find the range of UIDs for normal users. Using this range, we displayed a list of all the normal users. We certainly hope that using this comprehensive and detailed guide, you will be able to manage and list users in Linux effectively! Happy Learning! People are also reading:

    Leave a Comment on this Post

    0 Comments