Updated May 20, 2023
Definition of Linux List Groups
In Linux, the primary usage of groups is to define a set of access permissions for resources, such as files or directories, that are shared among a set of users. A group is a collection of users who are granted the same set of permissions to access and manipulate these shared resources. In this article, we will discuss how to use group commands in linux and ways to see the groups in linux.
Below are the types of groups to which a user belongs:
- Primary Group: The group designated to the files created by the user is generally the primary group, and its name is typically the same as the name of the creator. Unique users should belong to only one primary or login group.
Secondary/supplementary Group: This secondary group allows the user in granting particular privileges to a limited number of users. this user also might be a member of any other or many supplementary groups.
There are many ways to figure out groups that a user belongs to.
The login user’s group is located in the file – /etc/passwd, and the secondary group users are located in the /etc/group file. The general way to list the groups is by listing the list of the above file by using linux commands like less, cat, or grep. the other way is to use the command that provides information about the system’s groups and users.
Below are the ways to find out either group or user’s information:
- /etc/group File: User group file
- Members Command: List members of a group
- Lid Command (or Libuser-Lidon Newer Linux Distros): List user’s groups or group’s users
Syntax of Group List Command in Linux
Syntax of using groups command in linux can be done in two ways. they are:
One by mentioning the username associated with the group.
Second by just mentioning ‘groups’ as shown below:
How Does Linux List Groups Work?
We can get the list of groups in linux in two ways:
- /etc/group File
- Getent Command
These commands are explained briefly using syntaxes and examples below for better understanding. the above options are described below:
1. Using /etc/group File
To get all the local groups in the system, we can go to /etc/group file. as this is a file, we can open it and see all the local groups present.
2. Using Getent Command
The linux getent command can be used to retrieve entries from databases contained in name service switch libraries. We can use this to retrieve information from the group database.
Examples of Group List Command in Linux
Below are the ways to use group command in linux with its syntax and example.
- We can print only the group names using cut command, as shown below. this example can be suited best when you are searching for any specific name of the group in the system.
Cat -D: -F1 /etc/group
Example: The cut command here is splitting the line with colon(:) as its delimiter. the first field is cut by using -f1 option.
- We can list all the group names alphabetically by using the sort command in linux. the sort command usually helps to sort the output in alphabetical order.
Getent Group | Cut -D: -F1 | Sort
- To count the number of groups in the system using the ‘-c’ option, you can utilize the getent command with the /etc/group file. The syntax and an example are provided below:
Cat /etc/group | Grep -C "" Getent Group | Grep -C ""
To list all the groups a user belongs to in the system, you can use the groups keyword followed by the username. The syntax and an example are provided below:
- To list the groups of current users in the system, we need to run the groups command without any username.
- To list the user groups in the system along with the group id, we need to run the id command. it will then print the groups associated with its group id.
- To list all the members in a group, we can use getent group command with the group name. syntax and example is given below.
Getent Group Group_Name
Getent Group Admin
Note: When you get no output for the above syntax, that means there is no such group available in the system.
In this article, we have seen that /etc/group file and getent commands will give you complete information on the group lists in linux. to modify the output, we have also used cut, sort, count options for better understanding.
We hope that this EDUCBA information on “Linux List Groups” was beneficial to you. You can view EDUCBA’s recommended articles for more information.