In Linux, there is number of users and groups in the system who use and operate the system. The group is nothing but a logical mechanism to manage a collection of users.
There are two types of groups in Linux:,
- Primary Group
- Secondary Group
The Primary group is the main group that the operating system assigns to files that are created by the user. Each user must belong to a primary group.
The Secondary group is any group other than your primary group. Users can belong to up to 15 secondary groups.
If you are a Linux system administrator then it is an essential part of you to find how to list users of a specific group.
In this tutorial, we will show you how to find and list all members of a group in Linux.
Basic Syntax of Groups Command
The basic syntax of groups command is shown below:
groups [OPTION]... [USERNAME]...
You can display all the groups available in your system with the following command:
groups
You should get the following output:
vyom adm cdrom sudo dip plugdev lpadmin sambashare vboxusers chrome-remote-desktop
List All Groups Available in Your System
The simple and easiest way to list all the group available in your system with the following command:
cat /etc/group
You should see the following output:
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,vyom
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:
news:x:9:
uucp:x:10:
man:x:12:
proxy:x:13:
kmem:x:15:
dialout:x:20:
fax:x:21:
voice:x:22:
cdrom:x:24:vyom,user1,user2
floppy:x:25:
tape:x:26:
sudo:x:27:vyom
audio:x:29:pulse
dip:x:30:vyom
www-data:x:33:
backup:x:34:
operator:x:37:
list:x:38:
irc:x:39:
src:x:40:
gnats:x:41:
shadow:x:42:
utmp:x:43:
video:x:44:
sasl:x:45:
plugdev:x:46:vyom
staff:x:50:
games:x:60:
users:x:100:
nogroup:x:65534:
libuuid:x:101:
netdev:x:102:
crontab:x:103:
syslog:x:104:
fuse:x:105:
messagebus:x:106:
ssl-cert:x:107:postgres
lpadmin:x:108:vyom
scanner:x:109:saned
mlocate:x:110:
ssh:x:111:
utempter:x:112:
avahi-autoipd:x:113:
rtkit:x:114:
saned:x:115:
whoopsie:x:116:
avahi:x:117:
lightdm:x:118:
nopasswdlogin:x:119:
bluetooth:x:120:
colord:x:121:
pulse:x:122:
pulse-access:x:123:
vyom:x:1000:
sambashare:x:124:vyom
vboxusers:x:125:vyom
postgres:x:126:
docker:x:999:
mysql:x:127:
chrome-remote-desktop:x:128:vyom
snmp:x:129:
user1:x:1001:
user2:x:1002:
You can also see the sample output in the following screen:
You can list all available groups with the following commands:
getent group
Or
more /etc/group
Or
less /etc/group
List all Members of a Group with Command Line
There are several ways you can list all members of a specific group. In this section, we will show you different methods to find all members of a group.
List all Members of a Group with the /etc/group File
The /etc/group is a file that contains information about all groups. You can use cat with grep command as shown below:
cat /etc/group | grep -i groupname
For example, if you want to list all members of a group named cdrom, run the following command:
cat /etc/group | grep -i cdrom
You should see the following output:
cdrom:x:24:vyom,user1,user2
You can also list all members of a group named cdrom using the awk command as shown below:
awk -F':' '/cdrom/{print $4}' /etc/group
You should see the following output:
vyom,user1,user2
You can also see the sample output in the following screen:
List all Members of a Group with members Command
You can also print all members of a group with members command. By default, members command is not installed in most Linux operating systems. You can install it by running the following command:
apt-get install members -y
After installing members package, you can all members of a specific group with the following command:
members groupname
For example, to display all the members of a group named cdrom run the following command:
members cdrom
You should see the following output:
vyom user1 user2
List all Members of a Group with lid Command
lid is an another command-line utility that can be used to provide information about groups containing usernames.
By default, lid utility is not installed in most Linux operating systems. You can install it by just running the following command:
apt-get install libuser -y
Once installed, you can display all users in the group named cdrom with the following command:
lid -g cdrom
You should see the following output:
vyom(uid=1000)
user1(uid=1001)
user2(uid=1002)
With lid command, you can also see the information of all groups that contain user named vyom as shown below:
lid vyom
You should see the following output:
adm(gid=4)
cdrom(gid=24)
sudo(gid=27)
dip(gid=30)
plugdev(gid=46)
lpadmin(gid=108)
vyom(gid=1000)
sambashare(gid=124)
vboxusers(gid=125)
chrome-remote-desktop(gid=128)
You can also see the sample output in the following screen:
Conclusion
In the above tutorial, we learned how to find and list all users of a specific group in Linux. We have also learned different commands to find all the members of a group with examples. I hope you have now enough knowledge to use these commands in the production environment.