Introduction

Linux OS is unique because of its multiuser characteristic. It allows multiple users on one system, at the same time. Tracking all users is essential.

In this article, you will learn multiple commands to list all Linux users along with their login information. These commands should work on Centos/Ubuntu/Arch and other Linux distros as well.

Prerequisites

  • A Linux distribution installed and running
  • A user with sudo privileges
  • Access to a terminal/command line
  • The apt tool, pre-loaded in Ubuntu and other Debian-based distros.

List All Linux Users, 2 Options

List all users in Linux with the /etc/passwd File

Details of local users can be found in the /etc/passwd file. Every line contained in the file contains the information of one user.

There are two options.

Open the etc/passwd file by typing the command:

cat etc/passwd

Alternatively, you can use the less command:

less etc/passwd

List all Users with the getent Command

Database entries configured in the /etc/nsswitch.conf file include the passwd database with all the usernames and login information.

To extract this data, use the command: getent passwd

Both Option 1 and Option 2 will display all the users and their login information.

a list of all Linux users output

Each line represents one user and has seven (7) fields.

The fields are separated by : (colons) and each line includes the following information:

1. Username
2. The encrypted password (represented by x, located in the /etc/shadow file)
3. User ID number (known as UID)
4. User group ID (known as GID)
5. User full name
6. User home directory
7. The login shell (by default set to bin/bash)

output user fields

How to only list Linux user names

In case you do not need all the information related to each user, you can list only the usernames on the system. There are two (2) ways to see just the first field (the username) of each user.

Using the awk or cut command

To list usernames only, you can use either of the following two (2) commands:

awk –F: ‘{ print $1}’ /etc/passwd
cut –d: –f1 /etc/passwd

Option 2 – Using the getent command with awk and cut

To read and display the username without any additional information using the getent command, run the following command:

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

Alternatively, use the command:

getent passwd | –d: –f1

How to search for existing Linux users

The getent command also allows you to check whether a user is present on the system.

Any of the following two (2) commands will provide you with that information:

getent passwd | grep username
getent passwd username

If the user exists, it will display login information. On the other hand, if there is no such user, there will be no output.

For example, in the image below, the query displays whether a user named example exists. The output proves that such a user exists.

getent command to search for existing users

System User vs Normal User

The difference between Normal Users and System Users is slight. On the one hand, a system user is the one that creates normal users. Therefore, in this instance, the system user is the root. This user is created when you first install the Linux operating system and new packages. Additionally, you can create system users for particular applications.

On the other hand, normal users are all users that the root (or a user with sudo privileges) creates. Each normal and system user has a real login shell, home directory, as well as a user ID (UID) number. The user ID number is given automatically in the range between the minimum and maximum values.

How to check UID_MIN and UID_MAX

If you want to check what the UID range for normal users is, use the following command:

grep –E”^UID_MIN|^UID_MAX” /etc/login.defs

The output shows that all normal users have a UID anywhere from 1000 (UID_MIN) to 6000 (UID_MAX).

checking the UID_MIN and UID_MAX


Note: Change the values in the command according to the minimum and maximum UID values for your system.


How to list Normal Users

With these numbers in mind, you ask for a list of all the users in that range with the command:

getent parrwd {1000..6000}

The query lists all the normal users, as seen in the image below. In this example, there are two normal users in the specified range.

listing normal users in linux example

Conclusion

After reading this guide, you should know how to list all Linux users, search for users, and find the number of Linux users.

Next, you can learn about the privileges and permissions different users have, as well as how to work with users, groups, and directories.