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.
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.
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.
By using the groups command, we will see all the primary groups that are present on a Linux machine.
groups
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
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
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
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.
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
(Image credit: Petri/Sagar)
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.
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.
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
A couple of notes about these commands:
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
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
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.