How to List Users on Linux


All the users on Linux, including system users and normal users, are stored in the /etc/passwd file. The /etc/passwd file contains information about users such as usernames, UIDs, login shell, and home directory. Reading this file essentially lists all the users on Linux, as each line of the file represents a user.

As a Linux system administrator, user management is one of the crucial tasks. Listing all the users is crucial before carrying out any actions on them. In this guide, I will discuss various methods to list users on Linux. Before listing users, it is important to understand both system users and normal users.

Types of Users on Linux

On Linux, the users are categorized into two types:

  • System Users
  • Normal Users

System Users: The system users are created by the system when the operating system is installed. These users run the non-interactive processes, such as managing services and running background processes.

Normal Users: The normal users are created by root or other users with root access. The normal users interact with the system directory and perform operations like running applications, and manually managing services.

System Users Normal Users
Non-Interactive Interactive
Created by system Created by root or other normal users
UID ranges from 0-999 (It may vary depending upon system configuration) UID ranges from 1000-65534 (16-bit Integer)

 

The UID range for normal users is defined in the /etc/login.defs file.

You can extract the above information using the grep command.

grep -E ‘^UID_MIN|UID_MAX’ /etc/login.defs

List Users Using the cat Command

To list users on Linux, use the cat command to read the /etc/passwd file and print it in the standard output.

cat /etc/passwd

Each line in the above output signifies each user and contains fields separated by a colon.

username:password:UID:GID:/home/user:/bin/bash

The explanation of the above-mentioned fields is given below:

  • username: The name of the user
  • password: The hashed password
  • UID: The user’s unique identification
  • GID: The user’s group ID
  • /home/user: The user’s home directory path
  • /bin/bash: The user’s login shell

To get the user’s list with a line number, use the -n flag.

cat -n /etc/passwd

To list the details of a specific user.

cat /etc/passwd | grep sam

With the cat command, you can get an idea about users and the number of users, however, it is hard to identify the system users and normal users.

List Users using the compgen Command

The compgen command is a bash built-in command that is used for various purposes, such as listing all the commands, listing bash keywords, listing aliases, and listing usernames. The command needs an option as an argument to display the related information. For example, to list all the commands -c is used, and to list all aliases, -a is used.

To list all users using the compgen command, the -u option is used.

compgen -u

List Users Using the getent Command

The getent command is used to get the entries from the databases supported by Name Server Switch (NSS) libraries. These databases contain information about user’s accounts, hostnames, and services.

To list all the users, use the getent command with passwd.

getent passwd

To list users with respect to user IDs (UIDs), the getent command can be used with UID range.

getent passwd {1000..60000}

To list information about a specific user or to check whether a user exists or not, type the username after the above command or UID.

getent passwd [username/UID]

List Users Using the awk Command

The awk command allows executing awk programming in the shell. It is a powerful text-processing tool used to manipulate and extract text from text files.

To list usernames only, the awk command is used with the -F flag which indicates the field separator and print to print the output.

awk -F':' '{print $1}' /etc/passwd

The {print $1} is used to print the entries of the first field.

If you want to print the third field, which is UID, replace the {print $1} with {print $3}.

List Users Using the cut Command

The cut command is used to print the selected section of the file to stdout. It can also be employed to extract and list the users from the /etc/passwd file.

cut -d: -f1 /etc/passwd

In the above command -d indicates the delimiter which is the colon (:). The -f flag signifies the field which is first in this case.

Similarly, to get the UID, change the field from -f1 to -f3.

The methods mentioned above list all the users, whether they are system users or normal users. As discussed earlier, the system users have UID ranges from 0-999 and normal users from 1000-60000. Using this information can help us extract the normal users and system users.

In the next section, I will discuss a method to list normal and system users only with the combination of getent and awk/cut commands.

How to List System Users and Normal Users Only

List the normal users only use the getent command and pipe it to the cut command.

getent passwd {1001..60000} | cut -d: -f1

Similarly, the awk command can also be used in the place of cut command.

getent passwd {1001..60000} | awk -F':' '{print $1}'

To list all the system users, simply replace the UIDs in the above commands.

getent passwd {0..999} | cut -d: -f1

How to List Currently Logged-in User on Linux

The users command is used to list all the users that are logged in currently.

users

The users will be listed in alphabetical order. The who command can also be utilized for the same purpose.

who

If a user is connected using two login sessions, then it will appear twice.

Conclusion

User information whether they are system users or normal users is stored in a /etc/passwd file. To retrieve a list of users on Linux, utilize the cat command to read the contents of the /etc/passwd file. In the same way, the getent, compgen, awk, and cut commands are also used to list the users in the terminal. However, these commands list all the users, irrespective of their types. To list only normal users or system users, the UIDs can be used with the getent command in combination with cut or awk.

Print Friendly, PDF & Email
Categories