Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

How to List Users in Linux?

If you are a system admin or in general want to see all the users present in your Linux machine, then in this tutorial, you will learn 3 different ways to do this.

If you just want the command to do so without any bullshit, then here is the command that you can run on your Linux machine to list down the users, along with a bunch of other information too:

cat /etc/passwd

The /etc/passwd file keeps all the user's information in it. In this command, we used the cat command to read this file.

If you want to learn all three different ways with some additional tips and tricks then continue reading.

Following are the three ways to list down users in Linux:

  1. Reading /etc/passwd file

  2. Using getent command

  3. Using compgen command

Let's cover them all one by one, sharing some unique insights about each one as we cover them.

1. Reading the /etc/passwd file

We can either use the cat, less or more command to read the /etc/passwd file, to list down all the users. Here are the commands:

cat /etc/passwd

less /etc/passwd

more /etc/passwd 

USE /ETC/PASSWD TO LIST USER

When you run any of the above commands you will get a list of all the users available on your system, along with other relevant information, separated by a colon (:). Each line has information for one user.

Note: This is a list of all the available users on your machine, it doesn't mean logged-in users. To check the currently logged-in user, use the who command.

The /etc/passwd file stores a list of all the users, along with the following information about them:

  • User name.

  • Encrypted password (The value x means that the password is stored in the /etc/shadow file).

  • User ID number (UID).

  • User's group ID number (GID).

  • Comment which is generally the username only.

  • User home directory - For some user, this can be empty.

  • Login shell (default value is /bin/bash).

/ETC/PASSWD FILE CONTENT

How to display only Usernames using /etc/passwd?

We can use the awk command or the cut command. To use these commands we first use the cat command to list the content of the /etc/passwd file, then use the pipe operator, and then use the cut or awk command to show only the first column data, which is the username.

Either run this command:

cat /etc/passwd | cut -d: -f1

or use the following command:

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

Run any of the above commands, and you will see the following output:

LIST USERNAMES LINUX

2. List Users using the getent Command:

In Linux machines, the file /etc/nsswitch.conf stores a list of databases configured. You can use the cat command to see the contents of this file.

READ NSSWITCH.CONF FILE

As you can see in the output above, the passwd database is also listed in the output. Using the getent command we can access it and list all the users. Here is the command:

getent passwd

The output of the getnet command is similar to the content of the /etc/passwd file.

GETENT COMMAND TO LIST USERS

How to display only Usernames using getent command?

If you want to list down just the usernames using the getent command, we can do so using the awk command or the cut command.

Here is the command using the cut command:

getent passwd | cut -d: -f1

Or with awk command:

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

3. List Users using the compgen command

If we want to list down just the usernames, then we can also use the compgen command. This command is easier to use to list down all the usernames available in your Linux system.

Here is how to use this command:

compgen -u

COMPGEN COMMAND TO LIST USERS

How to check if a Username Exists?

You can use the compgen command to check if a username exists already or not, before creating any new user. We can use the grep command to search for any particular username out of all the available usernames.

If you want to search for daemon username, run the following command:

compgen -u | grep 'daemon'

We can use the grep command with the other two approaches too, but the commands will become complex, this one is easier.

Check for Current Active Users in Linux

The above 3 approaches will list down all the available users in your Linux system. But if you want to see the current active user, you can use the who command.

For example,

who


studytonight pts/0 2021-07-13 12:14 (62.27.117.907)

In the above output, we get the currently active user using which we logged into the system. The output can have multiple usernames, if multiple users are logged onto the same Linux machine.

Conclusion:

With this, we have reached the end of this guide. We covered 3 different ways to list down users in Linux and various other shortcuts like listing only usernames and searching for certain usernames.