CentOS 7.x Get members of OS user group or see groups that a user belongs to

From Notes_Wiki

Home > CentOS > CentOS 7.x > Troubleshooting > CentOS 7.x Get members of OS user group or see groups that a user belongs to

List all users and their corresponding groups

In Linux to list all users and groups for which they are member:

  cat /etc/passwd | awk -F ':' '{print $1 }' | xargs -n1 groups

In other way round to see which users belong to a particular group we can use above output and grep for groupname. Such as

  cat /etc/passwd | awk -F ':' '{print $1 }' | xargs -n1 groups | grep <group1>


Getent information of a group

Please note that genent group might not be sufficient as users primary group information would be in /etc/passwd and wont be shown in this output. For example

   getent group root 

on CentOS might show group root not having any members. However doing

   groups root 

will show user root belonging to group root


Home > CentOS > CentOS 7.x > Troubleshooting > CentOS 7.x Get members of OS user group or see groups that a user belongs to