How to List Users in Bash [2 Easy Ways]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

To list users in Bash utilize the /etc/passwd file. This file contains a lot of information about the users. You can find a list of normal users, system users, user’s recent activity and many more by searching over the file. To extract information from /etc/passwd file you can use the read or getent command. The getent command has a leverage for this purpose as it is far more useful than listing the users only. The theme of this article is to list users in Bash and determine what kind of users they are by reading and extracting info from the passwd file.

System User and General User

  • System users are known as privileged users. These are accounts on the system with elevated privileges. These users have more extensive control over the system and can perform tasks that regular users cannot. They have access to critical system files, configuration settings, and administrative tools. In Unix-based systems like Linux, the root user is the superuser with complete control over the system
  • A general user is referred to as a regular user. General users do not have administrative privileges like the root user of the Unix system. General users can perform activities, such as running applications, accessing personal files, and using the system’s basic features.

2 Ways of Getting a List of Users in Bash

You can read the /etc/passwd file or can directly use the getent command to retrieve information about users. Read the methods below to know more about these.

You can read the Comparative Analysis of Methods to find one that suits you.

Method 01: How to Get a List of Users by Reading the “passwd” File

The passwd file is located in the etc folder of the system. This file contains information on users along with their names. The following script is designed to display information about current system users.

To get a list of users by reading the passed file, use the following script:

#!/bin/bash

while IFS=: read -r username _ _ _ _ _ _
do
echo "$username"
done < /etc/passwd
EXPLANATION

The provided Bash script uses a while loop to read each line of the /etc/passwd file. The IFS=: sets the Internal Field Separator to a colon (:), indicating that each field in the line should be separated by colons. The read -r username _ _ _ _ _ _ command reads the first field from each line and ignores the rest of the fields. Inside the loop, the script prints the value of the username variable using the echo command, displaying one username per line. The input redirection < /etc/passwd ensures that the loop reads the /etc/passwd file as its input.

Executing the script to list users in BashOnce executed the script will print a whole list of users of the system. If you go down you can also see the currently logged-in username.Finding currently logged-in user from the list As you can see the currently logged-in username of my system is printed at the end of the list.

Method 02: Printing User List Using the “getent” Command

The getent command is another built-in command to get the list of users in the system. Here I am providing a Bash script to print all the users of the system.

To print the user list using the getent command, go through the below script:

#!/bin/bash

# Retrieve the list of users using getent command and store it in a variable
user_list=$(getent passwd | cut -d: -f1)
# Print the list of users
echo "List of users:"
echo "$user_list"
EXPLANATION

The given Bash script uses the getent command to retrieve user information and then pipes the output to the cut command, which extracts only the usernames from each line. The list of usernames is stored in the user_list variable. Finally, the script prints the list of usernames with using the echo command.

getent command to list users in BashOnce executed the script will print all the users including system users and general users as you can the above image.

Comparative Analysis of Methods of Listing Users in Bash

Here is a comparative analysis between the above-discussed methods. Take a look at the analysis to determine which method suits your needs:

Method Advantage Disadvantage
/etc/passwd file
  • Can read all the fields of the passwd file
  • Not versatile like the getent command.
getent command
  • Can work on various databases.
  • May need to install it in the system

I think the best approach here is to use the getent command as it can extract more information about the user other than the username only. Moreover, the getent is straightforward and doesn’t need to extract any field.

Each Field of passwd File

Let’s talk more about the entire passwd file instead of just the first field of each line only. Each line of the passwd file has seven different fields separated by a colon. For example, consider one of the outputs of the getent passwd command:

 Meaning of each field of passwd file - Copy

EXPLANATION

man – The user name or login name.

  • x – The second field is the password field, which typically contains an x to indicate that the encrypted password is stored in the /etc/shadow file. The actual password hash is stored in the shadow file, which is more secure and accessible only to privileged users. If this field is empty it means the user or application is not password protected.
  • 6 – This represents the user’s unique numerical identifier (UID). Each user on the system has a unique UID, which helps the system identify and manage users. Typically system users UID is within the range of 100.
  • 12 – The fourth field refers to the primary group’s numerical identifier (GID) to which the user belongs. The primary group is the default group associated with the user. The group information is typically stored in the /etc/group file.
  • man –The fifth field contains additional information about the user, such as the user’s full name and other descriptions. In case of application, it contains details of the application. Here this field simply contains the same value as the username (man).
  • /var/cache/man – This is the user’s home directory. Here man’s home directory is /var/cache/man.
  • /usr/sbin/nologin – This refers to the login shell for the user, which is the command-line interpreter or program that is run when the user logs into the system. In this case, the user man has /usr/sbin/nologin as the login shell, which means they are not allowed to log in interactively. This is often used for system accounts or users who do not need shell access.

Counting System and General User

You can easily identify general users and system users with basic knowledge of Bash scripting. In the script below, I am going to count different types of users currently in the system based on the UID range.

UIDs below 1000 are typically reserved for system users, while those 1000 and above are for regular user accounts. Run the following two commands to find the UID range for the system and regular user:

grep UID_MIN /etc/login.defs
grep UID_MAX /etc/login.defs

UID range for system and general user

To count the number of system and general users in Bash, please run the following Bash script:

#!/bin/bash

# Number of total user
total_user=$(wc -l < /etc/passwd)
echo "Number of total users in the system: $total_user"

# Loop through all users and check if they have superuser privileges
regular_user=0

while IFS=: read -r username _ uid _ _ _ _; do
if (( uid >= 1000 )); then
regular_user=$((regular_user + 1))
fi
done < /etc/passwd

system_user=$((total_user - regular_user))
echo "Number of regular users in the system: $regular_user"
echo "Number of system users in the system: $system_user"
EXPLANATION

This Bash script begins by using the wc command to count the number of lines in the /etc/passwd file, which corresponds to the total number of users on the system. The result is stored in the total_user variable.

The script then utilizes a while loop to read each line of the /etc/passwd file, extracting the username and UID fields. It checks if the UID is greater than or equal to 1000, It increments the regular_user counter for each regular user encountered. After the loop completes, it calculates the number of system users by subtracting regular_user from total_user, and both counts are displayed using the echo command.

Counting different types of user in the systemHere the script shows the number of total users in the system is 48. Among them, 2 users are regular users and the rest of them are system users.

Checking the Existence of a User

Using the getent command you can check whether a user with a specific name exists in the system:Checking existence of user in the system Here, the user laku exists in the system hence getent passwd laku shows the output. However, getent passwd alina doesn’t give any output because user alina doesn’t exist in the system.

You can also list down all the group users using the following command:

getent group | cut -d: -f1

List of group users in Bash

Conclusion

In conclusion, Getting a list of users in Bash is an important aspect of system administration and security. Knowing all present users of a system allows administrators to have better control over access and permissions. Hope now you can easily get all the users and groups with a simple Bash script.

People Also Ask

What does users command do in Linux?

The users command only prints the currently logged-in username in Linux. It doesn’t print the whole list of users of the system.

What is the fourth field of the /etc/passwd file?

The fourth field of the /etc/passwd file is for the Group ID, known as GID. The group ID, mainly a number determines the primary group of a user.

How to I list users of a specific group in Linux?

To print all the users of a specific group name, let’s say, group_name, the getent group group_name | cut -d: -f4 command can be used. Just replace the group_name with your desired group name.

How can I switch user in Linux?

To switch between users in Linux, use the su command. In order to switch to a specific user, you simply need to provide the desired username after the su command as a command argument.

Related Articles


<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial

Rate this post
Md Zahidul Islam Laku

Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio

Leave a Comment