How to modify groups with the groupmod command in Linux

In Linux, different groups are created to add the users, these groups are granted specific permissions of editing, viewing, and executing the files. These groups can be modified using the groupmod command at any instant of time. The groupmod command can only be used either by the root user or the system users by using the sudo command.

The groupmod command can be used at any instant of time to modify the group definition, in this follow-up, we will discuss the groupmod command usage in Linux.

How to modify the groups with the groupmod command in Linux

The groupmod command is used to change the definition of the group which includes its name, its GID (group identification), and the name of the group. The general syntax of using the groupmod command:

$ groupmod [options] [groupname]

The options which can be used with the groupmod command are:

OptionsExplanation
-nIt is used to change the group name
-gIt is used to change the GID of the group
-oIt is used to change the already assigned GID to the group
-pIt is used to change the password of the group
-hIt is used to display the help menu of the groupmod command

The group details are present in the /etc/group, we can use the tail command to display the last ten lines of the file to display the groups created in Linux:

$ sudo tail /etc/group

We have a list of the groups, if we want to change the name of the group, “myGroup” with “myLinuxFossGroup”, we have to use the “-n” option in the command:

$ sudo groupmod -n myLinuxFossGroup myGroup 

While changing the name of the group using the “-n” flag, first type the new name which you want to assign the group and then type the name of the group of which you want to change it. Similarly, we can change the GID of the groups by using the “-g” flag, for this purpose, we will change the GID of “NewGroup” from 1003 to 1012 using the command:

$ sudo groupmod -g 1012 NewGroup

And if we want to assign the GID, which is already assigned to any group then we will use the “-o” flag, for example, if we want to assign GID 1010 to the mygroup1, which is already assigned to myNewGroup, run the command:

$ sudo groupmod -og 1010 mygroup1

Likewise, if we want to change the password of the group, we have to use the “-p” flag. For example, we change the password of “mygroup3” and write a new password “itsnewpassword”:

$ sudo groupmod -p itsnewpassword mygroup3

The password has been changed successfully. To confirm all the changes we made using the groupmod command, we will again display the file /etc/groups using the tail command:

$ sudo tail /etc/group

In the above output, we can see that the name “myLinuxFossGroup” replaced the old name, “myGroup”, similarly, the “NewGroup” has been allotted the new GID that is 1012. The mygroup1 has also assigned the new id “1010” which is already being used by the myNewGroup. To get more information about the groupmod command, execute the command using the “-h” flag:

$ sudo groupmod -h

Conclusion

The groupmod command is used to modify the existing definitions of the groups in Linux which includes its name, GID, and password. This command can be used at any instant of time. There are different options that can be used with the groupmod command to implement these modifications. In this write-up, we have explained the usage of the groupmod command using its different options.