Linux List Groups | How does Linux List Groups Work?

Linux List GroupsLinux List Groups

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

Linux List Groups-1.1

Linux List Groups-1.1

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:

Linux List Groups-1.2Linux List Groups-1.2

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.

Linux List Groups-1.3Linux List Groups-1.3

  • 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:

Linux List Groups-1.4Linux List Groups-1.4

  • 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:

Linux List Groups-1.5Linux List Groups-1.5

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:

Linux List Groups-1.6Linux List Groups-1.6

  • 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:

Linux List Groups-1.7Linux List Groups-1.7

  • 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:

Linux List Groups-1.8Linux List Groups-1.8

  • 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:

Linux List Groups-1.9Linux List Groups-1.9

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.

Alternate Text Gọi ngay