5 Methods to Find Out Which Group a User Belongs to in Linux

Adding a user to an existing group is one of the typical task of a Linux administrator.

For some administrator working in the larger environment, this is a daily activity.

Since mine is a big environment, I do this on a daily basis in my environment due to business needs.

This is an important command that help you to identify an existing groups on a Linux system.

These commands help you to identify which groups a user belongs to.

All system users are listed in the /etc/passwd file, the groups are listed in the /etc/group file, and the actual password is stored in the /etc/shadow file.

No matter what command we use, it will fetch information from these files.

Also, each command has its own unique feature, which enables the user to get only the information they need.

There are two types of groups in Linux:

  • Primary Group: The primary group is the main group associated with the user account. Each user must be a member of a single primary group.
  • Secondary Group: The second group is used to grant additional rights to the user. Each user can become a member of a multiple secondary group.

What is /etc/passwd file

“/etc/passwd” is a text file containing every user information that is required to login to the Linux system. It maintains useful information about users such as username, password, user ID, group ID, user information, home directory and shell.

Each user profile in the password file is a single line with seven fields as described above.

$ grep "daygeek" /etc/passwd

daygeek:x:1000:1000:daygeek,,,:/home/daygeek:/bin/bash

What is /etc/group file

“/etc/group” is a text file that defines which groups a user belongs to. We can add multiple users in the same group.

Linux permissions are organized into three classes user, group, and other, which allows the user to access other users’ files and folders.

It maintains useful information about the group such as group name, group password, group ID (GIT) and membership list. Each group details in a group file is a single line with four fields as described above.

You can do this using the below seven commands.

  • groups:Show All Members of a Group.
  • id:Print user and group information for the specified username.
  • lid or libuser-lid:It display user’s groups or group’s users.
  • getent:get entries from Name Service Switch libraries.
  • compgen:compgen is bash built-in command and it will show all available commands for the user.
  • members:List members of a group.
  • /etc/group file Also, we can grep the corresponding user’s groups from the /etc/group file.

Method-1: How to Find Out Which Groups a User Belongs to in Linux Using the groups Command

The Groups command Prints the name of the primary and their subgroups for the given username.

$ groups daygeek

daygeek : daygeek adm cdrom sudo dip plugdev lpadmin sambashare

If you want to check the list of groups associated with the current user. Only run the “groups” command without any username.

$ groups

daygeek adm cdrom sudo dip plugdev lpadmin sambashare

Method-2: How to Find Out Which Groups a User Belongs to in Linux Using the id Command

The id command stands for identity. It prints real and effective user, group, and supplementary group information such as username, UID, group names and GUID.

$ id daygeek

uid=1000(daygeek) gid=1000(daygeek) groups=1000(daygeek),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),118(lpadmin),128(sambashare)

If you want to check the list of groups associated with the current user. Only run the “id” command without any username.

$ id

uid=1000(daygeek) gid=1000(daygeek) groups=1000(daygeek),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),118(lpadmin),128(sambashare)

Method-3: How to Find Out Which Groups a User Belongs to in Linux Using the lid Command

The lid or libuser-lid command displays information about groups containing user name, or users contained in group name. This command requires sudo privileges.

You should run the libuser-lid command instead of the lid command on newer systems.

$ sudo libuser-lid daygeek

 adm(gid=4)
 cdrom(gid=24)
 sudo(gid=27)
 dip(gid=30)
 plugdev(gid=46)
 lpadmin(gid=116)
 daygeek(gid=1000)
 sambashare(gid=126)

Method-4: How to Find Out Which Groups a User Belongs to in Linux Using the getent Command

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

$ getent group | grep daygeek

adm:x:4:syslog,daygeek
cdrom:x:24:daygeek
sudo:x:27:daygeek,2gadmin
dip:x:30:daygeek
plugdev:x:46:daygeek
lpadmin:x:116:daygeek
daygeek:x:1000:
sambashare:x:126:daygeek

If you only want to print the name of a given user’s related groups, use the awk command with the above command.

$ getent group | grep daygeek | awk -F: '{print $1}'

adm
cdrom
sudo
dip
plugdev
lpadmin
daygeek
sambashare

Run the below command to print only the primary group information.

$ getent group daygeek

daygeek:x:1000:

Method-5: How to Find Out Which Groups a User Belongs to in Linux Using the /etc/group file

Using the grep command, you can easily print information about a given user’s related groups from the /etc/group file.

The grep command refers to “global regular expression print”, which prints the matching format from the file.

$ grep daygeek /etc/group

adm:x:4:syslog,daygeek
cdrom:x:24:daygeek
sudo:x:27:daygeek,2gadmin
dip:x:30:daygeek
plugdev:x:46:daygeek
lpadmin:x:116:daygeek
daygeek:x:1000:
sambashare:x:126:daygeek

If you only want to print the name of a given user’s related groups, use the awk command with the above command.

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

adm
cdrom
sudo
dip
plugdev
lpadmin
daygeek
sambashare

Method-6: How to Find Out Which Groups a User Belongs to in Linux Using the Bash Script

You can achieve this using a simple bash script.

$ for user in $(cut -d":" -f1 /etc/passwd); do groups $user; done
root : root
daemon : daemon
www-data : www-data nagcmd
saned : saned scanner
pulse : pulse audio
avahi : avahi
colord : colord
hplip : lp
geoclue : geoclue
gnome-initial-setup : nogroup
gdm : gdm
daygeek : daygeek adm cdrom sudo dip plugdev lpadmin sambashare
sshd : nogroup
thanu : thanu
renu : renu
2gadmin : 2gadmin sudo
testuser : testuser
demouser : demouser
sudha : sudha
suresh : suresh
mysql : mysql
ntp : ntp
u1 : u1
u2 : u2
u3 : u3
u4 : u4
u5 : u5
nagios : nagios nagcmd

Use the following script for a specific user.

$ for user in daygeek 2gadmin; do groups $user; done

daygeek : daygeek adm cdrom sudo dip plugdev lpadmin sambashare
2gadmin : 2gadmin sudo

Bonus Tip-1: How to Find Out All Groups in Linux Using the compgen Command

Compgen is a bash built-in command that displays all groups in the Linux system.

$ compgen -g

root
daemon
bin
sys
adm
tty
disk
lp
mail
news
uucp
.
.
gdm
daygeek
sambashare
thanu
renu
2gadmin
testuser
demouser
sudha
suresh
admin
u1
u2
u3
u4
u5
nagios
nagcmd

Bonus Tip-2: How to List Members of a Group in Linux Using the member Command

The member command allows you to list members of a group in Linux.

$ members sudo

daygeek 2gadmin

Prakash Subramanian

Prakash Subramanian is a Linux lover and has 3.5+ years of experience in linux server administration with major Linux distribution such as (RHEL, CentOS, Ubuntu). He is currently working as a Senior L2 Linux Server administrator.

You may also like...