Groups are the essential part of basic Linux filesystem security by design. If you know about the file permissions in Linux, you already know that groups play a huge role in limiting and allowing access of files to the desired users only.
The idea is to collect users in a group based on their roles. This way, you can easily set permissions for the intended groups of user. For example, users in sudo groups can run commands with superuser privileges while other users cannot.
Now that might make you curious about knowing which groups you belong to and this is exactly what I am going to show you in this quick tutorial.
Check user group in Linux command line
To find out which groups your user account belongs to, simply use this command:
This will show all the groups you belong to.
abhishek@linuxhandbook:~$ groups abhishek adm cdrom sudo dip plugdev lpadmin sambashare kvm
As you can see, the user abhishek belongs to groups abhishek, sudo, adm and several other groups.
I am using Ubuntu in this tutorial and Ubuntu creates a group with the same name as the user. This is why you see user abhishek belonging to group abhishek.
Find out groups of other users in Linux
You just learned to see the groups you belong to. What about checking the groups of other users on your system?
You probably already know how to list users in Linux. When you know the username, you can find which group it belongs to by using the groups command in this way:
Obviously, you’ll have to replace the user_name in the above command with the name of the other user.
abhishek@linuxhandbook:~$ groups prakash prakash : prakash sudo
You can also check groups of more than one users at a time by
groups user_1 user_2 user_3
The output will display the groups information for each user in separate rows:
abhishek@linuxhandbook:~$ groups abhishek prakash abhishek adm cdrom sudo dip plugdev lpadmin sambashare kvm prakash : prakash sudo
Bonus Tip: Get group information along with gid
You can also get group information of a user with id command. The additional benefit of the id command is that it also displays the uid of the user and gid of the groups. Read this article to know more about UID in Linux.
The user name is optional and by default, it will show the information about your user account.
uid=1000(abhishek) gid=1000(abhishek) groups=1000(abhishek),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin),126(sambashare),127(kvm)
You can see that getting the group information of a user is a simple task. It could come in handy in many situations and I would let you experience them on your own.
Bonus Tip 2: Get primary group of a user in Linux
Every user has a default or primary group. You can check the primary group of a user with id command in the following fashion:
id -gn user_name
You can change the primary and secondary group of a user with the usermod command.
I hope this quick little tip helped you to list user groups in Linux. You may also want to read about checking the members of a group in Linux.
If you have questions or suggestions, please feel free to use the comment section.