In this tutorial, 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:
- Group name
- Password
- Group ID (GID)
- 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 to Linux run the following command:
# 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 you need use 'groupadd' command.
The following command will add a new group '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'. We will use usermod command for this.
To add the user 'james' to the group 'opensource' run the following command:
# 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
Here,
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
Output
james alice paul
3) Using getent command
You can also use the getent
command to list users in the group.
Below is the syntax:
# getent group groupname
For example
# getent group opensource
Output
opensource:x:1005:james,paul
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 userlist.pl
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; } } if($wantedgroup) { print_group_members($wantedgroup); } else { foreach my $group (sort keys %groupmembers) { print "Group ",$group," has the following members:\n"; print_group_members($group); 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 userlist.pl
Finally, run the script
# ./userlist.pl
Sample Output
Group opensource has the following members: james paul Group paul has the following members: paul Group plugdev has the following members: ubuntu Group postfix has the following members: postfix Group proxy has the following members: proxy Group root has the following members: root Group sudo has the following members: ubuntu Group sys has the following members: sys Group syslog has the following members: syslog Group systemd-bus-proxy has the following members: systemd-bus-proxy Group systemd-network has the following members: systemd-network Group systemd-resolve has the following members: systemd-resolve Group systemd-timesync has the following members: systemd-timesync Group ubuntu has the following members: ubuntu Group uucp has the following members: uucp Group uuidd has the following members: uuidd Group video has the following members: ubuntu Group www-data has the following members: www-data
As seen above, we've been able to accomplish a lot with little effort using the shell script.
Conclusion
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. Stay here for more informative tutorials.
Read Also: