How to List Users in Linux


Linux is a multi-user operating system. To keep the system functional and productive, proper user management is mandatory. For a system administrator, knowing all the users is necessary to manage users and permissions.

This guide demonstrates how to list users in Linux.

Listing users

The system stores the list of users in specific files and databases. We can access them using various tools. We can also filter the output to collect specific info.

List users from /etc/passwd
The /etc/passwd file is a plain-text-based database that contains info about all the users in the system. The file is owned by root with the file permission 644. For an in-depth explanation of file permissions, check out this guide on Linux file permission and ownership.

We can check the content of the file /etc/passwd to have a comprehensive list of all the users in the system.

$ cat /etc/passwd | sort | less

Each line in the file denotes a unique username and its associated info. The info is separated into seven fields, delimited by colons. Here’s a quick rundown of the fields.

  • Field 1: The username of the user.
  • Field 2: It describes if the user password is encrypted. If the value is “x,” it signifies that the password is stored at the text file “/etc/shadow.” It’s a system-protected file that requires sudo privilege to access.
  • Field 3: The UID (user ID) of the user.
  • Field 4: The GID (group ID) of the user.
  • Field 5: Full User name (GECOS).
  • Field 6: Home directory dedicated for the user.
  • Field 7: The user login shell. By default, this value would be set to “/bin/bash.”

If the additional info is not necessary at the moment, we can omit them in the output.

$ cat /etc/passwd | awk -F: ‘{ print $1}’ | sort

$ cat /etc/passwd | cut -d: -f1 | sort

Listing users using getent
The term “getent” is a short form for “get entries from the administrative database.” As it suggests, getent can work with various administrative databases. Check out all the supported administrative databases.

We’re interested in the “passwd” database as it contains info about all the users in the system. Check out the passwd database with getent.

The output is similar to the content of “/etc/passwd.” Note that this getent command will display users from both passwd and LDAP databases if the system is configured to use LDAP for user authentication. For more in-depth usage, check out this guide on Linux getent command.

We can remove all the additional info from the output, keeping the username only.

$ getent passwd | awk -F: ‘{ print $1}’ | sort

$ getent passwd | cut -d: -f1 | sort

Listing user of a group
In Linux, groups are organization units to organize and administer user accounts. It helps to manage the various system and file permissions easier.

To list all the users from a particular user group, we can use getent.

$ getent group

Using user list

We now know how to list all the users in the system. Here are a few scenarios to apply this knowledge.

Checking user existence
From the user list, we can check if a user exists in the Linux system. The getent tool can check if the user exists in the system.

$ getent passwd

Another (albeit not-so-good) method is to use grep. We can simply filter the list of users using grep. As each user has its own unique entry, it won’t generate any collisions.

$ getent passwd | grep

Number of user accounts
As we’ve seen so far, all methods report a unique user at each line of the output. By counting the line number, we can check how many users there are currently in the system.

To count the line number, we’ll use the wc tool. Pipe the getent output to the wc command.

Listing connected users
If multiple users logged in, we can check the list of connected users using the who command.

There are three different columns of information in the output.

  • Column 1: The connected username.
  • Column 2: The type of connection to the system.
  • Column 3: The starting time and date of the session.

Final thoughts

In this tutorial, we demonstrated how to list and filter the users in the system. It also showcases some of the potential usages of the user list. These methods work irrespective of what Linux distro you’re using.

User management is a big part of the Linux ecosystem. To learn more, check out this in-depth guide on how to list and manage users in Linux.

Happy computing!

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More