Linux List Groups | How does Linux List Groups Work?
Mục Lục
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.
Linux Groups:
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
Groups [username]....
Second by just mentioning ‘groups’ as shown below:
$groups
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.
Cat /etc/group
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.
Getent Group
Output:
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.
Command:
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.
Command:
Getent Group | Cut -D: -F1 | Sort
Output:
- 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.
Command:
Cat /etc/group | Grep -C ""
Getent Group | Grep -C ""
Output:
To list all the user’s groups in the system we can use groups keyword. syntax and example is given below.
Syntax:
Groups Username
Command:
Groups Admin
Output:
- 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.
Command:
Groups
Output:
- 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.
Syntax:
Id Username
Command:
Id Root
Output:
- To list all the members in a group, we can use getent group command with the group name. syntax and example is given below.
Syntax:
Getent Group Group_Name
Command:
Getent Group Admin
Output:
Note: When you get no output for the above syntax, that means there is no such group available in the system.
Conclusion
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.
Recommended Articles
We hope that this EDUCBA information on “Linux List Groups” was beneficial to you. You can view EDUCBA’s recommended articles for more information.