Introduction
In this lab, we will explore the Linux groups command, which is used to manage user groups. We will learn how to view the groups a user belongs to, create new groups, and add or remove users from groups. The lab covers understanding the groups command, creating and managing user groups, and assigning and removing users from groups. The commands covered in this lab are commonly used for user and permission management in Linux environments.
Understand the Linux groups Command
In this step, we will explore the Linux groups command, which is used to manage user groups. The groups command allows you to view the groups a user belongs to, create new groups, and add or remove users from groups.
First, let's check the groups the current user labex belongs to:
groups labex
Example output:
labex : labex sudo
The output shows that the user labex is a member of the labex and sudo groups.
To view all the groups on the system, you can use the following command:
cat /etc/group
Example output:
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:labex
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:labex
floppy:x:25:
tape:x:26:
sudo:x:27:labex
audio:x:29:
dip:x:30:
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:labex
staff:x:50:
games:x:60:
users:x:100:
nogroup:x:65534:
systemd-journal:x:101:
systemd-network:x:102:
systemd-resolve:x:103:
systemd-timesync:x:104:
input:x:105:
crontab:x:106:
netdev:x:107:
syslog:x:108:
messagebus:x:109:
render:x:110:
ssh:x:111:
lxd:x:112:labex
This shows all the groups defined on the system, including their group ID and the users that belong to each group.
Create and Manage User Groups
In this step, we will learn how to create and manage user groups in Linux using the groupadd, groupmod, and groupdel commands.
First, let's create a new group called "developers":
sudo groupadd developers
Example output:
No output, but the group "developers" is now created.
To verify the group was created, we can list all the groups again:
cat /etc/group
You should see the "developers" group in the output.
Now, let's add the user "labex" to the "developers" group:
sudo usermod -a -G developers labex
To confirm the user was added to the group, we can check the groups the user belongs to:
groups labex
Example output:
labex : labex sudo developers
If you need to modify the properties of a group, you can use the groupmod command. For example, to change the group name from "developers" to "engineering":
sudo groupmod -n engineering developers
Finally, to delete a group, use the groupdel command:
sudo groupdel engineering
Example output:
No output, but the "engineering" group is now deleted.
Assign and Remove Users from Groups
In this final step, we will learn how to assign and remove users from groups in Linux.
First, let's create a new group called "testers":
sudo groupadd testers
Now, let's add the user "labex" to the "testers" group:
sudo usermod -a -G testers labex
To verify the user was added to the group, we can check the groups the user belongs to:
groups labex
Example output:
labex : labex sudo developers testers
If you need to remove a user from a group, you can use the gpasswd command:
sudo gpasswd -d labex testers
Let's verify the user has been removed from the "testers" group:
groups labex
Example output:
labex : labex sudo developers
You can also use the usermod command to remove a user from a group:
sudo usermod -G labex,developers labex
This will remove the user "labex" from all groups except "labex" and "developers".
Summary
In this lab, we first learned about the Linux groups command, which allows us to view the groups a user belongs to, create new groups, and add or remove users from groups. We then explored how to create and manage user groups using the groupadd, groupmod, and groupdel commands. Finally, we covered how to assign and remove users from groups using the usermod command.
The key learning points from this lab include understanding the purpose and usage of the groups command, creating and modifying user groups, and managing user membership within those groups. These skills are essential for effectively administering user access and permissions in a Linux environment.



