How to list all users and groups on a Linux system
Let’s find out how to list all the users and groups on a Linux system. We will also cover how to find out which users are currently logged on to the system, and how to distinguish between system users and normal users.
Listing all users
There are a few different ways to list all the users on a Linux system. The most common way is to use the cat command to read the /etc/passwd file. This file contains information about all the users on the system, including their username, a password placeholder (x, because the real hash lives in /etc/shadow), user ID (UID), group ID (GID), full name (the GECOS field), home directory, and login shell.
Here is an example of how to use the cat command to list all the users on a Linux system:
cat /etc/passwdThis will output a list of all the users on the system, along with their associated information.
Another way to list all the users on a Linux system is to use the getent command. The getent command can be used to retrieve information from a variety of databases, including the /etc/passwd file.
Here is an example of how to use the getent command to list all the users on a Linux system:
getent passwdThis will output a list of all the users on the system, along with their associated information.
Listing all groups
There are a few different ways to list all the groups on a Linux system. The most common way is to use the cat command to read the /etc/group file. This file contains information about all the groups on the system, including their group name, group ID (GID), and a list of users who belong to that group.
Here is an example of how to use the cat command to list all the groups on a Linux system:
cat /etc/groupThis will output a list of all the groups on the system, along with their associated information.
Another way to list all the groups on a Linux system is to use the getent command. The getent command can be used to retrieve information from a variety of databases, including the /etc/group file.
Here is an example of how to use the getent command to list all the groups on a Linux system:
getent groupThis will output a list of all the groups on the system, along with their associated information.
Finding out which users are currently logged on
There are a few different ways to find out which users are currently logged on to a Linux system. The most common way is to use the who command. The who command will output a list of all the users who are currently logged on to the system, along with their username, terminal name, and the time they logged on.
Here is an example of how to use the who command to find out which users are currently logged on to a Linux system:
whoThis will output a list of all the users who are currently logged on to the system, along with their associated information.
Another way to find out which users are currently logged on to a Linux system is to use the w command. The w command will output a list of all the users who are currently logged on to the system, along with their username, terminal name, the time they logged on, and the last time they were active.
Here is an example of how to use the w command to find out which users are currently logged on to a Linux system:
wThis will output a list of all the users who are currently logged on to the system, along with their associated information.
Distinguishing between system users and normal users
System users are users who are created for system tasks, such as running daemons or managing files. Normal users are users who are created for human users to log in to the system.
There are a few ways to distinguish between system users and normal users. One way is to look at the user ID (UID) of the user. System users typically have a UID below 1000 (with UID 0 reserved for root), while normal users typically have a UID of 1000 or more. The exact cutoffs are UID_MIN and SYS_UID_MAX in /etc/login.defs. The special nobody user sits at 65534.
Another way is to look at the login shell. System users typically have /usr/sbin/nologin or /bin/false, while normal users have a real shell such as /bin/bash. On Debian and Ubuntu, each new normal user also gets a private group with the same name, not the users group.
I hope this blog post has been helpful. If you have any questions, please feel free to leave a comment below.
Examples
Here are some examples of how to use the commands we have discussed in this blog post:
- To list all the users on a Linux system:
cat /etc/passwd- To list all the groups on a Linux system:
cat /etc/group- To find out which users are currently logged on to a Linux system:
who- To list only normal users (UID 1000 and up, excluding
nobody):
awk -F: '$3 >= 1000 && $3 < 65534 {print $1}' /etc/passwd- To list all the users who belong to the
usersgroup (group membership lives in/etc/group, not/etc/passwd):
getent group users