Introduction
Before we start discussing about how to list users in linux, let's first understand-What is List Users ?
On Linux, the list users
command is used to retrieve information about the users registered on the system. The most common command for listing users is cat /etc/passwd
, which displays a colon-separated list of user account details.
As a system administrator, you may have the requirement of listing all the users on your host. There are commands to create a user, delete a user as well as, list the logged-in users.
In this tutorial, you will list users on Linux. We will also address a few FAQs on how to list users on Linux.
List All Users using /etc/passwd File
1) /etc/passwd
file contains the local user information and each line in this file represents login information for one user. You can either use cat
or less
command to open the file.
less /etc/passwd
There are 7 fields in each line delimited by the colons and has the below information:
- The User name
- An encrypted password. The
x
means that password is stored in/etc/shadow
file - The user ID that is UID
- User’s group ID number, or GID
- Full name of the user (GECOS)
- User home directory
- Login-shell that defaults to
/bin/bash
2) Next, you can display only the username by using either awk
or cut
command. It will print only the first field having the username:
awk -F: '{ print $1}' /etc/passwd
cut -d: -f1 /etc/passwd
List all Users using getent Command
1) The getent
command displays entries from the databases. The configuration is done in /etc/nsswitch.conf
file including the passwd
database, useful for querying a list of all users. Use the following command:
getent passwd
The output is similar to the content of /etc/passwd
file. Also, if you are using LDAP for user authentication, then the getent
will display all Linux users from both /etc/passwd
file and LDAP databases.
You can even use awk
or cut
, it will print only the first field containing the username:
getent passwd | awk -F: '{ print $1}'
getent passwd | cut -d: -f1
Checking if a User Exists in the Linux system
1) We have the list of all the users and now if you want to find a particular user in this list, you can use the grep
command to filter the user. For example, if you want to find John in the list you got, run the following command:
getent passwd | grep John
2) If there is no output, it means that the user doesn't exist, otherwise you will get the login information of the said user. You can find the user using the following command as well:
getent passwd jack
If the user exists, it will display the user’s login information.
3) Next, If you want to find the number of user accounts on your system, use the following command:
getent passwd | wc -l
Output
15
You will see from the above output, my Linux system has 15 user accounts.
System and Regular Users
1) Technically, there is no real difference between both system and regular users. System users are created when installing OS and other new packages. In few cases, you will be able to create a system user that will be used by some applications.
Normal users are created by root or a user with sudo privileges. A normal user has a real login shell and a home directory.
Every user has a numeric User ID known as UID. If it is not allocated at the time of user creation with useradd
command, it will be automatically selected from /etc/login.defs
the file based on the UID_MIN
and UID_MIN
values.
Use the following command to check the UID_MIN
and UID_MIN
values on your system:
grep -E '^UID_MIN|^UID_MAX' /etc/login.defs
Output
UID_MIN 1000
UID_MAX 60000
It is clear from the above output that all regular users should have a UID between 1000 and, 60000.
2) Below command will list all normal users in your Linux system:
getent passwd {1000..60000}
Output
vagrant:x:4500:4500:vagrant,,,:/home/vagrant:/bin/bash
john:x:1001:1001:,,,:/home/john:/bin/bash
molly:x:1002:1002:Molly Harper,,,:/home/molly:/bin/bash
peter:x:1003:1003:Peter Jane,,,:/home/peter:/usr/sbin/nologin
Your system UID_MIN
and UID_MAX
values can be different, so the more generic version of the above command will be:
eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)}
3) Then, if you want to print only the usernames, you need to pipe the output to the cut
command:
eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)} | cut -d: -f1
FAQs to List Users on Linux
Can I filter the output to display only usernames?
Yes, you can use the command awk -F: '{print $1}' /etc/passwd
to fetch and display only the usernames.
What information is displayed for each user in the /etc/passwd file?
Each line in the file represents a user and contains fields such as username, UID, GID, full name, home directory, and login shell.
How can I find the number of user accounts on my Linux system?
You can use the command wc -l /etc/passwd
to count the number of user accounts in the /etc/passwd file.
How do I list only the currently logged-in users?
The who
command displays information about currently logged-in users, including their usernames and login times.
Can I display additional details like the user's group information?
Yes, you can use the command getent passwd
to obtain a more comprehensive list of user account details, including group information.
Is it possible to sort the list of users alphabetically?
Yes, you can use the command cat /etc/passwd | sort
to sort the list of users alphabetically based on usernames.
Can I search for a specific user in the list?
Yes, you can use the grep
command along with cat /etc/passwd
to search for a specific user in the list using their username.
Conclusion
We hope this detailed tutorial helped you to List Users on Linux. If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.