How to List Users on Linux System in 2024

How to List Users on Linux System

Are you looking for a way to list or count the number of users on your Linux system? By using the command on Linux, how to list users in Linux? There are a number of commands regarding the user, like creating a user, deleting a user, and listing logged-in users, just like these commands you can easily list users or count the number of users in the Linux system as well as check if a user exists in the Linux system by using the Linux command.

In this tutorial, you will learn how to list users in the Linux system using the Linux command. Also, check whether a user exists in the Linux system or not by using the Linux command.

Use the /etc/passwd file to get a list of all users

/etc/passwd file stores local user information. Each line of the file shows the login information of a user. By using cat or less command you can open the file:

less /etc/passwd
Use the /etc/passwd file to get a list of all users

Each line containing seven fields separated by colons has user information formatted like:

  • User name
  • Encrypted password (x means password stored in /etc/shadow file)
  • User ID number (UID)
  • User’s group ID number (GID)
  • Full name of the user (GECOS)
  • User home directory
  • Login shell (defaults to /bin/bash)

If you wish to list only the user name you can use ewk or cut commands to display only the first field that has the username of the user.

awk -F: '{ print $1}' /etc/passwd
cut -d: -f1 /etc/passwd
Output
root
daemon
bin
sys
sync
...
...
sshd
vagrant
jack
anne

Use the getent command to get a list of all users

The getent command shows the entries from the database stored in /etc/nsswitch.conf file, which includes the passwd database as well. It can be used to find a list of all users.

Use the following getent command to list all the users of the Linux system:

getent passwd
Use the getent command to get a list of all users

By using this command you will see the same output as the previous one showing the contents of the /etc/passwd file. If you are using the LDAP for user authentication then, getent command will show all Linux users from both /etc/passwd the file and LDAP database.

Like the previous one, you can use ewk or cut commands with getent to display only the first field that has the username of the user.

getent passwd | awk -F: '{ print $1}'
getent passwd | cut -d: -f1

Check if a user exists in the Linux system or not

From the upper part of the article, you can easily list all the users. In the list, you may manually search for the user you want to find, but if the list is too big it’ll be a lot of work. Linux has the command to check whether a user exists in the Linux system.

We can easily filter the user list by adding the grep command followed by the username. For example, if you want to find out the user name anne from the Linux system, use the following command:

getent passwd | grep anne
use grep command to check user existence

If the user exists in the Linux system then the command will show the information about the user. If there is no output shown, then the user doesn’t exist in the Linux system.

You can also use an alternative way of checking a user’s existence without the grep command:

getent passwd jack

This command will show the same result as above, if a user exists then it will show the user information otherwise no output will be shown.

Count the number of users in the Linux system

Now, if you only want to see the number of users exist in the Linux system, manually counting them one by one will be a hard job. By using Linux command you can easily see the number of users in the Linux system, use wc command with getent passwd to see the output:

getent passwd | wc -l
Output
33

As the example shows the Linux system contains 33 user accounts.

Difference between system users and Normal users

In general, the system users and the normal users (regular users) are quite the same. System users are created during the installation of Linux OS and new packages. You can create a system user that can be used by some applications.

Normal users or regular users are created by the root or the user with the sudo privileges. Generally, a normal user is given a real login shell and home directory.

Each user information contains a user ID called UID. UID is a numeric ID that can be specified using useradd command, if not provided then UID will be automatically generated from /etc/login.defs file that depends on the UID_MAX and UID_MIN values.

You can also check the UID_MAX and UID_MIN values stored on your system by using the following command:

grep -E '^UID_MIN|^UID_MAX' /etc/login.defs
Output
UID_MIN          1000
UID_MAX         60000

The example above shows the output of all the normal users should have a UID between 1000 and 60000. This information of minimal and maximal value lets us list all the normal users in the Linux system.

To get the list of all the normal users in the system you can use the following command:

getent passwd {1000..60000}
using UID_MAX and UID_MIN to get the list of all the normal users in the system
Output
vagrant:x:1000:1000:vagrant,,,:/home/vagrant:/bin/bash
jack:x:1001:1001:,,,:/home/jack:/bin/bash
anne:x:1002:1002:Anne Stone,,,:/home/anne:/bin/bash
patrick:x:1003:1003:Patrick Star,,,:/home/patrick:/usr/sbin/nologin

Different systems can have different UID_MIN and UID_MAX values, so a better version of the command would be:

eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)}

If you only wish to only display usernames then use 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

Conclusion

By following this tutorial you are able to learn how to list all the users, how to find a user, check the number of users in the Linux system, distinguish between normal users and system users, and learn commands of the Linux system.

These commands can also be applied to any Linux distribution, such as Ubuntu, CentOS, RHEL, Debian, and Linux Mint.

If you still face difficulty regarding this tutorial, feel free to comment below and let us know your feedback.

James

James

Hi, this is James, a tech specialist and a core member of the TrioTeam. I like to research and write content about various interesting things especially tech-related stuff, and also have an interest in philosophical discussion. I have been writing solutions for technical problems, how-to tutorials, technology reviews, tools and websites, and so on for TrioGuide.

We will be happy to hear your thoughts

Leave a reply

TrioGuide
Logo