Popular Searches

Manage Local Groups and Membership

LinuxBeginner
Practice Now

Introduction

Linux uses groups to share access and administrative responsibility. A user has one primary group and can also belong to supplementary groups. Those supplementary memberships become important later when you work with shared directories, delegated sudo rules, ACLs, and service access.

In this lab, two practice users, opsalice and opsbob, are prepared for you. You will inspect their current groups, create a new local group named projectops, add users to it, compare group records with id output, remove memberships, and delete the group. This lab focuses on group management rather than repeating the full user lifecycle from the previous lab.

Inspect Initial Group Membership

In this step, you will inspect group membership before changing it. This gives you a baseline, which is important because group changes are easier to understand when you can compare before and after output.

Move into the LabEx project directory.

cd /home/labex/project

Use id to inspect opsalice. The uid field identifies the user. The gid field is the primary group. The groups section lists all group memberships.

id opsalice

Inspect opsbob in the same way.

id opsbob

The id -nG form prints only group names. At this point, each practice user should belong to their own primary group and should not belong to projectops.

id -nG opsalice
id -nG opsbob

Create a Local Group

In this step, you will create a local group. The groupadd command adds a group record to the local group database. Local groups are commonly stored in /etc/group, but, as with users, getent group is the safer query command because it asks the configured group database.

Create the projectops group.

sudo groupadd projectops

Inspect the group record.

getent group projectops

The group record uses colon-separated fields:

group-name:password-placeholder:GID:member-list

At this point, the member list should be empty because you created the group but have not added any users to it.

Add a Supplementary Group Member

In this step, you will add opsalice to projectops as a supplementary group member. A supplementary group does not replace the user's primary group. It adds another group that can be used for shared file access or delegated permissions.

Use usermod -aG to append a supplementary group. The -G option sets supplementary groups, and the -a option means append. Use them together when you want to add one group without removing existing supplementary memberships.

sudo usermod -aG projectops opsalice

Check the user view of membership.

id opsalice

Check the group view of membership. The final field should now include opsalice.

getent group projectops

Add Another Member with gpasswd

In this step, you will add a second user with gpasswd. While usermod is user-centered, gpasswd is group-centered. It is useful when you are thinking about managing the membership list of one group.

Add opsbob to projectops.

sudo gpasswd -a opsbob projectops

Inspect both users. Each user should now show projectops in the group list, but their primary groups remain their own user-private groups.

id opsalice
id opsbob

Inspect the group record. The member list should now include both opsalice and opsbob.

getent group projectops

Remove Membership and Delete the Group

In this step, you will remove users from the group and then delete the group. Removing a user from a supplementary group does not delete the user account. Deleting the group removes the group record, but it also must not be the primary group of an existing user.

Remove opsbob from projectops.

sudo gpasswd -d opsbob projectops

Inspect the group record. At this point, opsalice should still be listed and opsbob should be gone.

getent group projectops

Remove opsalice too, so the group is empty before deletion.

sudo gpasswd -d opsalice projectops

Delete the group.

sudo groupdel projectops

Confirm the group is gone.

getent group projectops || echo "projectops removed"

Confirm the user accounts still exist. Removing a supplementary group and deleting that group should not remove the users.

id opsalice
id opsbob

Summary

You practiced the local group workflow that follows naturally after user management. You inspected initial memberships, created a local group, added users as supplementary members with both usermod -aG and gpasswd -a, removed memberships, and deleted the group while leaving the user accounts intact. These skills prepare you for later labs on shared permissions, sudo delegation, ACLs, and service access.