Managing users in Ubuntu is essential for system administrators to ensure proper security of the Ubuntu system. In Ubuntu, you can list users to quickly check the existing users on your system and search for specific users to get detailed information about them.
In this blog post, I will discuss how you can list all users in Ubuntu using the command-line interface (CLI). I will also differentiate between system users and normal users and explain how to find specific users and retrieve their details.
Table of Contents
Listing Users in Ubuntu
There are various methods to list users in Ubuntu, each providing a list of users on your Ubuntu system. In the following section, I will explain different ways to list users, and you can choose any method that suits your needs.
Method 1: Using the cat Command
The simplest method to list users in a Linux system, including Ubuntu, is by using the cat
command. The /etc/passwd
file in Ubuntu contains all the information about users on your system, including their usernames
, UIDs, GIDs, home directories, and default shells. You can list all the information with the following command:
cat /etc/passwd
This command will display output similar to the following:
If this seems overwhelming, you can modify the command to list only the usernames on each line. To print only the usernames, use the following command:
cat /etc/passwd | cut -d: -f1
After finding a specific user in your Ubuntu system, you can modify this command to retrieve the full information for that specific user instead of listing all the users. Use the following command:
cat /etc/passwd | grep "username"
This command will print all the information associated with the specified username
.
Method 2: Using the w Command to Print Only Logged-In Users
The w
command is another easy-to-use command to list all the users who are currently logged in to your Ubuntu system. Simply type w
in the terminal, and it will display all the logged-in users:
w
Method 3: Using the cut
and grep Commands to List All Users
You can also utilize the cut
and grep
commands together to list all users in the Ubuntu system. This command will produce output similar to the first method using the cat
command. You can use either of these methods to list the users:
cut -d: -f1 /etc/passwd | grep -vE "nologin|false"
This command filters out users who have /usr/sbin/nologin
or /bin/false
as their login shells.
Method 4: Using the awk Command to List All Users
Another alternative to the cat
and cut
commands in Ubuntu is using the awk
command to list all users on your system:
awk -F: '{ print $1 }' /etc/passwd
This awk
command is an alternative to the cat
command. It will print the first field, which represents the username, of each user entry stored in the /etc/passwd
file.
Finding Specific Users in Ubuntu
In many situations, you may need to list all the users to find a specific user. You can find specific users with a single command without manually searching through the user list.
1. Using the grep Command
The simplest way to find a user in the /etc/passwd
file is by using the grep
command, which is a popular command for searching:
grep 'username' /etc/passwd
This command will provide you with the specific user’s information you are looking for.
2. Using the id Command
You can also utilize the id
command to find out specific users in your Linux system. This command provides useful information about the user, which is different from the previous command. To use this command, you need to specify the username in the following way:
id username
This command will output useful information about the user, including the UID, GID, and group membership.
Obtaining Details About Specific Users
In the previous section, we discussed how to find specific users on your Ubuntu system, and these commands provided some useful information along with the searches.
1. Using the grep Command
For example, you can use the grep
command to find basic information about a user with the following command:
grep 'username' /etc/passwd
This command will provide basic information such as the username, UID, GID, home directory, and default shell for the user.
2. Using the finger Command
If you need additional information, you can use the finger
command. This command will provide you with useful information such as when the user last logged in to the system and how much time the user has been active on the system. You can use the finger
command in the following way:
finger username
The output will look similar to the following:
The finger
command provides more detailed information about a specific user on your Ubuntu system.
Differentiating Between System and Normal Users
You can differentiate between system and normal users by inspecting their User IDs (UIDs). UID 0 is reserved for the root user, and UIDs lower than 1000 are typically reserved for system users. Normal users have UIDs greater than or equal to 1000.
In the /etc/passwd
file of Ubuntu, you can find both system and normal users listed, along with their respective UIDs. The format of user entries in the /etc/passwd
file is as follows:
To list all users in Ubuntu from the /etc/passwd
file, you can use the following command:
cat /etc/passwd
System Users
System users are special-purpose users on your Ubuntu system. They are responsible for running processes and services. System users are typically assigned lower UID numbers. You can filter system users with lower UID numbers using the following command:
cut -d: -f1,3,7 /etc/passwd | awk -F: '$2 < 1000 { print $1 }'
This command will list all the users whose UID is less than 1000.
Normal Users
Normal users are created for human users, and they are assigned specific permissions and privileges for various actions in the system. Normal users have UID numbers greater than 1000. To filter and list normal users in your Ubuntu system, you can use the following command:
cut -d: -f1,3,7 /etc/passwd | awk -F: '$2 >= 1000 { print $1 }'
This command will filter all the users whose UID is greater than or equal to 1000, thus listing the normal users in your Ubuntu system.
Conclusion
In this blog post, I have covered the basics of managing users in Ubuntu and discussed how to identify normal and system users. I have also shown various methods for listing users in the Ubuntu system. With the information provided, you can effectively manage users on your Ubuntu system. Now it’s your turn to try out these methods and see which one is your favorite for listing users in Ubuntu.
Feel free to share your thoughts and let me know which method you prefer for listing users in Ubuntu.