How to List Users in Linux [With Examples]

April 12th, 2024
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 and more 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 ).

list-users-in-linux-using-cat-command

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

list-specific-user-in-linux-using-cat-command

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 

list-users-in-linux-using-less-command

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 

list-users-in-linux-using-more-command

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.

list-users-in-linux-using-getent-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

list-users-in-linux-using-getent-command

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.

list-minimum-and-maximum-uids-in-linux

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.

list-specific-user-in-linux-using-getent-command

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}

list-a-range-of-users-in-linux-using-getent-command

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

list-users-in-linux-using-awk-command

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.

Winnie is a seasoned Linux Systems administrator, currently specializing in writing technical Linux tutorials. With over seven years of experience in deploying and working with major Linux distributions such as Ubuntu, Debian, RHEL, OpenSUSE, and ArchLinux, she has written detailed and well-written "How to" Linux guides and tutorials. Winnie holds a Bachelor's Degree in Computer Science from Masinde Muliro University, Kenya and resides in Nairobi, Kenya. She is an expert in authoring Linux and DevOps topics involving Docker, Ansible, and Kubernetes. She currently works as a freelance technical writer and consultant. In her previous roles, she worked in the capacity of an IT support specialist and Linux administrator. Her key roles included offering level 1 and 2 support to both in-house and remote staff and managing and monitoring Linux servers.

Cloud VPS - Cheaper Each Month

Start with $9.99 and pay $0.5 less until your price reaches $6 / month.

We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: 372cf894.659