How to Add User to Group on CentOS

Groups on CentOS

On a CentOS system, groups are used to manage user access to system resources. A group is a collection of users that share common access permissions to resources such as files, directories, and devices.

Groups are created and managed with the groupadd, groupmod, and groupdel commands. Each user on the system is a member of at least one group and can be a member of additional groups. By default, a user is a member of the group with the same name as the user’s username.

Groups are used to control access to resources through the use of permissions. When you create a file or directory, you can assign permissions to the file or directory that specify which users and groups have access to the resource. For example, you can give read and write permissions to the group developers for a specific directory, allowing all members of the developers group to read and write to the directory.

Groups are also used to manage access to certain system commands and settings. For example, the sudo command allows users in the wheel group to execute commands with root privileges.

How to Add User to Group on CentOS

To add a user to a group on a CentOS system, you can use the usermod command. Here’s an example of the basic syntax:

usermod -a -G group_name username

This command will add the user username to the group group_name. The -a option tells usermod to append the user to the group, rather than replacing the user’s current group membership.

Here’s an example of how to use usermod to add the user john to the group developers:

usermod -a -G developers john

To make the change effective, you may need to log out and log back in or reboot the system.

Alternatively, you can use the gpasswd command to add a user to a group. The syntax for gpasswd is similar to usermod, but you need to specify the group name with the -a option, like this:

gpasswd -a username group_name

For example, to add the user john to the group developers using gpasswd, you would run the following command:

gpasswd -a john developers

Again, you may need to log out and log back in or reboot the system to make the change effective.