How to List Users on Ubuntu

In this guide, we will show you a couple of ways to list users on Ubuntu using the terminal.

Ubuntu List Users

Listing users on Ubuntu is useful for discovering who or what is currently running on your system.

In Linux and Ubuntu, not every user is used by an actual person. Users are also used for applications to run under.

Over the following sections, you will learn various ways to list users on the Ubuntu operating system. Each of these methods has its pros and cons, so use what you find easiest.

As we will be relying on the terminal ensure you have it open before continuing. On the desktop flavour of Ubuntu, you can open the terminal by pressing CTRL + ALT + T on your keyboard.

Using the “/etc/passwd” File to List Users on Ubuntu

All users on Ubuntu and Linux systems are referenced within the “/etc/passwd” file. As all the users are in one place, it is the easiest solution to list all available users on your system.

1. To list the users on Ubuntu, we will use the cat command to output the contents of the “/etc/passwd” file to the terminal.

Be prepared, as this command outputs more than just the names of each user,

cat /etc/passwd

2. Below is a small example of what will be output from the “/etc/passwd” file.

You can see that each user on Ubuntu has seven different pieces of information in this list.

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin

3. Now, with the list of users output by Ubuntu, let us explore what each piece of information means. Using this information, you can tell which of your users is a system user or not.

A semicolon separates each value, so let us explore each column.

  • User name
  • Password
  • User ID
  • User Group ID
  • Optional User information (Full name, room number, phone number, etc)
  • Home Directory
  • Default Login Shell

One of the easiest ways to differentiate between a user who can be logged in and a system user is by looking at the default login shell.

Typically, a user that can be logged in will have the shell set to something like “/bin/bash“. System users will typically use “/usr/sbin/nologin

Listing User’s Names on Ubuntu

As you have seen in the previous section, the “/etc/passwd” file will give you too much information if you are just after a list of users.

This is especially true if you only want to list the names of users on your Ubuntu system. Luckily, there is a straightforward way to work around this.

1. To only list the user names on Ubuntu, use the same command as before but pipe it to the cut command this time.

Using the cut command, we can separate each field by its semicolon and tell the command only to output the first field.

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

2. After running this command, you can see how only a list of user names is output. This is especially useful when you aren’t interested in other information, such as the user’s home directories.

root
daemon
bin
sys
sync
games
man
lp
mail

Listing Users using getent

Getent is a command that allows you to get entries from text-based databases on your Ubuntu system. One of these databases is the passwd file.

This command allows you to check whether a specific user exists on your Ubuntu system more easily than other solutions.

1. Using the following command on Ubuntu, you can use the getent command to list the users on your system.

You will notice that the result of this command is the same as using “cat“.

getent passwd

2. The result below shows that the getent command is equivalent to using the cat command. If you want a better explanation of the data returned by this command, look at the previous section.

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin

Only Listing the Names of Users with getent

Sometimes, you will not want all the information from the “/etc/passwd” file.

To get the getent command only to give us the names of users on our Ubuntu system, you will want to pass it into the cut command, as shown in the example below.

getent passwd | cut -d: -f1

Checking Whether a User Exists on Ubuntu

One of the critical things with the getent command is its ability to be used to check whether a specific user exists on your Ubuntu system. We don’t have to worry about using grep. We simply need to pass in the name of the user we are looking for.

1. To check whether a specific user exists on the Ubuntu operating system, you will want to use the command shown below.

Just replace “USERNAME” with the name of the user you want to check exists. This command will output the line from the “/etc/passwd” file.

getent passwd USERNAME

2. For example, if we want to check whether the user “pimylifeup” exists on our system, we could use the following line.

getent passwd pimylifeup

3. If the user doesn’t exist, you will see nothing output on your system.

Alternatively, you will see a line similar to the one below in the terminal.

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

Using the compgen command to List Users on Ubuntu

Another way of listing users on the Ubuntu operating system is to utilize the compgen command. This command is used to generate possible completion matches for bash scripting. By passing in a specific option, we can get this tool to output the users that exist on the Ubuntu system.

1. To get a list of the users on Ubuntu using compgen you will want to utilize the “-u” option. This option tells compgen to output the names of users.

compgen -u

2. The example below shows how compgen listed the users on our system. With this result, you don’t have to worry about stripping out additional information.

root
daemon
bin
sys
sync
games
man

Listing Currently Logged in Users on Ubuntu

As Ubuntu can support multiple logged-in users at once, you may want to list the other users who are logged in on your system.

We will be covering two different methods for listing users who are logged in on Ubuntu.

Listing Logged In Users using the who command

The first way to list logged-in users on Ubuntu is to utilize the who command.

This command gives you several different pieces of information about the user, including when they logged in and the IP address they are connecting from if they are using SSH.

1. Using the who command on Ubuntu is as straightforward as using the following within the terminal.

who

2. As we are currently the only logged in user, we only had the following output to the terminal.

You can see we are connecting over SSH from a device with the IP “192.168.0.87“.

pi       pts/0        2023-08-23 12:04 (192.168.0.87)

Listing Logged In Users Using the users command on Ubuntu

The other way to list who is logged in on Ubuntu is to utilize the “users” command. This command is similar to the one above, but doesn’t output additional information.

1. You can produce a list of users who are logged in by using the command below within the terminal.

users

2. Below, you can see the difference between these two commands, as only our users name is output this time.

pi

Conclusion

At this point in the tutorial, you should hopefully understand how you can list users on Ubuntu.

Listing the users on your system allows you to manage it better and track new users on your system.

Please feel free to comment below if you have questions about listing users on your system.

If you found this tutorial to be useful, be sure to check out our many other Ubuntu guides.

Leave a Reply

Your email address will not be published. Required fields are marked *