Source From Here
PrefaceIn Linux, a group is a collection of users. The main purpose of the groups is to define a set of privileges like read, write, or execute permission for a given resource that can be shared among the users within the group. Users can be added to an existing group to utilize the privileges it grants.
This tutorial explains how to show all groups a user is a member of. We will also explain how to list all members of a group.
Linux Groups
There are two types of groups that a user can belong to:
List all Groups a User is a Member of
There are multiple ways to find out the groups a user belongs to.
The primary user’s group is stored in the /etc/passwd file and the supplementary groups, if any, are listed in the /etc/group file. One way to find the user’s groups is to list the contents of those files using cat , less or grep . Another easier option is to use a command whose purpose is to provide information about the system’s users and groups.
Using the groups command
The most memorable command to list all groups a user is a member of is the groups command. When executed without an argument the command will print a list of all groups the currently logged in user belongs to:
To get a list of all groups a specific user belongs to, provide the username to the groups command as an argument:
Using the id command
The id command prints information about the specified user and its groups. If the username is omitted it shows information for the current user. For example to get information about the user john you would type:
To print only the names instead of the numbers use the -n option. Option -g will print only the primary group and -G all groups:
List All Members of a Group
To list all members of a group, use the getent group command followed by the group name. For example, to find out the members of a group with the name admin_user you would use the following command:
List All Groups
To view all groups present on the system simply open the /etc/group file. Each line in this file represents information for one group:
Another option is to use the getent command which displays entries from databases configured in /etc/nsswitch.conf file including the group database which we can use to query a list of all groups. To get a list of all groups, type the following command:
The output is the same as when displaying the content of the /etc/group file. If you are using LDAP for user authentication the getent will display all groups from both /etc/group file and LDAP database.
You can also use awk or cut to print only the first field containing the name of the group:
Supplement
* Add a User to a Group (or Second Group) on Linux
* How to create users and groups in Linux from the command line
沒有留言:
張貼留言