Introduction
Linux OS is unique because of its multi-user characteristic allowing multiple users on one system, at the same time. However, tracking all users is essential.
In this article, earn multiple commands to list all Linux users along with their login information. These commands work on CentOS, Ubuntu, Arch Linux, and other Linux distributions as well.
Prerequisites
- A Linux distribution installed and running
- A user with sudo privileges
- Access to a terminal/command line
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 Linux 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.
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)
How to Only List Linux Usernames
In case you don’t need all the information related to each user, you can list only the usernames on the system. There are two ways to see just the first field (the username) of each user.
Option 1: 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 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.
System User vs Normal User
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. 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).
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.
Conclusion
After reading this guide, you should know how to list all Linux users, search for users, and find the number of Linux users in any Linux distribution ( Ubuntu, CentOS, RHEL, Debian, and Mint).
Next, you can learn about Linux file permissions, as well as how to list scheduled cron jobs for specific users.
Next you should also read
How to Add User to a Group in Linux
November 6, 2019
By following the steps in this tutorial you will learn how to add a user to a group in Linux. Read the easy…
How to Create a File in Linux Using Terminal/Command Line
June 27, 2019
Creating a file in Linux might seem straightforward, but there are some surprising and clever techniques. In…
How to Use mkdir Command to Make or Create a Linux Directory
April 8, 2019
The mkdir command in Linux allows users to create or make new directories. mkdir stands for “make directory.”…
How To Use grep Command In Linux/UNIX
March 28, 2019
This guide details the most useful grep commands for Linux / Unix systems. After going through all the…
Author
Sofija Simic
Sofija Simic is an aspiring Technical Writer at phoenixNAP. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.