Coder Perfect

How can I find out which group a user belongs to?


How do you find out what group a user belongs to in Unix/Linux using the command line?

Asked by Alex Argo

Solution #1



groups user

Answered by Bombe

Solution #2

This one displays the user’s uid as well as all of the groups to which they belong (along with their gids).

id userid

Answered by Paul Tomblin

Solution #3

To see the groups to which you (or the optionally provided user) belong on Linux/OS X/Unix, type:

id -Gn [user]

On Unix, this is comparable to the groups [user] utility, which has been deprecated.

For standard interaction on OS X/Unix, run the command id -p [user].

The following is an explanation of the parameters:

Answered by kenorb

Solution #4

or just look through /etc/groups (Ok, this isn’t going to work if it’s using pam with ldap.)

Answered by Nils

Solution #5

The script that is incorporated into Ansible and generates a dashboard in CSV format is shown below.



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 results are saved in the text files below.

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

Answered by Namasivayam Chinnapillai

Post is based on