How do you get a list of all the users on a Ubuntu machine?

In this article, we'll cover three different ways to do this, including the compgen -u command, manually viewing or listing the contents of the /etc/passwd configuration file, and using the getent tool.

Let's go through each example!

Table Of Contents

Option 1: Compgen Command

First, you can use the compgen -u command to list all the local users on your machine.

compgen is a bash built-in command that uses the -u option to output each user on the machine.

The command looks like this:

    
      compgen -u
    
  

The output should look something like this:

    
      root
      daemon
      bin
      sys
      sync
      games
      man
      lp
      mail
      news
      uucp
      proxy
      www-data
      backup
      . . .
    
  

You can read more about compgen here.

Option 2: Examine Contents of the /etc/passwd File

The second option is to view the content of the /etc/passwd file on your machine.

You can open and view that file with this command:

    
      less /etc/passwd
    
  

The contents of that file should look similar to this:

    
      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 of that file represents a user account with seven fields separated by colons (:).

The fields are as follows (in that order):

  • Login name
  • Optional encrypted password
  • Numerical user ID
  • Numerical group ID
  • User name or comment field
  • User home directory
  • Option user command interpreter

Using the cat command, you can also output the entire list to your terminal:

    
      cat /etc/passwd
    
  

After running that command, you should see all the files listed in your terminal.

To list only the user's login name, you can use this command:

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

That will only list the login name for each user:

    
      root
      daemon
      bin
      sys
      sync
      games
      man
      lp
      mail
      news
      uucp
      proxy
      www-data
      backup
      . . .
    
  

There you go! There's the list of all your users!

Option 3: Use the Getent Tool

The third option is to use the getent passwd command to list all the users in the /etc/passwd file. This is a more succinct version of the second option and will output the same result.

getent is a UNIX command for getting entries from different files on a Linux machine. These files include ahosts, ahostsv4, ahostsv6, aliases, ethers (Ethernet addresses), group, gshadow, hosts, netgroup, networks, passwd, protocols, rpc, services, and shadow.

Here's what the command looks like:

    
      getent passwd
    
  

This outputs the contents of the entire /etc/passwd file:

    
      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
      . . .
    
  

That was the last of the three options.

Thanks for reading and happy computing!