Finding out the list of users on a Linux system is a common situation, especially for system administrators. We will show you how to do this in today’s tutorial.
Linux is a multi-user platform. It ensures that multiple users can use it without the need for a new installation. Linux handles applications securely. No user can access other user files without proper authentication access. The list of accounts is shown on the Terminal and helps manage.
Let’s get started.
How to list Users in Linux
There are many ways you can list users in Linux.
1. Using etc/passwd command
One of the easiest ways to access the list of users in Linux is to find that information in the /etc/passwd file. To check its data, you need to use either less or cat.
$ cat /etc/passwd | more
You should see a lot of lines being outputted on the Terminal. Each line is divided into seven fields using a colon delimiter. The sequence of information is as below.
- User name
- Encrypted password
- UID: User ID number
- GID: User’s group ID number
- GECOS: User’s full name
- User home directory
- Login shell
All this information can be a little overwhelming and unnecessary. That’s why it is always a good idea to use the awk command only to display the username.
To do so, you need to use the following command.
awk - F: '{print $1}' /etc/passwd
You can also get the same result using the cut command.
cut -d: f1 /etc/passwd
For some reason, if the above command doesn’t work, then you need to use the following.
cut -d: -f 1 etc/passwd
2. Using the Getent command
You can also use getent command to display the list of users. In this case, it queries the passwd database, which is in the list of database configured in /etc/nsswitch.conf.
The command to list all the users using getent command is as below:
getent passwd
You can also use more or less command along with it to limit the output according to your window size.
The output is precisely similar to the information contained in the etc/passwd file. If you want to access the LDAP database, then you need to provide the user authentication with LDAP.
You can also trim the output of the user’s list using the awk and cut command. The commands are as below for your ready reference.
$ getent passwd | awk -F: '{print $1}'
$ getent passwd | cut - d: - f 1
3. Finding a specific user
Getting a long list of users is not desirable in most cases. What if you want to search for a particular user? It is possible, and here is how.
To do so, you need to use the grep command and pipe it with the getent command.
For example, if we want to search for tuts user, then we can do it using the following command.
getent passwd | grep tuts
If there is no output, then it means that the user is not registered in the system. There is also a more straightforward command that lets the job done. In this case, you do not need to use the grep command.
getent passwd tuts
If you get a reply, then the user is present; if not, then there is no user with that specific name.
4. The total number of users in the system
If you are curious to know the total number of users in the system, then you can check it using the following command:
getent passwd | wc -l
Here, we used the wc(word count) command to learn about the total number of users.
Conclusion
This leads us to the end of our tutorial on how to list users in Linux. Besides, we also learned how to find a specific user, and also to fish out the total number of users registered on a Linux system. Do you use the commands mentioned above for your daily job? What other tricks do you use? Do share with the rest of the FOSSLinux readers. Sharing is caring!