How To Change User Group In Linux?

Linux users are assigned to different groups to get different permissions. A single user may have single or multiple groups. In this tutorial, we examine how to change user groups in Linux operating system.

Linux Group Types

There are two types of Linux Groups. First is the primary group which is generally the same as the user. The primary group is used when the user creates a file or folder the primary group is assigned as the group for the created file or folder. The second group type is the second group which is mainly used to provide permission for other files and folders to access.

Display User Groups

A user may have single or more groups. These groups can be listed by using the groups command like below.

$ groups
ismail adm cdrom sudo dip plugdev lpadmin lxd sambashare wireshark

Change Primary Group

The primary group can be changed with the usermod command. The -g option is used to provide the primary group name to be set. The syntax of the usermod command is like below.

usermod -g PRIMARY_GROUP USERNAME
  • PRIMARY_GROUP is the group name we want to change.
  • USERNAME is the user we want to change.

In the following example, we set the user “ismail” secondary group as “admin”.

$ usermod -g admin ismail

Change Secondary (Other) Group

The secondary group of a user can be changed using the -G option. This is also called changing another group of users. The -G option is used to specify the secondary group to add for the user. Also, the -a option is used to add a new secondary group. In the following example, we set the secondary group of the “ismail” as “editor”.

$ usermod -a -G admin ismail

Change Multiple Groups

The usermod command can be also used to set multiple secondary groups for a user. The multiple group names are provided with the -G option of separating all group names with a comma. In the following example, we set multiple user groups for the user “ismail”.

$ usermod -a -G admin,editor,superadmin ismail
See also  How To Install Python3 On Ubuntu?

Leave a Comment