How to List Users in Ubuntu Linux

Do you need to find out which user accounts exist on your Ubuntu or Linux host? Read on to find out how to list users in Ubuntu Linux! In this tutorial we will be using some CLI tools to list Linux user accounts. Lets start by looking at the passwd file.

Listing all Users

On Linux, all user details for the users on the local Linux system is held in the /etc/passwd file. Each line in this special file is an entry for a user on the Linux system. To list what users exist we can simply list the contents of the passwd file:

$ cat /etc/passwd
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
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin

Each line in the output is a user on the system. If you haven’t seen this file before, some explantation is in order. Whilst each line represents a separate user, there is a lot of detail on each line. Whilst it at first looks like a single line, its actually seven columns/fields which are each separated by a colon. These fields are displayed in the following order:

  • Username
  • Encrypted password (x means that the password is stored in the /etc/shadow file).
  • User ID number (UID).
  • User’s group ID number (GID).
  • Full name of the user.
  • Location of the users home directory.
  • The users Login shell (which defaults to /bin/bash).

With this in mind, to list the users and display only the username we need to output this file, whilst only focussing on displaying the contents of the first field (username) for each entry in the file. We can do so easily by manipulating the output, by using the cut command.

$ cut -d: -f1 /etc/passwd

Here we are using the cut command to open the /etc/passwd file, whilst telling it to use “:” as the field delimiter, and to output only the first field. This results in an output like this:

$ cut -d: -f1 /etc/passwd
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp

As you can see, we now just have a list of the usernames!

Get a List of all Users using getent 

Another way to list local users on Linux is to use the getent command. The getent command displays entries from databases supported by the Name Service Switch libraries, which are configured in /etc/nsswitch.conf. One of these databases is the passwd database (which is the same file we looked at earlier). We can use getent to display it’s contents:

$ getent passwd
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

The output is exactly the same as in the earlier example, so nothing new here, but useful to be aware of! Again, you can manipulate the output using the cut command so that you only list the user names:

getent passwd | cut -d: -f1

Ok. So that’s how to list users, but what about if we wanted to determine if a particular user existed? Read on!

Using Grep to Search for Existing Users on Linux

As we have shown in the earlier examples, you can output and manipulate the output of the passwd file to list users on a Linux system. As the output is text we can use the grep command to search through it. For example, if we wanted to determine if the mail user existed on the system we could:

$ getent passwd | grep mail
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin

As you can see, we have a line returned, which indicates that the mail user does exist. As before, we can take this a step further to tidy up the output:

$ getent passwd | grep mail | cut -d: -f1
mail

If no results are returned, the it would mean the user doesn’t exist.

List Logged in Users

Now you know how to list users configured on a Linux system, you may also want to list logged in users. There are a number of ways to do this. One way is to use the w command:

$ w
 19:52:05 up 18 min,  1 user,  load average: 0.00, 0.06, 0.19
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
cloud_us pts/0    192.168.0.1     19:37    5.00s  0.04s  0.00s w

Final Word

You have learned how to list users in Ubuntu Linux, or indeed any other flavour of Linux. We have looked at how we can list users in the /etc/passwd file, how to format and search the output and how to use the getent command as an alternative way to list users on Linux.

Finally, we covered a quick way in which you can list logged in users on Linux.

Related posts

Mastering the Linux ifconfig Command

Docker Exec Command With Practical Examples

Debugging with Git Bisect

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More