Linux Commands

How to Add a User to a Group in Rocky Linux 9

Groups are used to organize the users to define their access rights in the system. Adding a user to a group helps you share the resources (files and services) and offers a streamlined approach to managing the users.

As an administrator, adding a user to the group is also beneficial to maintain security. However, if you are unfamiliar with how to add users to a group, we recommend you to read this guide completely. In this guide, we will explain the simple methods to add the users to a group in Rocky Linux 9 (RHEL-based OS).

How to Add a User to a Group in Rocky Linux 9

In Rocky Linux, there are multiple ways to add the users to a group. But if you want to create a group first, run the following command:

sudo groupadd group

Make sure that you replace the “group” with the name that you want to use. For example, we create a group named “LinuxTeam”:

sudo groupadd LinuxTeam

If you want to list all the available groups in the system, you can run the following command:

getent

As you can see in the previous image, there are 5 groups: LinuxTeam, LinuxTeam_Devs, LinuxTeam_Managers, LinuxTeam_Interns, and LinuxTeam_SEO. If you want to get a simplified result, execute this command:

getent group | cut -d: -f1

Now, we have multiple users in the system. Let’s list them all through the following command:

getent

or

getent group | cut -d: -f1

If you want to add a user to a group, run the following command:

sudo usermod -a -G LinuxTeam Mackle

In the previous command, the -a option ensures that the users must be added to a group without removing them from the existing one.

Similarly, you can use the “gpasswd” command to manage the groups’ passwords and memberships. Here is the example command that you can try:

sudo gpasswd -a Ron LinuxTeam_SEO

If you want to add a single user in multiple groups, you can run the following command:

sudo usermod -a -G <group1>,<group2>,<group3> <username>

Remove a User from a Group

If you want to remove a user from a group, run the following command:

sudo gpasswd -d <username> <group_name>

Conclusion

This tutorial is all about adding a user to a group in Rocky Linux 9. This guide works like a complete information to understand everything about adding and removing groups in Linux. Remember to use the previous commands correctly. Otherwise, you may get errors while adding users to groups.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.