Linux is a multiple-user operating system, and user management is a basic administrative task. We need to administer the users in a system where multiple users work. To ensure the users’ privacy, we must know the correct number of users and other necessary details about every user in the system.
In this guide, we will show the methods of listing the users with details in Linux, only the user names, numbers of users, and other details like checking the user in the system.
Let’s start!
- Method 1: Using the “cat” Command
- Method 2: Using the “getnet” Command
- Method 3: Using the “awk” Command
- Bonus Tip: Get the Group Name of the User
Let’s start with the first method.
Method 1: Using the “cat” Command
The “cat” command’s basic purpose is to show the files’ data. Here, the “cat” command will be used on the “/etc/passwd” and “/etc/shadow” files:
Using /etc/passwd File
There are various scenarios to get the list of users through the “/etc/passwd” file. Let’s have a look at the following examples.
Example 1: Get the List of the Users
The “cat” command is applied to the “/etc/passwd” file to get the list of users as follows:
$ cat /etc/passwd
The above output shows the content of the user files delimited by “colon (:)”, and every field shows specific information about the User. Each line in the above output shows the following information about the user:
User Name : Password (Encrypted): User Identification (UID) : User Group ID (GID) : Full Name : Home Directory : Shell Used
Example 2: Get Only User Names
If you want to print the system “User Names” only, use the following command:
$ cat /etc/passwd | cut -d: -f1
Example 3: Get the Total Count of the Users
If you want to get the total number of users in the system, execute the below command:
$ cat /etc/passwd | wc -l
The output shows the number of users in current Linux system.
Using the /etc/shadow File
This file keeps the password hashes of the user accounts in the Linux system. Using the cat command on this file can get you the list of users as follows:
$ sudo cat /etc/shadow
Method 2: Using the “getnet” Command
In this section, we will use the “getnet” command to get the list of users with all its details, only user names and the number of users in the system. Moreover, the “getnet” command can also be used to check the user in the system.
Example 1: Get the List of System and Normal Users With Details
To list the user’s details in Linux, use the below-mentioned command:
$ getent passwd
Example 2: Get Only the Names of the Users
If you want to show the user’s names only in the system, use the below commands (Both commands will show the same output):
$ getent passwd | awk -F: '{ print $1}'
Example 3: Check the Existance of the User
Do you want to find if the user exists in the system to avoid creating duplicate users? Yes, the command “getnet passwd <user-name>” will give the user’s details.
Below, both commands will find you the user exists or not, in the system:
$ getent passwd itslinuxfoss
$ getent passwd | grep islinuxfoss
Example 4: Get Only Normal Users
A system consists of a system and a normal user. The system user is created while installing the operating system or new dependencies, while the normal users are created by the root user or user having sudo privileges. A normal user is logged in from the shell and has a home directory.
In Linux, we mostly require to get the normal/regular users from the system, the below command will list all the normal users in the system:
$ getent passwd {1000..60000}
The “{1000..60000}” represents the range of the user ids of a normal user.
Method 3: Using the “awk” command
Another method to get the system users’ names are utilizing the “awk” command as shown below:
$ awk -F: '{ print $1}' /etc/passwd
This “awk” utility fetches the details from the “/etc/passwd” file.
Bonus Tip: Get the Group Name of the User
If you want to check the user’s group, use the “groups <user-name>” command. The following command will show the user group named “itslinuxfoss”:
$ groups itslinuxfoss
That’s the end of this guide!
Conclusion
To list the users in Linux with all the details, use the “cat /etc/passwd” command. If you want to get only the user’s names in the system, use the “cat /etc/passwd | cut -d: -f1”, “cat /etc/passwd | awk -F: ‘{print $1}’”, and “awk -F: ‘{ print $1}’ /etc/passwd” commands. This post has provided the possible methods to list the users in the Linux system.