How to get or list all the users in Linux?

July 28, 2021 - 3 min read

To list all the users, we can view all the users stored inside of the /etc/passwd file using the cut command. The /etc/passwd stores all the login information of all the users in Linux.

TL;DR

# List all users in Linux
cut -d : -f 1 /etc/passwd

# OUTPUT
# ------
#
# root
# daemon
# _uucp
# ...
# ...
# ...

The /etc/passwd is the file in Linux, which stores the information regarding all the users. It is used more while a user is logging into the Linux system.

If we try to view the contents of the /etc/passwd file usign the cat /etc/passwd command, we would get an output something like this,

# Example of contents inside the /etc/passwd file
root:*:0:0:System Administrator:/var/root:/bin/sh
_mcxalr:*:54:54:MCX AppLaunch:/var/empty:/usr/bin/false
_appleevents:*:55:55:AppleEvents Daemon:/var/empty:/usr/bin/false
.....
.....
.....

This may look daunting at first 🤯, but the information we need is only the names of all the users in Linux. If we look closely at the second line in the above output which is,

root:*:0:0:System Administrator:/var/root:/bin/sh
# "root" is one of the users in this Linux system

The name of a single user is defined before the first occurrence of the : (colon character), which is the root. So root is a single user in the system. Like that, every single line inside the /etc/passwd file has the information of a single user in the Linux system.

Now instead of trying to get the output in an unreadable format, let's make this entire thing into a human-readable format using the cut command where we will get only the name of the users in Linux.

To do that first, we can use the cut command followed by the delimiter flag (-d) then pass the delimiter which is the colon character (:) then the field selection flag (-f) then selecting the first field of 1 and then the name of the file which is the /etc/passwd file.

So in the end, the cut command will look like this,

# List all users in Linux
cut -d : -f 1 /etc/passwd

and the output will be in a human-readable format only showing the users in the Linux system.

It will look like this,


# Output of the above command
# ---------------------------
#
# root
# daemon
# _uucp
# ...
# ...
# ...

See the execution of the above command live in repl.it.

That's all 😃!

Feel free to share if you found this useful 😃.