How to List Raspberry Pi Users

How do you list the users on a Raspberry Pi? Here's the short answer:

You can get a list of the users on a Raspberry Pi by dumping the contents of the /etc/passwd file and filtering out all the service based users. As an alternative you can use awk to return a sorted list of usernames.

Step 1. Get to the command line

  • Remote login over ssh or open up a terminal window via the desktop

Step 2. List all users including system users

  • To see a list of all users, use the cat utility to print all the lines of the passwd file to the screen:
$ cat /etc/passwd

It will contain one user on each line.

But the file is filled with user accounts that are used by the system and services, too.

What if you are only interested in real users and not service based users?

For that you need a filter.

Step 3. List only users with home directories

  • To filter out the service users you can pipe the output of the cat command through grep:
$ cat /etc/passwd | grep home

The grep command will only return lines with the word home in them. This works because the passwd file contains a list of every real users home folder.

  • Example output:
pi:x:1000:1000:,,,:/home/pi:/bin/bash
mitch:x:1001:1001:Mitch Allen:/home/mitch:/bin/bash

This assumes that when you created each user, you included the option to also create a home folder for them. That would normally be the case.

Step 4. List only usernames

  • If you would like to list only the usernames, use this awk command:
$ awk -F ":" '/home/ {print $1}' /etc/passwd | sort

The awk command above does the following:

  • tokenizes each line in /etc/passwd using the colon character (:) as a separator
  • filters for lines that only contain the word home
  • prints the first token in the line (the username)
  • pipes the output through the sort command to return a sorted list
  • Example output:
mitch
pi

Step 5. List logins

  • You can also list users using lslogins with the -u flag:
$ lslogins -u
  • Example output:
 UID USER  PROC PWD-LOCK PWD-DENY  LAST-LOGIN GECOS
   0 root    99                               root
1000 pi       5                         08:47 ,,,
1001 mitch    0                   Aug29/17:22 Mitch Allen

The advantage of this kind of output is that you can see when a user was last logged in.

Though it's a bit more difficult to parse for just the usernames.

Step 6. Use getent

Even though the /etc/passwd file looks like text, the operating systems sees it as a special kind of database file.

  • To access such a database file you can use getent, like this:
$ getent passwd {1000..6000}

The parameter is a range of key values. In this case the UID of each user. Starting with pi at 1000.

  • Example output:
pi:x:1000:1000:,,,:/home/pi:/bin/bash
mitch:x:1001:1001:Mitch Allen:/home/mitch:/bin/bash

It looks just like the output as if you had used cat /etc/passwd | grep home.

Filter using awk

  • You can apply the same awk command from before. But this time you would pipe the output from the getent command to it like this:
$ getent passwd {1000..6000} | awk -F ":" '/home/ {print $1}' | sort
  • Example output:
mitch
pi

Filter by username

  • To see information for just one user you can filter using getent by their username:
getent passwd pi
  • Example output:
pi:x:1000:1000:,,,:/home/pi:/bin/bash

Troubleshooting

Access denied

If for some reason you don't have the rights to see the contents of /etc/passwd try logging in as the default pi user or using sudo.

Conclusion

In this article you learned how to:

  • See a list of all users including system and service based users
  • Filter the list down to non-service users using cat, piping and grep
  • Use awk to return just the usernames and sort them
  • Use lslogins to list an alternative form of information
  • Use getent to query the password file like a database using keys

Related Articles

References

  • Terminal - Raspberry Pi Documentation [1]

 

.