如何找出给定用户所属的组?

问题描述 投票:255回答:5

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

linux unix command-line sysadmin
5个回答
368
投票
groups

groups user

102
投票

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

id userid

16
投票

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

id -Gn [user]

相当于在Unix上已废弃的groups [user]实用程序。

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

参数说明:

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

[-n--name-为-ugG]打印名称而不是数字>

[-p-使输出易于阅读。


0
投票

或只是研究/ etc / groups(好吧,如果它与ldap一起使用pam,那么这可能行不通)


0
投票
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
© www.soinside.com 2019 - 2024. All rights reserved.