Definition of Linux List Groups
In linux, the main usage of groups is to define a set of access to given resources like read, write, or execute that is to be shared among the users in the group. A set of users are called a group. in this article, we will discuss how to use group command in linux and ways to see the groups in linux.
Below are the types of groups where a user belongs to:
- Primary Group: This is the group that is designated to the files which are created by the user. generally, the primary group’s name is similar to the name of the creator. Unique user should be belonging to only one primary or login group.
Secondary/supplementary Group: This secondary group allows the user in granting particular privileges to a limited 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. 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 is providing 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 we done in two ways. they are:
One by mentioning 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 by two ways:
- /etc/group File
- Getent Command
These command 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
Getent command in linux will help to get the entries from databases that is stored in the name service switch libraries. with this, we can get the information of groups from 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 show below. this example can be suited best when you are searching for any specific name of group in the system.
Cat -D: -F1 /etc/group
Example: The cut command here is spliting the line with colon(:) as its delimiter. the first field is cut by using -f1 option.
- We can list all the group names in alphabetical order by using sort command in linux. the sort command usually helps to sort the output in alphabetical order.
Getent Group | Cut -D: -F1 | Sort
- We can also count the number of groups present in the system using ‘-c’ option. here we can count the group names in both /etc/group file and by using getent command. syntax and example is given below.
Cat /etc/group | Grep -C ""
Getent Group | Grep -C ""
To list all the user’s groups in the system we can use groups keyword. syntax and example is given below.
- To list the groups of current user in the system, we need to run the groups command without any username. It will then print the groups that the current user is associated with.
- To list the user groups in the system along with 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.
A set of users are called a group. in linux, the main usage of groups is to define a set of access to given resources like read, write, or execute that is to be shared among the users in the group. In this article, we have seen that /etc/group file and getent command will give you the complete information on the group lists in linux. to modify the output, we have also used cut, sort, count options for better understanding.
This is a guide to Linux List Groups. Here we also discuss the definition and how does linux list groups work? along with different examples and its code implementation. You may also have a look at the following articles to learn more –