How to List Groups in Linux?
A Linux system consists of multiple users divided into different groups. These groups set different privileges for every user, which can be accessed by adding a user to the group. The group’s information in Linux is kept in the “/etc/group” file.
This guide will discuss the possible ways to list groups in Linux. The post’s content is as follows:
Let’s begin!
Mục Lục
What are the Types of Groups in Linux?
There are two types of groups that exist in Linux, which are as follows:
Primary Group
How to Install Packages in Ubuntu
Please enable JavaScript
A user is a member of a primary group that is created by default for the user in the “root” directory. The name of the primary group is the same as the user name.
Secondary Group
A secondary group is created by the system to give certain permissions to the user.
How to List All Groups for a System in Linux?
To get the list of groups with their details in a system, we can use the “/etc/group” file with “cat”, “more”, “less”, and “getent” commands.
Using cat Command
To find all the users (primary and secondary groups) in a system, use the following command:
$ cat /etc/group
Using more or less Command(s)
The basic purpose of the “more” and “less” commands is to navigate in a directory or system for a single window or to show a complete list. We can use these commands for the “/etc/group” file to get the groups in a system.
To list a few results from a long list of groups in a system using the “less” command, utilize the following command:
$ less /etc/group
To get the entire list of groups, use the “more” command as follows:
$ more /etc/group
Using getent Command
To get the entire list of primary and secondary groups using the “getent”, run the below-written command in the terminal:
How to List All Group Names in Linux?
The users can list the names of all the groups in a system using the “cat” command with “cut” and “awk” commands.
Using the cat With cut Command
To list all the group names for a system in Linux using the “cat” command, utilize the below command:
$ cat /etc/group | cut -d: -f1
Using the cat With awk Command
Another way to find the user names in a system is by executing the below command:
$ cat /etc/group | awk -F: '{print $1}'
The output shows all the group names (including primary and secondary) of the system.
How to List the Groups of a User?
There are several ways to find the group of users in Linux. In this section, we will discuss how to find the groups of the currently logged-in users using “groups”, “id”, “getent”, and “/etc/group” commands.
Let’s first list the currently logged-in users to the host in the system. To list the connected users to the host, use the below-mentioned command:
$ who
The output shows the user “itslinuxfoss” is logged in and its other details.
Using the groups Command
To get the groups of the logged-in user “itslinusfoss”, execute the “groups” command as follows:
$ groups
The output shows the “Names” of the groups, i.e., itslinuxfoss, adm, etc.. the user belongs.
Similarly, to get the list of groups of the logged-in user or for the specified user with its name, the “groups <username>” syntax is used. For listing the user groups for the user “itslinuxfoss”, execute the below command in the terminal:
$ groups itslinuxfoss
The output shows all the groups where the user “itslinuxfoss” is a member. The first group in the above list is the “primary” group.
Using the id Command
Another way to find the logged-in user is by utilizing the “id” command as shown below:
$ id -nG
The “id” command can be utilized to find the groups of a specified user. To find all the groups for user “itslinuxfoss”, execute the below command:
$ id itslinuxfoss -nG
Using cat With grep Command
To get all the required details of the groups for a specified or logged-in user, execute the “grep” command with the “cat” command as follows:
$ cat /etc/group | grep <group>
The output shows the group names before the first delimiter (:) like adm, cdrom and to the last line sambashare are groups for the itslinuxfoss user.
Using getent Command
If you want to get the primary group of a user in Linux, use the below-mentioned command:
$ getent group itslinuxfoss
The output shows the following details of the primary group of the “itslinuxfoss” user delimited with a colon “:” sign, “Username”, “Encrypted Password”, and the “User Group ID (UID)”.
That’s the end of this guide!
Conclusion
To list the groups in Linux, use the “groups”, “id”, “getent”, and “/etc/group” commands. We can get the groups of currently logged-in users and specified users in Linux by utilizing the “cat”, “less”, “more”, and “getent” commands. Moreover, we have discussed the methods to list the group names only in Linux. This post has demonstrated the practical implementation of the commands to list groups in Linux.