How to List Users in Linux
Knowing how to list or count users on a Linux system can be very useful for managing who has access to use your server or machine.
What commands can we use to find users and what information can we show about them? In this article, we will go through a number of different commands to list all users and find specific users by their name in Linux.
List All Users from /etc/passwd
All local users are stored in
/etc/passwd and can be opened using a command-line based text editor such as
root:*:0:0:System Administrator:/var/root:/bin/sh daemon:*:1:1:System Services:/var/root:/usr/bin/false uucp:*:4:4:Unix to Unix Copy Protocol:/var/spool/uucp:/usr/sbin/uucico taskgated:*:13:13:Task Gate Daemon:/var/empty:/usr/bin/false networkd:*:24:24:Network Services:/var/networkd:/usr/bin/false installassistant:*:25:25:Install Assistant:/var/empty:/usr/bin/false lp:*:26:26:Printing Services:/var/spool/cups:/usr/bin/false postfix:*:27:27:Postfix Mail Server:/var/spool/postfix:/usr/bin/false john:*:1000:1000:John H:/home/john:/bin/bash james:*:5001:5001:James H:/home/james:/bin/bash
The above output looks messy but once you know what each component of a line represents it is easy enough to break down.
From left to right they are:
- User name
- Encrypted representation of the password
- User ID (UID)
- Group ID (GID)
- Full user name
- User root directory
- Shell default
Pro tip - typically a user with an ID of 1000 and above will be a “real” user and ones below will be “system” users, though there is no physical difference between them.
List Users with Getent
getent command we can get user information from
passwd and from the GNU Name Service Switch, which is obtained VIA the
/etc/nsswitch.conf configuration file.
To exit the above output press
: (colon) followed by
List Users with Compgen
If we just need a list of all users with no other information the
compgen command will work perfectly.
root daemon bin sys ... pollinate john -agent colord
To make the above output easier to look though we can pipe in the
sort command to order users alphabetically.
compgen -u | sort
_apt backup bin colours ... uucp uuidd vmail www-data
Check a User Exists in Linux
If we need to check if a user name exists we can pipe in the grep command to search lines in the user database. If no matches are found nothing will be returned.
getent passwd | grep john
Note - the above grep command is case sensitive; please follow the above link to the
grep command if you would like to learn about how to change the behaviour of grep.
Check Logged in Users in Linux
To check users that are logged into your system use the who command.
john pts/0 2020-07-07 22:18 (126.96.36.199)
Here are the columns of the who command from left to right.
- User name.
- How the user connected;
pts/0for terminal and
:0for GUI (Graphical User Interface).
- Time of log in
- User log in IP address
You now know a number of ways to find out what users have access to your Linux system and what users are logged in. There are other ways to filter down what users by their attributes, though that is more related to the usage of the grep command.