How to List Users in Linux [With Examples]
User management is one of the principal tasks carried out by system administrators. It entails operations such as adding new users to the system, removing existing users, and modifying user attributes to mention just a few. In this tutorial you will learn how to list users in Linux using five different methods.
#How to list users in Linux?
You can use a few simple commands to retrieve a list of existing Linux users along with useful metadata like their usernames, UIDs, home directories, and more. Below, we'll show you five different ways to list users in Linux:
-
cat
command; -
less
andmore
command-line utilities; -
getent
command; - list regular and system users using the
grep
command; - and
awk
command.
Before we dive into specific examples of each, I'll briefly explain the different types of Linux users and understand the /etc/passwd file.
#Two types of Linux users
Before we delve into the ways to list users in Linux, I’ll briefly go through the different types of users, as there are different types. These users fall into two broad categories: regular and system users. Let’s check out the differences between the two.
#1. Regular users
Regular users are users created by the root or sudo user. These users have login capabilities and a home directory for storing personal files such as documents, pictures, music, and videos.
#2. System users
These users are automatically created by the system during installation of the operation system or software applications. They are used to run system services and applications. All users bear a unique user ID ( UID ) to identify them. UIDs for system users range from 0 (the root user ) to 999, whereas regular users typically have UIDs from 1000 onwards.
#What is /etc/passwd?
Before we look at how to list users in Linux, it’s crucial that we, first, discuss the /etc/passwd file. The /etc/passwd
file is a special colon-separated file that stores user account information. The information appears as follows from left to right.
- User name;
- Encrypted password;
- User ID number (UID);
- User's group ID number (GID);
- User's full name;
- User home directory;
- Login shell.
Here’s an entry from the /etc/passwd
file.
cherry:x:1000:1000:cherry:/home/cherry:/bin/bash
From the above output, the username is cherry
, the encrypted password is represented by an x
, the UID is 1000
, the GID is 1000
, the user’s full name is cherry
, the home directory is /home/cherry
and login shell /bin/bash
.
Now let’s switch gears and see various ways of listing Linux users and information contained in the /etc/passwd
file.
#Prerequisites
To get along with this guide, ensure that you have the following:
- A Linux system (any distribution will work perfectly fine);
- Access to the terminal or shell.
So, let’s dive right in and bring specific examples for each.
#List users in Linux: 5 ways
#1. List users in Linux using the cat command
One of the most straightforward ways of listing users in Linux is using the cat
command. The cat command reads a file sequentially and displays its content to standard output. The syntax for reading a file is as follows.
cat file_name
To list users in a Linux system, view the /etc/passwd
file using the cat
command line utility as shown.
cat /etc/passwd
The command prints out the entire file with information about all the users ( both regular and system ).
You can list a specific user using the grep
command. In the command below, the cat command lists the details of the user called cherry
. The -i
option ignores case sensitivity.
cat /etc/passwd | grep -i cherry
#2. List users in Linux using the less
and more
command-line utilities
The etc/passwd
file is quite a long file and can grow exponentially as more users are added to the system. While the cat
command does a splendid job of listing the users alongside their details, the less
and more
commands enable smooth file scrolling.
The less
command is extremely handy when viewing large files and you need to smoothly go line by line. The basic syntax of the less
utility takes the following form:
less file_name
``
You can use the `less` command to open the `/etc/passwd` file in a `less` environment. Thereafter, you can scroll up and down using the up and down arrow keys or `Page up` and `Page down` keys to navigate up and down a full page at a time.
```bash
less /etc/passwd
The more
command is in many ways similar to the less
command. However, unlike less
, more
only allows forward scrolling of the file, and not backward. To navigate to the next screen, just hit the spacebar key on your keyboard.
Like the less
command, you can view the /etc/passwd
file as shown.
more /etc/passwd
#3. List Linux users with getent
command
The other alternative to listing Linux users is using the getent
command. The command references the /etc/nsswitch.conf
file and displays the system database entries to standard output. The passwd
database is the file that is included by default.
You can list all the contents of the passwd
database using the getent
command as shown.
getent passwd
Notice how the output mirrors the one generated when viewing the /etc/passwd
file using the cat command.
Additionally, you can list the entry for a specific user by specifying the username at the end of the command. In the command below, we are displaying the entry for the user cherry
.
getent passwd cherry
#4. List regular and system users using the grep
command
In our introduction, we looked at the two broad categories of users in a Linux system: regular and system users. The UIDs for system users range from 0 (root user) to 999 while regular users have their UIDs from 1000 onwards.
You can have a glance at the UID range for regular users using the grep
command as follows. The information is referenced from the /etc/login.defs
file.
grep -E '^UID_MIN|^UID_MAX' /etc/login.defs
From the output, you can see that valid UIDs for regular users range from 1000
to 60000
. This implies that the lowest valid UID a regular user can have is 1000
and the highest is 60000
.
In addition, you can search a user based on their UID using the following syntax:
getent passwd UID
In the example below, we are searching for a regular user based on a UID of 1000
.
getent passwd 1000
From the output, we can see that the cherry
user is the regular user associated with UID 1000
.
Additionally, you can list a range of system users using the following syntax
getent passwd {[first-UID]..[last-UID]}
For example, to list a range of system users between 0 and 400, run the command:
getent passwd {0..400}
#5. List users in Linux with awk
command
The methods we have looked at so far list the users together with other user attributes such as the username, UID, GID, and path to the home directory, to mention a few. To list usernames only and omit the rest of the details, then the awk
command is the command-line utility to go for.
You can instruct awk
command to print the usernames only using the following command. The directive { print $1}
prints out the first field only for each entry.
awk -F':' '{ print $1}' /etc/passwd
Furthermore, you can combine awk
with less
for smooth scrolling of the results page by page as shown.
awk -F':' '{ print $1}' /etc/passwd | less
#Conclusion
That’s it for this guide. In this tutorial, you have learned how to list Linux users in five different ways.
Cloud VPS - Cheaper Each Month
Start with $9.99 and pay $0.5 less until your price reaches $6 / month.