As a Linux system administrator or regular user, one key component of keeping your system secure is managing what privilege every user has over a particular file, directory, or settings. To achieve that, you need to have a detailed understanding of all users present on your system.
You have probably encountered commands on how to create or delete a user on your system. But how do you list all users on your system? If that’s an issue of concern to you, then worry not, as this post will give you a comprehensive guide on ‘how to list all users on Ubuntu.’
Note: This post will use Ubuntu 20.04 LTS as the choice release. However, all the steps described here should work for any other Ubuntu release, including the Ubuntu server.
Listing users on Ubuntu
You can use different methods to list users on any Linux distribution. However, this post will focus on the two most popular and easy-to-use ways.
- List users using the
/etc/passwd
file. - List users using the
getent
command.
[Method 1] List Users Using the /etc/passwd
File
The /etc/passwd
file stores important user information required during log-in. One thing to note about this file is that it’s an ASCII text file. Therefore, you can easily edit it using any of your favorite text editors, such as nano or vim. Execute any of the commands to see the contents of this file.
cat /etc/passwd
less /etc/passwd
Each line in this field represents a single user and has seven fields separated by a full colon. Let’s have an in-depth look at these fields in order.
- Username (First field): This field represents the username used for log-in.
- Password (Second field): This field represents the encrypted password of that particular user. The
x
symbol shows that the password is stored in the/etc/shadow
file.
Note: You cannot update a user password by editing the /etc/passwd file. You will need to use the passwd command, which will compute the hash of the password typed on the Terminal. - User ID – UID (Third field): Each user on a Linux system has a unique user ID. UID 0 (zero) is default reserved for the root user. The UIDs 1 – 99 are reserved for different system users. UIDs 100 – 999 are reserved for different administrative user accounts and groups. Other normal users are assigned UID 1000 and above.
- Group ID – GID (Fourth field): This field represents the primary group to which the user belongs.
- User ID Info – GECOS (Sixth Field): This field holds additional user information like address, phone number etc.
- Home directory (Seventh field): This field represents the user’s default home directory when they log in.
Tip: When you look at the /etc/passwd
file thoroughly, you will notice there are many other users (some of which you don’t even know) other than the users you created. These other users are known as “service account.” They are not necessarily used for logging in from the log-in screen but mainly for “separating privileges.” For example, the MySQL user can only access specific files and configurations, not the entire system.
Display only the Username
When you cat
or less
the /etc/passwd
file, it comes with lots of information. Luckily, there are different hacks that you can use to list only a specific field. For example, use any of the commands below to list only the username (first field) in the /etc/passwd
file.
awk -F: '{ print $1}' /etc/passwd
or,
cut -d: -f1 /etc/passwd
[Method 2] List Users Using the getent
Command
getent
is a Linux command that you can use to fetch entries from various important Linux files known as databases. One such file is the /etc/passwd
file that you learned about in the previous sections. Therefore, you can also use the getent
command to list all users in your system.
getent
will fetch users from the /etc/passwd
file, and if you use LDAP for user authentication, it will also fetch users from the LDAP database.
Use the command below to list all users using the getent command.
getent passwd
If you want to fetch information about a specific user, getent
provides you with a simple-to-use syntax shown below.
getent [database] [... key]
For example, execute the command below to fetch the details of the root
user.
getent passed root
If you use the command above to fetch details of a specific user and it doesn’t return an output, that means the user is not available in the system.
Additionally, you can also pipe the output of the getent
command to other commands like grep
or cut
to list a particular field in the database. For example, execute the commands below to list the first field (username) in the passed database.
getent passwd | awk -F: '{ print $1}'
or,
getent passwd | cut -d: -f1
Normal and System Users
System users are created when installing the operating system. Most of these are the users you will find inside the /etc/passwd
file you did not create yourself. However, you can create an additional system user to run a specific service on the system. On the other hand, normal users are users created by the root user and have an interactive log-in capability.
As you learned from the previous section, each user is assigned a unique user ID (UID). When you create a new user using the useradd
command and don’t specify the UID, the system will automatically set a unique ID selected from the /etc/login.defs
file. This file specifies the UID min and max values.
Use the command below to check your system’s min and max values of normal users’ UIDs.
grep -E '^UID_MIN|^UID_MAX' /etc/login.defs
From the output above, you know that normal users are assigned UIDs from 1000 to 60000. This information is helpful as you can easily list all normal users using the command below.
getent passwd {1000..60000}
However, since the min and max UIDs might differ in other Linux distributions, the recommended command to list all normal users is:
eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)}
Conclusion
This post has given you different methods and techniques that you can use to list users on your Linux system. Although we used Ubuntu as the distribution of choice for this post, these commands should also work for other distributions like Debian or RHEL. Was this post helpful? Do you have any comments or suggestions? Please don’t hesitate to let us know in the comments below.