Home / Linux/Unix/BSD / Ubuntu list users

Ubuntu list users

In this tutorial, we’ll guide you through different ways to list users in Linux.

Being able to list users in Linux primarily serves the purpose of helping you improve how to manage your administration system and create better security between users.

Before we dive in, let’s take a look at the importance behind viewing a list of Linux users.

Why Viewing Your List of Users is Useful

Linux is most useful among technology companies and research development groups since these utilize a wide list of users. Team-based projects require a system to help manage user accounts and streamline productivity.

After all, managing and handling these groups is a day-to-day activity that requires efficient scheduling. If proper security parameters are not set, you could lose important data.

This is where learning how to list Linux users becomes essential to managing groups and team-based projects.

With the right knowledge, you can grant access to specific files and folders and adjust the permission settings for each user to secure your files, folders, and any data stored in your system.

Before we can get into managing users, we have to first learn how to view your list of users. Linux systems allow you to take control of Ubuntu’s features so you can control how each user interacts with the system.

Why does this matter? Because being able to see your list of users will also allow you to see who has permission to files and who has access to specific administrative controls.

The next sections to follow will discuss the commands used to list users in the Ubuntu server.

How to List Users

Listing users in the Ubuntu server will require the command line, which you can easily access through the terminal shell.

Listing users in Ubuntu can be found in the /etc/passwd file. The /etc/passwd file is where all your local user information is stored. You can view the list of users in the /etc/passwd file through two commands: less and cat.

less /etc/passwd 
or
cat /etc/passwd

Once you’ve typed the command, less /etc/passwd, you’ll be given the following output:

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin

Depending on the number of lines your Ubuntu server displays, this corresponds to the number of users in your system. From the example above, we would have 8 registered users in the system.

Looking at the first line, here is a brief definition of what each field means:

  • root – Refers to the login name of the user
  • x – Placeholder or encrypted password; this means the password is stored in a separate file
  • 0 – Refers to the user ID. Each user ID is unique per registered user, and in root’s case, it’s user ID is always 0
  • 0 – Refers to the group ID, wherein the group ID is also unique for every user
  • root – Refers to the comment field or a short description of the user
  • /root – Refers to the home or main directory of users.
  • /bin/bash – Refers to the user shell that users use to log in the system

You also have the option to list the name of the users solely by using the awk or cut command:

cut -d: -f1 /etc/passwd
or
awk -F: '{print $1}' /etc/passwd

Using the awk -F: ‘{print $1}’ /etc/passwd or the cut -d: -f1 /etc/passwd command should display the same results so either command will work appropriately for the system.

Using the Getent Command

The getent command allows you to display a list of entries configured in the /etc/nsswitch.conf file. Essentially, the getent command will also display similar results as the example shown above.

To view the list of users using getent, simply type the command:

getent passwd

You’ll see the same display as shown in the example above:

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin

Finally, you can also use the awk or cut command to view only the usernames:

getent passwd |cut -d: -f1
or
getent passwd | awk -F: '{print $1}'

You can also use the getent command to search for an existing Linux user in the system:

getent passwd username 

If the user exists in the system, then you’ll be prompted with the login information of the user. If the user is non-existent in the system, no output will be available.

Normal User Vs. System User

The system user is referred to as the root, which is the user that was created when you installed your Linux operating system. Normal users, on the other hand, are created by the root user.

Both types of users have their own home directory, login shell, and user ID number (UID).

The UID is automatically given from the /etc/login.defs file and ranges between the minimum and maximum values, also known as UID_MIN and UID_MAX respectively.  

To check the UID_MIN and UID_MAX values, type the command:

grep -E '^UID_MIN|^UID_MAX' /etc/login.defs

This will check the range of minimum and maximum UID values in the /etc/login.defs file where you’ll be prompted with the message:

UID_MIN                         1000
UID_MAX                         60000

The /etc/login.defs file shows us the values of normal users ranges between 1000 and 60000, which will allow us to run a command to query the list of normal users in the system.

To list all the normal users, simply type:

getent passwd {1000..60000}

If there are only 2 normal users found in the file or system, the command will display all the normal users and their related information.

Final Thoughts

We hope this tutorial was able to help you learn how to list and view users in Linux along with how to search for the number of users in the system.

For more questions, you can ask us in the comments section below!