In this tutorial, we’ll be going over how to list the users that have been created on your Linux system. This applies for all distributions, and is very easy to do.
Step 1. Connect to your server
To connect to your server via SSH as the root user, use the following command:
ssh root@IP_ADDRESS -p PORT_NUMBER
and replace “IP_ADDRESS” and “PORT_NUMBER” with your actual server IP address and SSH port number. Of course, you can also replace root with the username of any user you’d like, as all users can view the list of users on the system.
Step 2. The /etc/passwd file
In Linux, there isn’t any specific command that can list all users in your system. However, there is still a way to extract this information from our system. One important file that stores user information in your system is the /etc/passwd
file.
The /etc/passwd
file is a text file that stores all of the required information about all of the users that exist on our system.
Each entry in this file can have up to seven fields (separated by the colon “:” symbol) and each field contains some important information about that user. Here is an example of one entry for a user called “linux-user” stored in /etc/passwd
:
linux-user:x:1000:1000:Linux User:/home/linux-user:/bin/bash
And here is an explanation of each of the fields in this entry:
- Username – this is the name of the user, in our example “linux-user”
- Password: this field contains the encrypted password of the user, marked with “x”. The encrypted password is stored in the
/etc/shadow
file in our system and can only be accessed by the root user. - User ID number (UID) – this field contains a unique number for each user. The number 0 is reserved for the root user
- Group ID number (GID) – this is the ID number of the group, which is stored in the
/etc/group
file - User Info: this field contains all additional user information such as the user’s real name, phone numbers and other information.
- Home Directory: this field contains the path of the user’s home directory. In our example, the home directory of the “linux-user” user is
/home/linux-user
- Login shell: this field contains the path of the user’s login shell, for example
/bin/bash
3. List all users on your Linux system
Now that we know about the existence of the /etc/passwd
file and the information it contains, we can simply use the cat command to print all the contents of this file. You can then look at the first field of each entry which shows the name of each user in your system:
cat /etc/passwd
Output example:
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 . . . . . . sshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin mysql:x:109:114:MySQL Server,,,:/nonexistent:/bin/false linux-user:x:1000:1000:Linux User:/home/linux-user:/bin/bash
If you want to list only the first field for each user, which contains the actual username, you can use the cut
or the awk
command. For example:
cut -d: -f1 /etc/passwd
or
awk -F: '{ print $1}' /etc/passwd
Both commands will give you the following output:
root daemon bin sys sync games man lp mail . . . . . . sshd mysql: linux-user
Another way to list all users in your Linux system is with using the getent
command.
The getent
command displays all entries from databases supported by the Name Service Switch libraries, which are configured in /etc/nsswitch.conf
This file also contains the passwd database, so you can now use the following command again where you will get the list of all entries included in the passwd database:
getent passwd
How to Find if a Specific User Exists in Your System
Instead of listing all users, you may also want to check if any specific user exists on your Linux server. To do this, you can combine one of the previous commands with the grep
command. For example, if you want to check whether the “linux-user” user exists on your server, you can type the following command:
cat /etc/passwd | grep linux-user
If the user exists, the following output should be displayed on your screen:
linux-user:x:1000:1000:Linux User:/home/linux-user:/bin/bash
By this point, you now know how to see what users exist on your server, as well as search for a specific user on your server.
PS. If you liked this post on how to list all users in Linux, please share it with your friends through social media by using the share shortcuts, or simply leave a comment below. Thanks.