linux commands

How to find which users belongs to a specific group in linux

This sneppet guides you on how to find which users belongs to a specific group in linux. I am going to use my GCP cloud shell to show the commands for you. Let’s see in detail.

Find which users belongs to a specific group in linux

Let’s assume you are looking for how to find list of users who belongs to a group, so that you can manage file or folder permissions for giving some access.

First, you need to check list of all groups for your reference. Execute the following command to get list of all groups.

$ sudo groups

root google-sudoers

From the above response you can see that there are two groups (root and google-sudoers).

To find which users belong to specific group in linux, you  need to run the following command in the linux terminal.

$ sudo grep '^<groupname>:' /etc/group

For example, the following command list users belong to group “google-sudoers”

$ sudo grep '^google-sudoers:' /etc/group

google-sudoers:x:1001:sneppets,root

Therefore, the users (sneppets and root) belong to group “google-sudoers”.

Similarly, to get list of all groups a specific user belong to, you need to execute the following command from the terminal.

$ sudo groups <username>

For example, to get list of all groups the user “root” belong to, run the following command.

$ sudo groups root

root : root google-sudoers

Therefore, the user “root” belong to groups (root and google-sudoers)

That’s it. You had learnt how to list all groups and how to find list of all users who belongs to specific group.

Hope it helped 🙂

References

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments