Alex Argo
Alex Argo

Reputation: 9030

How to find out what group a given user has?

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

Upvotes: 292

Views: 331897

Answers (5)

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 files.

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

Upvotes: 0

kenorb
kenorb

Reputation: 166871

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

id -Gn [user]

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

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

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.

Upvotes: 20

Paul Tomblin
Paul Tomblin

Reputation: 182880

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

id userid

Upvotes: 111

Nils
Nils

Reputation: 13777

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

Upvotes: 0

Bombe
Bombe

Reputation: 83963

groups

or

groups user

Upvotes: 419

Related Questions