2020年9月25日 星期五

[Linux 文章收集] How to List Groups in Linux

 Source From Here

Preface
In 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:
Primary or login group – is the group that is assigned to the files that are created by the user. Usually, the name of the primary group is the same as the name of the user. Each user must belong to exactly one primary group.
Secondary or supplementary group - used to grant certain privileges to a set of users. A user can be a member of zero or more secondary groups.


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:
$ groups
john adm cdrom sudo dip plugdev lpadmin sambashare

To get a list of all groups a specific user belongs to, provide the username to the groups command as an argument:
$ groups john
john : john test docker admin_user

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:
# id john
uid=1001(john) gid=1001(john) groups=1001(john),1000(test),972(docker),1002(admin_user)

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:
# id -nG john
john test docker admin_user

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:
# getent group admin_user
admin_user:x:1002:john,root

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:
$ less /etc/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:
$ getent group

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:
$ getent group | awk -F: '{ print $1}'
$ getent group | cut -d: -f1

Supplement
Add a User to a Group (or Second Group) on Linux
How to create users and groups in Linux from the command line

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...