Home Linux User Management How To Find Out Which Groups A User Belongs To In Linux

How To Find Out Which Groups A User Belongs To In Linux

By sk
Published: Updated: 23.5K views

A Linux group is a collection of one or more users with identical permission requirements on files and directories. An user can be a member of more than one group at a time. In Linux, each group information is stored in the "/etc/group" file. In this tutorial, we will see all the possible ways to easily find out which groups a user belongs to in Linux and Unix-like operating systems.

Finding out the groups to which a user account belongs will be helpful in many occasions. For instance, the other day I was installing Dropbox on my Ubuntu server. When configuring Dropbox, I had to enter my current user name and the group name. You could also be in a situation where you need to identify the groups a user belongs to. If so, use any one of the following methods to know what group a user is in.

Find out which Groups a user belongs to in Linux

There are many ways to list the groups that a Linux user is a member of. The most commonly used method is by using groups command.

Method 1 - groups command

The groups command displays the current group names and the users belongs to those groups in Linux and Unix-like operating systems.

First, let us find how many groups are there in our Linux system.

To find the list of available in Linux, run the groups command without any arguments like below:

$ groups

Sample output:

sk adm disk cdrom sudo dip plugdev lpadmin sambashare kvm libvirt vboxusers
List available groups in Linux

List available groups in Linux

As you can see in the above output, there are currently 12 groups in my system.

Now, find out which groups a specific user, for example sk, belongs to. To do so, enter "groups" command followed the username like below:

$ groups sk

Sample output:

sk : sk adm disk cdrom sudo dip plugdev lpadmin sambashare vboxusers kvm libvirt
Find out which Groups a user belongs to in Linux using groups command

Find out which Groups a user belongs to in Linux using groups command

Here, sk is my username and the user sk is the member of all of the above groups. You might have noticed that "sk" is listed twice. Because, I have used the same name for both username and group name.

For more details about "groups" command, refer man pages.

$ man groups

Method 2 - id command

The another way to identify the groups a user is in is by using "id" command. The id command is used to print user and group information for the specified USER. If the USER is not specified, it will print the information for the current user.

To identify all the groups that a user belongs to using "id" command, run:

$ id sk

Replace sk with your username.

Sample output:

uid=1000(sk) gid=1000(sk) groups=1000(sk),4(adm),6(disk),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin),126(sambashare),1001(vboxusers),129(kvm),136(libvirt)
Find out which Groups a user belongs to in Linux using id command

Find out which Groups a user belongs to in Linux using id command

As you see here, the id command not only lists the groups that user is member of, but also group id and user id (UID and GID) of the given user and the groups. It has more verbose output than the "groups" command, so if you need the group id/user id use this!

If you don't want to print the numbers, simply use -Gn flag like below:

$ id -Gn sk
sk adm disk cdrom sudo dip plugdev lpadmin sambashare vboxusers kvm libvirt

For more details, refer the man pages.

$ man id

Method 3 - using "/etc/group" file

As I stated already, the /etc/group file contains the information about each group in a Linux system. You can find which groups the given user belongs to from the contents of the /etc/group file with the help of "grep" command as shown below:

$ grep sk /etc/group

Sample output:

adm:x:4:syslog,sk
disk:x:6:sk
cdrom:x:24:sk
sudo:x:27:sk
dip:x:30:sk
plugdev:x:46:sk
lpadmin:x:116:sk
sk:x:1000:
sambashare:x:126:sk
vboxusers:x:1001:sk
kvm:x:129:sk
libvirt:x:136:sk,libvirtdbus

If you want to exclude the group ids and the username and display only the group names, pipe the "grep" command's output to "awk" command like below:

$ grep sk /etc/group | awk -F: '{ print $1 }'

Sample output:

adm
disk
cdrom
sudo
dip
plugdev
lpadmin
sk
sambashare
vboxusers
kvm
libvirt
Find which Groups a user belongs to in Linux using grep and awk commands

Find which Groups a user belongs to in Linux using grep and awk commands

Method 4 - getent command

The "getent" command displays entries from databases supported by the Name Service Switch libraries, which are configured in /etc/nsswitch.conf file.

We can list all available groups and their members in a Linux system using getent command like below:

$ getent group

To find which groups a specific user (E.g. sk) belongs to,  run:

$ getent group | grep sk

Sample output:

adm:x:4:syslog,sk
disk:x:6:sk
cdrom:x:24:sk
sudo:x:27:sk
dip:x:30:sk
plugdev:x:46:sk
lpadmin:x:116:sk
sk:x:1000:
sambashare:x:126:sk
vboxusers:x:1001:sk
kvm:x:129:sk
libvirt:x:136:sk,libvirtdbus

If you want to display only the groups excluding all other details, filter the output with "grep" and "awk" commands like below:

$ getent group | grep sk | awk -F: '{ print $1 }'
adm
disk
cdrom
sudo
dip
plugdev
lpadmin
sk
sambashare
vboxusers
kvm
libvirt
Find which Groups a user belongs to in Linux using getent, grep and awk commands

Find which Groups a user belongs to in Linux using getent, grep and awk commands

List all users belongs to a group in Linux

We can also find the list of all users that belongs to a specific group. For instance, the following command displays the users which are belongs to the group named storage.

$ grep -w storage /etc/group
storage:x:95:sk

Easy, right? Indeed. Finding which groups a user belongs and the users of a specific group is super easy!!


Related read:


Hoe this helps.

You May Also Like

1 comment

Jalal February 25, 2021 - 9:55 am

Hi,
Pretty good and easy.
Thank you so much for the great topic

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More