Linux List Group Members in Linux Terminal

linux list group members
In this article, we'll look at different ways of how you can list members of a group in Linux.  The /etc/group text file stores group information. There's one entry per line containing the following information

  1. Group name
  2. Password
  3. Group ID (GID)
  4. Group users' list

To get a picture of what we are talking about, we are going to create new users and later add them to a group called opensource.

Adding a new user

To add a new user run

# adduser
You'll be prompted to enter the usernanme passsword and other details such as Phone number. For instance , let's add a new user called Andrew
addser andrew
Adding user `andrew' ...
Adding new group `andrew' (1001) ...
Adding new user `andrew' (1004) with group `andrew' ...
Creating home directory `/home/andrew' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for andrew
Enter the new value, or press ENTER for the default
        Full Name []: andrew james
        Room Number []: 45
        Work Phone []: 555-456
        Home Phone []: 987-764
        Other []:
Is the information correct? [Y/n] Y

Using the same command and procedure, we can add more users, in this case, James,  Alice, and Paul.

Adding a new group

To add a new group run

# groupadd

We'll add a new group called opensource

# groupadd opensource

To confirm the group exists in /etc/group run

# grep -i "opensource" /etc/group

Adding users to the group

Now, let's add the newly created users to the group opensource. The syntax for this is

 # usermod -aG

In our case to add users to our group we shall run the command below and repeat it for other users as well.

# usermod -aG opensource james

How to list members of a group

1) Using cat /etc/group

As we saw earlier, group information is stored in /etc/group . To display this information run

# cat /etc/group

You'll get a list of system-defined groups and the group we created earlier

# opensource:x:1005:james,alice,paul

opensource is the group name
x represents the encrypted password
1005 represents the group ID (GID)
James, Alice, Paul represents the users existing in the group.

2) Using members command

You can use members command to list users in a group. The syntax for this is

# members groupname

In this example, we'll have

members opensource


james alice paul

3) Using getent command

You can also use the getent command to list users in the group as shown

# getent group groupname

For example

# getent group opensource



4) Using a perl script

Finally, you can list all groups in your Linux system and display all the members in those groups using a perl script as shown.

First, create the script using your favorite text editor

# vim
Copy and Paste this script and Save
#!/usr/bin/perl -T
# Lists members of all groups, or optionally just the group
# specified on the command line.
use strict; use warnings;

$ENV{"PATH"} = "/usr/bin:/bin";

my $wantedgroup = shift;

my %groupmembers;
my $usertext = `getent passwd`;

my @users = $usertext =~ /^([a-zA-Z0-9_-]+):/gm;

foreach my $userid (@users)
my $usergrouptext = `id -Gn $userid`;
my @grouplist = split(' ',$usergrouptext);

foreach my $group (@grouplist)
$groupmembers{$group}->{$userid} = 1;

foreach my $group (sort keys %groupmembers)
print "Group ",$group," has the following members:\n";
print "\n";

sub print_group_members
my ($group) = @_;
return unless $group;

foreach my $member (sort keys %{$groupmembers{$group}})
print $member,"\n";

Save and exit.

Give the script execute permissions

# chmod +x

Finally run the script

# ./

Sample Output

Group opensource has the following members:

Group paul has the following members:

Group plugdev has the following members:

Group postfix has the following members:

Group proxy has the following members:

Group root has the following members:

Group sudo has the following members:

Group sys has the following members:

Group syslog has the following members:

Group systemd-bus-proxy has the following members:

Group systemd-network has the following members:

Group systemd-resolve has the following members:

Group systemd-timesync has the following members:

Group ubuntu has the following members:

Group uucp has the following members:

Group uuidd has the following members:

Group video has the following members:

Group www-data has the following members:

As seen above, we've been able to accomplish a lot with little effort using the shell script.

Read Also:

In this brief tutorial, we've shown you basic commands alongside a perl script that you can use to conveniently display groups and members of those groups. We're glad you took time with us. Keep it here for more informative tutorials.

Winnie Ondara 5:29 am


Your email address will not be published. Required fields are marked *

All comments are subject to moderation.