Linux 如何找出给定用户的组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/350141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:47:41  来源:igfitidea点击:

How to find out what group a given user has?

linuxunixcommand-linesysadmin

提问by Alex Argo

In Unix/Linux, how do you find out what group a given user is in via command line?

在 Unix/Linux 中,如何通过命令行找出给定用户所在的组?

采纳答案by Bombe

groups

or

或者

groups user

回答by Paul Tomblin

This one shows the user's uid as well as all the groups (with their gids) they belong to

这个显示了用户的 uid 以及他们所属的所有组(及其 gid)

id userid

回答by Nils

or just study /etc/groups (ok this does probably not work if it uses pam with ldap)

或者只是研究 /etc/groups (好吧,如果它使用带有 ldap 的 pam,这可能不起作用)

回答by kenorb

On Linux/OS X/Unix to display the groups to which you (or the optionally specified user) belong, use:

在 Linux/OS X/Unix 上,要显示您(或可选指定的用户)所属的组,请使用:

id -Gn [user]

which is equivalent to groups [user]utility which has been obsoleted on Unix.

这相当于groups [user]在 Unix 上已经过时的实用程序。

On OS X/Unix, the command id -p [user]is suggested for normal interactive.

在 OS X/Unix 上,id -p [user]建议使用该命令进行正常交互。

Explanation on the parameters:

参数说明:

-G, --groups- print all group IDs

-n, --name- print a name instead of a number, for -ugG

-p- Make the output human-readable.

-G, --groups- 打印所有组 ID

-n, --name- 打印名称而不是数字,用于-ugG

-p- 使输出可读。

回答by namasivayam.cse

Below is the script which is integrated into ansible and generating dashboard in CSV format.

下面是集成到ansible并以CSV格式生成仪表板的脚本。

sh collection.sh

#!/bin/bash

HOSTNAME=`hostname -s`

for i in `cat /etc/passwd| grep -vE "nologin|shutd|hal|sync|root|false"|awk -F':' '{print}' | sed 's/[[:space:]]/,/g'`; do groups $i; done|sed s/\:/\,/g|tr -d ' '|sed -e "s/^/$HOSTNAME,/"> /tmp/"$HOSTNAME"_inventory.txt

sudo cat /etc/sudoers| grep -v "^#"|awk '{print }'|grep -v Defaults|sed '/^$/d;s/[[:blank:]]//g'>/tmp/"$HOSTNAME"_sudo.txt

paste -d , /tmp/"$HOSTNAME"_inventory.txt /tmp/"$HOSTNAME"_sudo.txt|sed 's/,[[:blank:]]*$//g' >/tmp/"$HOSTNAME"_inventory_users.txt

My output stored in below text files.

我的输出存储在以下文本文件中。

cat /tmp/ANSIBLENODE_sudo.txt
cat /tmp/ANSIBLENODE_inventory.txt
cat /tmp/ANSIBLENODE_inventory_users.txt