In Linux, the group is a collection of users. The main purpose of these groups is to define a set of given resource permissions, such as read, write, or execute permissions, which can be shared between users in the group. You can also add users to existing user groups to take advantage of its privileges granted.
This tutorial describes how to display all the groups the user belongs in Linux. We will also explain how to list all the members of the group.
Linux group
Users can belong to two types of groups:
- Login or main group - the group is assigned to a user-created file. Typically, the same name as the user's primary group. Each user must belong to a major group.
- Auxiliary group or supplementary group - used to grant certain privileges to a group of users. The user may be a member of zero or more auxiliary groups.
Lists all the groups the user belongs
There are several ways to find groups the user belongs.
Primary user group stored in / etc / passwd file, supplemented group (if any) listed in the / etc / group file.
One way to find a user group is to use a cat, less or grep command to list the contents of these files. Another simpler option is to use a command, its purpose is to provide information about the system users and groups.
Use the command groups
The most commonly used commands all groups the user belongs to is listed groups command. When executed without arguments, the command will print a list of the currently logged-on user belongs to all the groups:
groups
The first group is the primary group.
To get a list of all groups specific user belongs to, the user name as a parameter groups provided:
The first group is the same as before the main group.
Use the id command
The id command prints information about the specified user and group. If you omit the user name, current user's information is displayed.
This command displays the user name (uid), user's primary group (gid) and user's secondary group (groups)
To print only user and group names without printing the user ID and group ID, use the -n option. -G option to print only the main group, -G option to print all groups.
The following command will print the name of the current user belongs:
id -a
All members of the group listed
To list all members of a group, use getent group command followed by the group name.
For example, to find the members of a group with that name, developers use the following command:
If the group exists, the command will print the group and all its members:
If no output indicates that the group does not exist. As shown below
List all groups
To see all the groups present on the system, simply open the / etc / group file. Each line of this file on behalf of a group of information.
less /etc/group
Another option is to use getent group command to display the database file /etc/nsswitch.conf configured in the entry, including the database can be used to query the list of all the groups.
To get a list of all groups, type the following command:
getent group
Output display / etc same output when the contents of the file group /. If you are using LDAP for user authentication, getent then all groups / etc / group file and display the LDAP database.
You can also use awk or cut print only the first field contains the name of the group:
getent group | awk -F: '{ print $1}'
getent group | cut -d: -f1
in conclusion