How to List Users on Linux-based Systems

In this guide, we will show you how to list the users on your Linux system.

linux list users

There are a variety of ways that you can list the users that exist on your Linux.

Knowing what users exist on your server can be crucial to managing your system.

You can also use these lists to see where users’ home directories are located or whether they can access the command line.

Within this guide, you will use commands that will work on all Linux-based distributions, including Ubuntu, Debian, and CentOS.

List All Users Information using the “/etc/passwd” File

On Linux systems, all user’s login information is stored within a file called “passwd“. This file is located within the “/etc/” folder on your system.

Each line located within this file represents a users that exists on your Linux system.

1. To view the contents of the “passwd” file, you can run the following command on your Linux device.

less /etc/passwd

We are using the less command as it allows you to paginate the contents of the file.

2. You can navigate up and down this file by using the ARROW keys or PG UP and PG DN.

linux list users using contents of passwd

You will notice that each line of this file contains several pieces of information, each separated by a colon (:).

The main thing you will be interested in is the first value, as this represents the user’s name.

3. For example, if we were to take the following line that sets out our “pimylifeup” user, we would end up with the following.

pimylifeup:x:1000:1000:pimylifeup,,,:/home/pimylifeup:/bin/bash

From this line, we can pull out several bits of information about our Linux user.

  • pimylifeup – This is the username for your user. The name is used to identify the user easily and to log in to that account.
  • x – The next value is the password for your user.

    If this value is set to “x“, it is an encrypted password stored within the “/etc/shadow” file.

  • 1000 – The first number you see is the UID (User ID Number)
  • 1000 – The second number represents the Primary GID (Group ID Number)
  • pimylifeup – This is the GECOS field.

    This contains general information about the user, such as their real name or phone number.

  • /home/pimylifeup – Here, you can see the home directory for the user.
  • /bin/bash – The final value stored within the “passwd” file is the login shell for the user. For our main user on Ubuntu, this was set to bash.

Only Listing Users from the Passwd File

If you only want to see the usernames and not the other bits of information provided by the “/etc/passwd” file, we can use the “awk” command.

The awk command allows us to extract data from a text file with relative ease. It is also packed with most Linux distributions.

1. Running the following command on your Linux system will process the “/etc/passwd" file.

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

awk will process the file, separating each line by the colon symbol (:). We then print out the first value retrieved.

2. From the results in this command, you should see a list of the Linux users on your system.

Linux List users with awk Command

List all Users Using the getent Command

We can use the “getent” command to display entries from databases configured within the “/etc/nsswitch.conf” config file.

1. One of the databases that are preconfigured to work is the “passwd” file.

This means we can use the getent command to return all users that exist on our Linux system.

getent passwd

2. You will see that the getent command will output the entire contents of the “passwd” file to the command line.

One of the advantages of using the getent command is that we can pass its response directly into other functions, such as grep or awk.

Use getent to list users on Linux

3. Like reading the contents out of the “passwd” file directly, we can use awk to filter the results.

We need to use the pipe character (|) so that the getent command sends its data directly to the awk command.

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

4. This command will only return the usernames for each of the users on your Linux system.

This is useful if you don’t need to know additional information such as the user’s group or home directory.

getent list only usersnames

Check if a Users Exists on Linux

If you only want to check if a specific user exists, you don’t need to list all of the users on your Linux system.

We can instead use the “getent” command again, as it allows us to pass in text to search the chosen database, which would be the username we want to check exists.

All we need to do is use “getent” followed by “passwd” then the name of the user you want to check exists.

getent passwd USERNAME

1. For example, if we wanted to check if a user called “pimylifeup” exists on our Linux system, we can use the following command.

getent passwd pimylifeup

2. If the user does indeed exist, getent will return you the row for that user from the “passwd” file.

pimylifeup:x:1000:1000:pimylifeup,,,:/home/pimylifeup:/bin/bash

However, if the user does not exist, no text will be output to the command line.

Conclusion

Hopefully, this guide has shown you how easy it is to list the users on your Linux-based system, such as Ubuntu.

Listing your users can help find out who exactly has an account on your system.

It can also give you an idea of how those users are set up. For example, you could use this to check if a user has access to the command line.

If you have had any issues listing the users on your system, please leave a comment below.

You can also check out our other Linux tutorials as well as our guides for the Ubuntu operating system.

Leave a Reply

Your email address will not be published.