How to List Users in Bash [2 Easy Ways]

The etc/passwd file contains a lot of information about users in Unix-like operating system. One can find a list of normal users, system users, user’s recent activity and many more by searching over the etc/passwd file. In this article, I’ll demonstrate how to list users in Bash and determine what kind of users they are. In the process of doing so, I will use read, cut and many other basic Bash commands that you might already know.

Key Takeaways

  • Finding a list of users.
  • Identifying system users and normal users.
  • User’s most recent action on the system.

Free Downloads

System User and General User

  • System User

System users are also 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

  • General User

A general user also referred to as a regular user. General users do not have administrative privileges like the root user of 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

One can read the /etc/passwd file or can directly use the getent command to retrieve the 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 of users along with their names. The following script is designed to display information about current system users.

Steps to Follow >

❶ At first, launch an Ubuntu Terminal.

❷ Write the following command to open a file named user.sh in the build-in nano editor:

nano user.sh
EXPLANATION
  • nano: Opens a file in the Nano text editor.
  • user.sh: Name of the file.
Creating user.sh file in nano❸ Copy the following scripts and paste them into nano. Press CTRL+O and ENTER to save the file; CTRL+X to exit. Alternatively, copy the following script. Paste the script in a text editor and save it as .sh file.

Script (user.sh) >

#!/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.

❹ Use the following two commands to make both file executable:

chmod u+x user.sh
EXPLANATION
  • chmod: Changes permissions.
  • u+x: Giving the owner executing permission.
  • user.sh: Name of the script.
Changing permission of Bash script

❺ Run the user.sh script by the following command:

./user.sh

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 listAs 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.

You can follow the Steps of Method 1 to learn about creating and saving shell scripts.

Script (getent.sh) >

#!/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.

Run the getent.sh script by the following command:

./getent.sh

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

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

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 in the system

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

Each Field of passwd File

Let’s talk more about the passwd file instead of 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

One 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 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 system and regular user.

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

UID range for system and general user

You can follow the Steps of Method 1 to learn about creating and saving shell scripts.

Script (count.sh) >

#!/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 echo command.

Run the count.sh script by the following command:

./count.sh

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 user rest of them are system users.

Checking the Existence of a User

Using the getent command one can check whether a user with a specific name exists in the system.Checking existence of user in the systemHere the user laku exist 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. It doesn’t print the whole list users of the system.
How to I list users of a specific group in Linux?
getent group group_name | cut -d: -f4” command can be used to print all the users of group_name. Replace the group_name with the desired group name.
How can I switch user in Linux?
Utilize the su command to switch user in Linux. To switch to a specific user, you simply need to provide the desired username after the su command.

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