How to List All Users in Linux System

Todays OS have the capability to use multiple users, each one with their settings and custom configurations to make things easier for administrators and operators to work in together on the same system.

Linux systems is very strong on this matter as it allows multiple users to work at the same time on the system in an independent way. It can even allow a single user to open several sessions even from different locations in order to work on the system.

Have you ever wanted to list all users in your Linux system or to count the number of users in the system? What is the command to list users under Linux operating systems?

This tutorial will shows you how to list all users in Linux

1. Get a List of All Users using the /etc/passwd File

Local user information is stored in the /etc/passwd file. Each line in this file represents login information for one user. To open the file you can either use cat or less:

As you can see from the output above, each line has seven fields delimited by colons that contain the following information:

  • User name
  • Encrypted password (x means that the password is stored in the /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 want to display only the username you can use either awk or cut commands to print only the first field containing the username:

2. Get a List of all Users using the getent Command

The getent command displays entries from databases configured in /etc/nsswitch.conf file including the passwd database which we can use to query a list of all users.

To get a list of all Linux users type the following command:

As you can see the output is same as when displaying the content of the /etc/passwd file. If you are using LDAP for user authentication the getent will display all Linux users from both /etc/passwd file and LDAP database.

You can also use awk or cut to print only the first field containing the username:

3. Check if a username already exists in the system

Now that we know how to list all users, to check whether a user exists in our Linux box we can simply filter the users’ list by piping the list to the grep command.

For example to find out if a user with name emma exists in our Linux system we can use the following command:

If the user exists the command above will print the user’s login information. If there is no output that means the user doesn’t exist.

We can also check whether a user exists without using the grep command as shown below:

Same as before, if the user exists the command will display the user’s login information.

If you want to find out how many users accounts you have on your system, pipe the getent passwd output to the wc command:

As you can see from the output above my Linux system has 26 user accounts.

4. System and Normal Users

There is no real technical difference between the system and regular (normal) users. Typically system users are created when installing the OS and new packages. In some cases, you can create a system user that will be used by some application.

Normal users are the users created by the root or another user with sudo privileges. Usually, a normal user has a real login shell and a home directory.

Each user has a numeric user ID called UID. If not specified when creating a new user with the useradd command, the UID will be automatically selected from the /etc/login.defs file depending on the UID_MIN and UID_MIN values.

To check the UID_MIN and UID_MIN values on your system you can use the following command:

From the output above, we can see that all normal users should have a UID between 1000 and 60000. Knowing the minimal and maximal value allow us to query a list of all normal users in our system.

The command below will list all normal users in our Linux system:

Your system UID_MIN and UID_MIN values may be different so the more generic version of the command above would be:

If you want to print only the usernames just pipe the output to the cut command:

5. List all the connected users

If you want to know what users are currently logged into your system, then you need to perform a simple ‘who’ on your command line and this will immediately list current usernames with an active session to your system

In this case, the listing will give you not only the list of usernames connected but also how they are connected, since when they are connected and from where they are connected.

The very first column will tell you what username is it.

The second column will give you what type of connection it is: if it’s represented with a “:X” where X is a number, it means it is using a Graphical User Interface (GUI) or Desktop session such as Gnome, XDE, etc; if it says “pts/X” where X is a number, it means it’s a connection made through SSH protocol (command line).

The third column will tell you since when this session has been connected to the server (date and time).

The fourth and last column will give you the location from where it’s connected, if remote it will display the IP from where the connection is made if local (like the GUI) it will display “(:X)” where X is the number of the session in this case and will match the number in the second column for that row.

Congratulations! In this guide, you learned how to list and filter users in your Linux system and what are the main differences between system and normal Linux users.

This tutorial apply for any Linux distribution, including the latest Ubuntu, CentOS, Linux Mint and Debian.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *