How to List Users in Ubuntu Command Line

Learn various ways of listing users in Ubuntu Linux. You'll also learn the difference between the system and regular users.

As a sysadmin, you'll encounter scenarios where you need to list the users present on your system.

Perhaps you want to remove a user or add it to the sudoer list or modify its groups, there can be several reasons you would want to check users.

And there are several ways to list users in Ubuntu. Let me share some of them with you.

List all users with /etc/passwd file

The most common and reliable way is to look at the content of /etc/passwd file. This file keeps the details of the user accounts.

cat /etc/passwd

You'll probably see a huge output like this:

List users in Ubuntu by looking at the contents of /etc/passwd file

Each row represents a user and the fields separated by the colon (:) has the following meaning:

  • User name
  • Password, x means that a password is set for the user
  • User ID (UID)
  • User's group ID (GID)
  • Full name, room number, phone number etc (optional)
  • Home directory of the user
  • Default login shell for the user
Understanding the content of /etc/passwd file in Linux

So many users? Which ones are real?

Surprised and overwhelmed to see so many users? You don't remember creating them all, do you? That's because you didn't create all of them.

Most of these users are created automatically with the Linux system itself. Some of them are created automatically or as part of the configuration when you install specialized software like postfix, MySQL etc.

These are system users. You'll notice that most of them have nologin set for the default login shell. This indicates that these system users cannot be used to login to the system. They will be used from within the system but you cannot do a ssh [email protected].

The users that you create to be used by other human users are called normal or regular users.

How do you distinguish between the system users and normal users? You use the UID.

System users have UID less than the UID_MIN. Regular users have UID between UID_MIN and UID_MAX. You can get these value with this command:

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

In some Linux systems, UID_MIN is 500. But in Ubuntu it is 1000.

[email protected]:~# grep -E '^UID_MIN|^UID_MAX' /etc/login.defs
UID_MIN			 1000
UID_MAX			60000

In other words, if you see the UID is 1000 or more, it is a normal user. Root user always has 0 UID and 0 GID.

DigitalOcean – The developer cloud
Helping millions of developers easily build, test, manage, and scale applications of any size – faster than ever before.
Get started on DigitalOcean with a $100, 60-day credit for new users.

Only list user names, not other details in /etc/passwd

You can parse the output of the /etc/passwd file to cut or awk command to only display the first column which shows the user name.

Use the cut command to use the : as column separator and then display the first column.

cat /etc/passwd | cut -d: -f1

You can do the same with the awk command:

cat /etc/passwd | awk -F: '{print $1}'

Both commands will give you output like this:

root
daemon
bin
sys
sync
games
man
lp

Use getent command to list users

The getent command queries the configuration files located at /etc/nsswitch.conf. The /etc/passwd file is one of them.

So, you query it with getent like this:

getent passwd

This will give you a result similar to what you saw in the contnet of /etc/passwd file:

[email protected]:~# getent passwd
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
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin

You can list only the regular users the same way:

getent passwd | cut -d: -f1

Use compgen to get the list of users

You can use the compgen command with option -u and list only the users present on the system without any additional information.

compgen -u

This lists all users, system and regular, without additional details.

root
daemon
bin
sys
sync
games
man
lp

Check if a user exists on the system

You know how to list the users in Ubuntu. What if you just want to know if a user exists or not?

While there is no dedicated command for that, you can filter the user list with the grep command like this:

getent passwd : grep user_name

If you see a result, the user exists.

[email protected]:~# getent passwd | grep prakash
prakash:x:1002:1002:,,,:/home/prakash:/bin/bash

If you don't see it, it doesn't.

[email protected]:~# getent passwd | grep random
[email protected]:~# 

List normal users only (for scripting)

If you just want to see what other regular users have been added to your Ubuntu system, you can use any of the above commands and look at the users with UID of more than 1,000.

This is good enough for day-to-day, manual use. But things will be different if you want to use this information in a script.

Say you plan to just list the names of the regular users on the system and use it in a script for some specific purpose.

In that case, you could do something like this:

eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)} | cut -d: -f1

The eval command executes the arguments as a shell command. The awk commands get the UID_MIN and UID_MAX and then use them with getent passwd to only fetch the rows with UIDs in 1000 to 60,000 (normal users). The cut command shows only the first column, which is the username.

List currently logged in users

If you are using an Ubuntu server with multiple users, you can check which users are currently logged in.

There are multiple ways to do that. The most common is the who command:

who

It will show additional details like the time of the last login and the IP address from where it was accessed.

[email protected]:~# who
root     pts/0        2022-09-07 02:23 (103.211.12.162)

If you just want to display the currently logged-in users without additional details, use the users command:

users

As you can see, it just gives the names of the currently logged-in users:

[email protected]:~# users
root

In the end ...

You saw numerous ways of listing users in the Ubuntu command line. You got to learn about the structure of the /etc/passwd command and the difference between system and normal users. You also learned to check if the user exists in the system and display the currently logged in users.

That's a pretty good addition to your Linux knowledge. Feel free to leave your questions and suggestions in the comments.