How to List All Groups in Linux

DevOps code

As a Linux administrator or DevOps engineer, it’s important to understand how Linux groups work as each folder, directory, or file is linked to specific users and groups. In this tutorial, I’ll show you how to list all groups on a Linux machine, how to list all users, and how to see the groups they belong to.

How to list all groups on Linux

There are multiple commands you can use to find all the groups that are present on Linux. But first of all, let’s start with an explainer about Linux groups.

What are groups on Linux?

Groups on Linux-based operating systems are used to assign a set of privileges to a group of users. There are mainly two types of groups in the Linux distribution operating systems.

Whenever a Linux user creates a file or directory, then the files within it are allocated to a primary group, which has the same name as the one of the current user. Every user has at least one primary group associated with them. Whenever a new user is created, it is allocated within a primary group. Apart from their primary group, Linux users can also belong to other groups known as secondary groups.

Listing all groups with the groups command

By using the groups command, we will see all the primary groups that are present on a Linux machine.

groups
The groups command show all primary groups on Linux
The groups command shows all primary groups on Linux (Image credit: Petri/Sagar)

Listing all groups with the /etc/groups file

Another way to see all groups is to navigate to the /etc/groups directory using the cat utility. By navigating to the /etc/groups directory, you can see all the groups with their associated group identifier (GID).

In the image below, you can see that the GID for the root group is 0, the GID for the bin group is 2, and so on.

cat /ect/group
The cat utility let us see all groups with their group identifier (Image credit: Petri/Sagar)
The cat utility let us list all Linux groups with their group identifier (Image credit: Petri/Sagar)

Listing all group names with the cut command

We can also use the cut command to see all group names. When we run this command, it retrieves all the details about groups in different columns, so we’ll be using the (-d) flag as delimiter as well as the (-f) option to select the column we want.

cut -d: -f1,6 /etc/passwd
Using the cut command to list all group names on Linux
Using the cut command to list all group names on Linux (Image credit: Petri/Sagar)

Listing all groups with the getent command

The getent command is another way to find groups. Using this command, you can fetch various information such as user accounts, their creation date, their group information, etc.

You can use the simple command below:

getent group
The getent command is another way to list groups on Linux
The getent command is another way to list groups on Linux (Image credit: Petri/Sagar)

How to list all users on Linux

As a Linux administrator, you may need at times to fetch the list of all users to perform some actions on them such as checking the groups they are attached to, the permissions they have, etc. I’ll now show you different ways to list all users on Linux.

Listing all users with the cat command on the ‘/etc/passwd” file

The cat command can once again be used to check the users of a file or the groups attached to it. The /etc/passwd file contains information about all Linux users including their user identifier (UID), group identifier (GID), and more.

In the command below, the first line shows details about the ‘root’ user: It has 0 as its UID and GID, and we can also see that it can log into the Bash terminal.

cat /etc/passwd
Listing all users with the cat command on the ‘/etc/passwd” file
Using the cat command on the ‘/etc/passwd” file (Image credit: Petri/Sagar)

(Image credit: Petri/Sagar)

Listing users with the “who” and “users” commands

The who command provides details about users who are logged into the Linux machine. The users command is another way to check who is currently logged into Linux.

The who and users commands can be used to see all connected users
The who and users commands can be used to list users connected to Linux (Image credit: Petri/Sagar)

How to list the groups of a specific user

After I explained how to list all groups on Linux, I’ll now show you various ways to check the groups of a specific user.

With Libuser

To check the groups of a specific user, we can use a tool named libuser, which stands for “library of users.” You can use the command below to install libuser:

sudo apt install libuser

Now, to check the list of groups of a specific user, you can run the commands below.

sudo libuser-lid
sudo libuser-lid ubuntu
sudo libuser-lid sudo
sudo libuser-lid root
Checking the list of groups of a specific user with libuser
Checking the list of groups of a specific user with libuser (Image credit: Petri/Sagar)

A couple of notes about these commands:

  • In the first command, we are not declaring any user so the command automatically picks ‘root’ as the user.
  • In the second command, we used ‘ubuntu’ as the user and we can see all the groups associated with this user.
  • In the third command, ‘sudo’ isn’t a correct user, that’s why the command doesn’t show any groups.
  • The last command shows the ‘root’ user, which has only one group.

With the /etc/group file.

We can also use the cat command on the /etc/group file to see the groups of a specific user. In the command example below, we are extracting all the groups of the user ‘ubuntu’ from the /etc/group file by running the cat command followed by the grep command to filter the ubuntu user.

cat /etc/group | grep ubuntu
Checking the list of groups of a specific user with the cat command
Checking the list of groups of a specific user with the cat command (Image credit: Petri/Sagar)

With the id command

Finally; we can use the id command to list the groups of a specific user. In the example below, we can see that the ‘ubuntu’ user has ‘ubuntu’ as its primary group with 1000 as the GID, and the rest of the groups such as dialout, adm, etc. are all secondary groups.

id ubuntu
Image10
The id command shows all groups for a specific user (Image credit: Petri/Sagar)

Conclusion

You should now have a good understanding of the different types of groups and Linux and the different ways to get more information about them. I hope that this knowledge will help you be more efficient when working with different file systems and disk permissions, which are quite important from a Linux administrator point of view.