Updated April 18, 2023
Introduction to Linux List Users
Today Operating Systems can use multiple users with each one having its own set of settings and configuration. This makes things easier for the administrator or operator to work together on the same system. In this tutorial, we will discuss how to list users working in the Linux operating system. The users may be either human users or system users. Normal users are people who log in and interact with our system. System users are used for non-interactive background services.
How to List All Users in Linux Using /etc/passwd File?
All the information of local users is stored in /etc/passwd file. Every line in the file represents the login information of one user. To open this file you can either use the cat command or the less command.
Code:
less/etc/passwd
Output:
Explanation: The above output is produced in the system by using the command. From the output, it is seen that every line contains seven fields separated by a colon. Each line contains the following information.
- user name
- Encrypted password ( x means the password and it is stored in /etc/shadow file.
- the user ID number (UID)
- user’s group ID number (GID)
- full name of the user (GECOS)
- user’s home directory
- Login shell of the user, which defaults to /bin/bash
How to Display only the User Name?
Suppose you want to display only the user name who is currently using the system. You can either use awk or cut command line
Code:
awk -F: '{ print $1 }' /etc/passwd
cut -d: -f1 /etc/passwd
Output:
How to Print the List of All Users Using Getent Command?
This command does the same function as that of cut. But getent displays entries from databases supported by Name service switch libraries which are configured in /etc/nsswitch.conf. This database includes both /etc/passwd and LDAP. So getent command will display users from both passwd and LDAP.
Syntax:
getent [option] [database]
Display All Users: This is the same as using the cat command. If no options are provided getent will display all users from the password database.
Code:
getent passwd
Output:
Finding All Groups
To list all groups logged into the system, use the following command
Code:
getent group
Output:
Display only the User Name
To display only the field containing the username use the awk or cut command.
Code:
getent passwd | awk -F: '{ print $1}'getent passwd | cut -d: -f1
Output:
How to Display a Specific User in a group?
Syntax:
getent group | grep username
This command produces the output which contains the group name and the user name belonging to the group.
Code:
cat /etc/group | awk -F: '{print $1, $3, $4}' | while read group gid members; do
members=$members,$(awk -F: "\$4 == $gid {print \",\" \$1}" /etc/passwd);
echo "$group: $members" | sed 's/,,*/ /g';done
Output:
How to Check Whether a User Exists in a Linux System?
Getent command helps us to check whether a particular user name already exists in the system or not. If the username exists in the system it will produce the output as shown below. If the username doesn’t exist, it will not produce any output.
For example, if we want to find out whether the user name jack already exists in the system, use the command shown below
Code:
getent passwd | grep jack
Similarly, we can also check whether the user exists or not without the usage of the grep command.
Code:
getent passwd jack
If we want to find the number of user accounts in the Linux system, use the command below.
Code:
getent passwd | wc -1
The output will display number 45 as the number of user accounts that exist in the Linux system.
How to List All the Connected Users?
If you want to know how many users are currently logged into the system, use the who command as shown below.
Syntax
who [option] [file name]
- If no option is provided, the who command will display the following information. From the above command, you will get the four columns to explain below.
user@system:~$ who
- The first column denotes the name of the user logged into the system.
johndoe :0 2019-01-28 21:35 (:0)
- The second column specifies the type of connection. If the value is:x where x is the number then the connection is GUI or desktop connection such as GNOME or XDE. If the value is pts/x where x is the number, then the connection is made through SSH protocol
harrysmithpts/0 2019-02-01 09:51 (192.168.1.1)
- The third column specifies the date and time since the user is connected.
stevejonespts/1 2019-02-02 09:51 (192.168.1.173)
- The fourth column specifies the location where the user is connected if the IP address is displayed when the connection is remote. If the value is the same as the one in the second column, the connection is local.
System and Normal Users
- There is not much of a difference between a system and normal users. Usually, system users are created while installing the Operating System and other new packages. In a few cases, the system user can be created that will be used by some other application.
- Normal users are created by the root directory or by another user with Sudo privileges. Normal users possess a real login shell and a home directory.
- Each user has a numerical user ID called UID. The UID will be assigned automatically while creating the user using the useradd command from the /etc/login. defs file. UID depends on the UID_MAX and UID_MIN values.
- We can use the following command to check UID_MAX and UID_MIN values in our system.
Code:
grep -E '^UID_MIN|^UID_MAX' /etc/login.defs
Output:
The above output implies that normal users will have UIDs between 1000 and 60000. To list all normal users in the Linux system, use the command below.
Code:
getent passwd {1000..50000}
Output:
The UID_MAX and UID_MIN values of your system may be different. So let us use the generic version of the command as shown below.
Code:
eval getent passwd {$(awk '/^UID_MIN/ {print $2}' https://cdn.educba.com/etc/login.defs)..$(awk '/
^UID_MAX/ {print $2}' https://cdn.educba.com/etc/login.defs)}
Output:
If we want to display only the user names, we have to pipe the output to the cut command as shown below.
Code:
eval getent passwd {$(awk '/^UID_MIN/ {print $2}' https://cdn.educba.com/etc/login.defs)..$(awk
'/^UID_MAX/ {print $2}' https://cdn.educba.com/etc/login.defs)} | cut -d: -f1
Output:
Recommended Articles
We hope that this EDUCBA information on “Linux List Users” was beneficial to you. You can view EDUCBA’s recommended articles for more information.