How To List The Members Of A Group In Linux – OSTechNix

This tutorial explains different ways to find and list all groups and list the members of a group in Linux and Unix-like operating systems.

Introduction

All users in a Linux system must be a member of at least one group. This group is known as Primary group. If an user doesn’t have a primary group, he/she can’t able to login.

Apart from the primary group, the users can be a member of additional groups as well. The primary group setting is stored in /etc/passwd file.

The primary group’s name is specified in the 4th field of this file. The settings of other(secondary) groups are stored in /etc/group file.

List all groups in Linux

Before listing all users belongs to a group, let us first find the list of available groups using compgen command. Compgen is BASH built-in to  manipulate the programmable completion facilities.

To list all available groups in a Linux system, run compgen command with -g option like below:

$ compgen -g

Sample output:

root
daemon
bin
sys
adm
tty
disk
lp
mail
news
uucp
man
proxy
.
.
.
nova
rdma
libvirt
libvirt-qemu
libvirt-dnsmasq
cockpit-ws
cockpit-wsinstance
libvirtdbus
root
nogroup

You can also get the list of all groups using getent command:

$ getent group

Sample output:

root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,sk
tty:x:5:syslog
disk:x:6:sk
lp:x:7:
mail:x:8:
news:x:9:
.
.
.
nova:x:134:
rdma:x:135:
libvirt:x:136:sk,libvirtdbus
libvirt-qemu:x:64055:libvirt-qemu
libvirt-dnsmasq:x:137:
cockpit-ws:x:138:
cockpit-wsinstance:x:139:
libvirtdbus:x:998:
root:x:0:
nogroup:x:65534:

The first field in the /etc/group file is the name of the group. So, we can use awk or cut commands to print only the first field that contains the group’s name like below:

$ getent group | awk -F: '{ print $1}'
$ getent group | cut -d: -f1

If you want to view the groups page by page, use “more” or “less” command like below:

$ more /etc/group 
$ less /etc/group 

Now, let us check the members of a group.

List the members of a Group in Linux

There are a few ways to find the group members in Linux. The methods we used here to identify the members of a group are given below:

  1. using /etc/group file,
  2. using getent command,
  3. using groupmems command,
  4. using members command,
  5. using lid command.

1. List the members of a group using /etc/group file

When a group is created, the group’s information is stored in the /etc/group file. Let us take a look at the contents of this file using cat command:

$ cat /etc/group

Sample output:

root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,sk
tty:x:5:syslog
disk:x:6:sk
lp:x:7:
mail:x:8:
news:x:9:
.
.
.
systemd-coredump:x:999:
nova:x:134:
rdma:x:135:
libvirt:x:136:sk,libvirtdbus
libvirt-qemu:x:64055:libvirt-qemu
libvirt-dnsmasq:x:137:
cockpit-ws:x:138:
cockpit-wsinstance:x:139:
libvirtdbus:x:998:

As I mentioned earlier, the first field in /etc/group is reserved for the name of the group. As you can see in the above output, a Linux system may contain several groups.

To view the members of a specific Group in a Linux machine, use grep command to filter the group details from the /etc/group file like below:

$ grep '^sudo' /etc/group

Or,

$ grep -w sudo /etc/group

The above commands displays the users belongs to the group named sudo.

sudo:x:27:sk

As you see in the above output, the sudo group contains only one member named “sk”.

2. View the members of a group using getent command

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

To find the members of a given group in Linux using getent command, run:

$ getent group sudo

This command displays members of the “sudo” group.

One notable advantage of getent command is it not only lists the local users but all users in all configured userdb backends, for example LDAP, on a given system.

3. Print users in a group using groupmems command

The groupmems command allows a user to administer their own group membership list without superuser privileges. It is part of the shadow utils package.

To print the members of a group using groupmems command, run:

$ sudo groupmems -g sudo -l

This command has two drawbacks. It only deals with groups in /etc/group file but not the ones in LDAP or other user databases. It also requires superuser privileges as it tries to open /etc/gshadow file.

4. Display group members using members command

As the name explicitly says, the members commands basically displays the members of a Group in Linux.

The members command is not available by default in most Linux distributions. You need to install it using your distribution’s package manager.

For example, on Debian-based systems, you can install it using command:

$ sudo apt install members

Once installed, you can display the members of a given group in Linux using members command like below:

$ members sudo

By default, the members command displays all users. Use -p or -s to display only the members of a primary group or secondary group.

5. List all users belongs to a group using libuser-lid command

The libuser-lid command used to displays information about groups containing user name, or users contained in group name.

The libuser-lid command is also not available by default in many Linux distributions. The libuser provides this command, so you need to install it using your distribution’s package manager.

On Debian, Ubuntu, install libuser package using command:

$ sudo apt install libuser

To list all users in a group, run:

$ sudo libuser-lid -g sudo

Please note that this command requires superuser privileges to run.

Sample output:

 sk(uid=1000)

For more details, refer the manual pages of the respective command.

These are a few different methods to find the list of users in a Group in Linux. Hope this helps.

Related Read:

Alternate Text Gọi ngay