How to find out what group a given user has?

LinuxUnixCommand LineSysadmin

Linux Problem Overview


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

Linux Solutions


Solution 1 - Linux

groups

or

groups user

Solution 2 - Linux

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

id userid

Solution 3 - Linux

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.

Solution 4 - Linux

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

Solution 5 - Linux

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex ArgoView Question on Stackoverflow
Solution 1 - LinuxBombeView Answer on Stackoverflow
Solution 2 - LinuxPaul TomblinView Answer on Stackoverflow
Solution 3 - LinuxkenorbView Answer on Stackoverflow
Solution 4 - LinuxNilsView Answer on Stackoverflow
Solution 5 - LinuxNamasivayam ChinnapillaiView Answer on Stackoverflow