Have you ever wanted to list and view all users and groups in your Linux system? Multiple commands help you create, delete, list, and view users in a Linux-based operating system. Here, we will talk about how to list users and groups in Linux. Keep along with us!
How to list users in Linux using the etc / passwd file?
To list users in a Linux-based system, you can use etc/passwd file with the below syntax:
$ cat /etc/passwd
Or
$less /etc/passwd
This file stores all the user information. Therefore, if you want to know how many users logged in to your system, you can execute one of the above commands. The output will be the number of lines and columns which contains vital information such as:
- Username.
- Encrypted password.
- UID or user identification number.
- GID or group identification number.
- User’s full name.
- User’s home directory.
- Login shell (defaults to /bin/bash).
The output would be something like this:
To read the lines, you can split them into columns. The first field is the username. Then, you can see the “x” word, which means the password is stored in the shadow file with the encrypted format.
The following field is the user ID; right after that, you can see the group ID, which is the primary group the user belongs to. Then, it displays the home directory for that particular user.
As you can see in the above example, not all users are actual users. Some of them are system users, and there are also some root users. However, the rest of them are real users.
How to list only the usernames in a Linux-based system?
If you do not need any other information and you want to view only the usernames (the content of the first column), you can use one of the following commands:
$ awk –F: '{ print $1}' /etc/passwd
Or
$ cut –d: -f /etc/passwd
This way, the system displays the list of usernames for you without showing the rest of the information. The output would be something like this:
root
daemon
bin
sys
sync
...
...
sshd
vagrant
jack
anne
How to list groups in Linux?
To check all the groups are available in your system, you can execute the following command:
$ cat /etc/group
If you type this command on your terminal and hit Enter, you can access all the group’s information in your Linux system. It will display all the groups that are available there.
If you want to count these clients by putting numbers at the beginning of each line, you can pipe the above command using “nl” at the end.
This way, you can see there are some built-in groups and some other groups you have created before.
The output will be the number of lines and columns which contains vital information such as:
- Group name.
- Group ID.
- The users that belong to that particular group.
How to list users in Linux using the “getent” Command?
The “getent” command helps you view entries from databases configured in the /etc/nsswitch.conf file. This will include the “passwd” database. Therefore, this command can be used to list all of the users available in a Linux-based system.
To check the list of all users in Linux, you can type in the following syntax on your terminal:
$ getnet passwd
The output would be something like this:
As you can see, the content is the same as the output of the /etc/passwd file. As the “getnet” command lists all users from /etc/passwd and LDAP database, you can use it to access the user’s authentication.
If you do not need any other information and you want to view only the usernames (the content of the first column), you can use one of the following commands:
$ getnet passwd | awk –F: '{ print $1}'
Or
$ getnet passwd | cut –d: -f1
How to check a particular user in Linux?
If you are going to check a particular user to see if it still exists in the system, you can use the “getnet” command with the following syntax:
$ getnet passwd | grep [Username]
Or
$ getnet passwd [Username]
This way, you can also view the login information of that particular user.
In order you are going to check the total number of accounts there are on your Linux system, you can pipe the above syntax to the “wc” command:
$ getnet passwd | wc –l
This way, the system will display a single number, which indicates the total number of user accounts on your Linux-based system.
The Bottom Line
Here, you learned how to list users and groups in a Linux system using different commands. I hope this article will be helpful for you. If you have any feedback, comments, or questions, feel free to share them with us. Also we offer that you buy linux vps server and you practice. Good luck!
People also read: