Show all users and their groups/vice versa
All users:
$ getent passwd
All groups:
$ getent group
All groups with a specific user:
$ getent group | grep username
List users and their groups:
for user in $(awk -F: '{print $1}' /etc/passwd); do groups $user; done
List groups and their users:
cat /etc/group | awk -F: '{print $1, $3, $4}' | while read group gid members; do
members=$members,$(awk -F: "\$4 == $gid {print \",\" \$1}" /etc/passwd);
echo "$group: $members" | sed 's/,,*/ /g';
done
If you dont care about remote users such as LDAP or NIS, to list users and their associated groups in a simple way:
cut -d: -f1 /etc/passwd | xargs groups
Output;
root : root myuser : root www-data fuse anotheruser : anotheruser cdrom floppy audio dip video plugdev scanner bluetooth netdev