Managing groups in Linux is essential for organizing users and controlling access to system resources. In this session, we’ll explore how to create, manage, and delete groups, using the developers group in a company as our real-life example.
To create a new group, use the groupadd command.
$ sudo groupadd <groupname>Example:
If you want to create a group called developers, you would run:
$ sudo groupadd developersTip
Real-Life Scenario:
In a company, when a new development project is initiated, you might create a developers group to manage the team members working on this project.
The /etc/group file lists all groups and their members on your system. Each line follows this format:
group_name:password:GID:user_list
- group_name: The name of the group (e.g.,
developers). - password: Usually empty, but can be used to set a group password.
- GID: The Group ID number, which is a unique identifier for the group.
- user_list: Comma-separated usernames of group members.
Example:
developers:x:1005:atharv,ritesh
This means:
developersis the group name.xindicates no password is set.1005is the Group ID.atharvandriteshare members of thedevelopersgroup.
The groups command shows which groups a user belongs to.
$ groups <username>Example:
To see which groups atharv belongs to:
$ groups atharv
atharv : atharv developersTip
Real-Life Scenario:
If you need to verify which teams or projects an employee is part of, you can use this command to check that atharv is part of the developers group.
The id command provides detailed information about a user’s UID (User ID) and GID, including the groups they belong to.
$ id <username>Example:
To check the IDs and groups for yash:
$ id yash
uid=1004(yash) gid=1004(yash) groups=1004(yash)Tip
Real-Life Scenario:
If you’re reviewing a team member’s profile to see their roles and associated groups, this command will show that yash is not a member of the developers group, among others.
There are several ways to add users to a group:
You can manually edit the /etc/group file to add a user.
Steps:
- Open
/etc/groupwith a text editor. - Locate the line for the group you want to modify.
- Add the username to the end of the line, separated by a comma.
Example:
To add yash to the developers group, modify the line:
developers:x:1001:atharv,ritesh
to:
developers:x:1001:atharv,ritesh,yash
You can use the usermod command to add a user to a group.
Command:
$ sudo usermod -a -G <groupname> <username>Example:
To add yash to the developers group:
$ sudo usermod -aG developers yash # -aG = -a -GTip
Real-Life Scenario:
When Atharv, the team lead, needs to include yash in the development team for a new project, he would use this command to ensure yash is part of the developers group.
The groupmod command can modify group properties.
Command:
$ sudo groupmod [options] <groupname>Options:
-a, --append– Add users without removing existing members.-g, --gid GID– Change the group ID.-n, --new-name NEW_GROUP– Rename the group.-U, --users USERS– List the users in the group.
Example:
To rename the group developers to software_engineers:
$ sudo groupmod -n software_engineers developersThe gpasswd command manages group memberships and administrative tasks.
$ sudo gpasswd [options] <groupname>Common Options and Examples:
-
Add a User to a Group:
$ sudo gpasswd -a <username> <groupname>
Example: To add
yashto thedevelopersgroup:$ sudo gpasswd -a yash developers
-
Remove a User from a Group:
$ sudo gpasswd -d <username> <groupname>
Example: To remove
riteshfrom thedevelopersgroup:$ sudo gpasswd -d ritesh developers
-
Make a User an Admin of the Group: You can use
gpasswdto manage administrative tasks, although making a user an admin is often handled through specific configurations or policies.
Example:
To make atharv an admin of the developers group (if applicable):
$ sudo gpasswd -A atharv developersTip
Real-Life Scenario:
Atharv, as the senior developer or team lead, might be given administrative privileges to manage the developers group. This allows him to add or remove team members and handle group-related administrative tasks.
To remove a group that is no longer needed, use the groupdel command.
$ sudo groupdel <groupname>Example:
To delete the developers group:
$ sudo groupdel developersTip
Real-Life Scenario:
If the developers team is restructured and the group is no longer needed, you would delete it to maintain system organization.