在Unix / Linux中,如何通过命令行找出给定用户所属的组?
groups
或
groups user
此显示用户的uid以及他们所属的所有组(及其gid)
id userid
在Linux / OS X / Unix上,显示您(或可选的指定用户)所属的组,请使用:
id -Gn [user]
相当于在Unix上已废弃的groups [user]
实用程序。
在OS X / Unix上,建议使用id -p [user]
命令进行常规交互。
参数说明:
[
-G
,--groups
-打印所有组ID[
-n
,--name
-为-ugG
]打印名称而不是数字>[
-p
-使输出易于阅读。
或只是研究/ etc / groups(好吧,如果它与ldap一起使用pam,那么这可能行不通)
Below is the script which is integrated into ansible and generating dashboard in CSV format.
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$1}' | 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 $1}'|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 file.**
cat /tmp/ANSIBLENODE_sudo.txt
cat /tmp/ANSIBLENODE_inventory.txt
cat /tmp/ANSIBLENODE_inventory_users.txt