Because of its multi-user features, Linux stands as the most liked operating system among people especially for those who love to run commands. Like its other exceptional characteristics, configuration and managing of users and groups is also a significant part of Linux system administration.

In Linux, there are several tools or program out there to list the user accounts on the system but instead of using any user listing tool we typically prefer user listing commands which fetch the details even from the depth of the user system accounts.

But before starting, there are some things you must have access to:

  • A Linux distribution must have installed and should be running.
  • A user with sudo privileges, if not, you can use sudo at the beginning of each command.
  • Having access to the terminal.

So let’s dive into the details of how to list users in Linux. Although, the article was accomplished using Ubuntu machine, but it’s equally usable for other Linux distributions.

Table of contents:

The File Containing Users in Linux

As a Linux user, you use different commands. For example, to create a user, delete a user, etc but have you ever thought how to count all the users in Linux or What file contains a list of the users on a Linux system?

You may already know that all the user information is stored in etc/passwd file and if we create a user then its information will also be automatically appended to this file.

Listing All Users

In this section, We will discuss here two methods that are considered to list users in Linux.

Using etc/passwd File

Open up your terminal and type:

$ sudo less /etc/passwd

Note: You will be supposed to start any command with the sudo keyword if you are not using the Linux system with sudo privileges.

listing users using /etc/passwd file

This command will result in listing users, see the output below:

Output of listing users with /etc/passwd file

From the image above you can clearly see the line represents the single user dividing into seven fields and distinguish with Colon(:) containing the following information:

  1. User name.
  2. The “x” indicating the password is encrypted.
  3. The user ID number which is generally known as UID (User identifier).
  4. User group ID which is known as GID (Group Identifier).
  5. Full user name.
  6. The login shell, the default is set to /bin/bash.

Using getent passwd Command

Doesn’t stop here, besides etc/passwd file, there are also other ways to list users in Linux. We all know nothing is impossible in the Linux world so if you don’t accomplish anything by attempting a single command, there are several other commands which can perform the same and sometimes with more depth results.

The getent passwd command is also used to list users in Linux. It retrieves the users listing using the /etc/nsswitch.conf file in which all the database information is stored. Here is a demo of this command telling how it actually brings the same result as in etc/passwd file:

$ getent password
Listing users using getent passwd command

Here, you can see it brings the same output:

Output of listing users using getent passwd command

Listing only User Names

If you want to list users by only their names, then you can use either cut or awk commands. They both do the job and there is no need to be confused, pick one of the following commands according to your will:

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

Or:

$ cut -d ':' -f1 /etc/passwd
Listing usernames using cut

The result will be something like this:

Output of using cut to list usernames

Listing Currently Logged in Users

It has been seen that people usually inquire about how to list users currently logged in the Linux server. If you are the one who is seeking how to list the usernames of currently logged users in Linux then this section of our post has your answer.

Below are the essential commands for that:

  • The w command is the one which holds the complete information of users that are currently using the machine and what processes they’re performing.
  • The who command shows the complete information of users that are currently logged in.
  • users displays the names of users that are currently on the system.

Below is an attempt to execute them:

Listing Logged in Users in Linux

Read also: Steganography using Linux Commands.

Listing All Users in a Group

The /etc/group file defines the groups on the Linux system. You can simply search this file to list and find all members of a group:

$ less /etc/group
Listing Users in a Group

And in results, it’ll show all the user groups on the system:

Listing Users in a Group Output

You can also use getent group command to do the same, and you can pipe it to grep to search for a specific username:

$ getent group | grep username

Listing Sudo Users

To list sudo privileged users, you easily do that using the following command:

$ grep '^sudo:.*$' /etc/group | cut -d: -f4

Or using getent group command:

$ getent group sudo | cut -d: -f4

Output:

arsl

In my machine, I have only one sudo user, but this will list all sudo available users in the system.

Listing Locked Users

The following command is responsible for listing locked users:

$ passwd -S -a | grep LK | cut -d " " -f1

Just a side note, you can lock a user using usermod -L username command, so you can test it out.

Conclusion

After you’ve completed reading this tutorial, you’ll be able to list the users in any Linux system and you should have an idea about how multiple user listing commands work and how users are stored in a Linux machine.

Learn also: Getting your Public IP Address in Linux.