1. Overview

The groupmod command is commonly used in Linux to modify existing group attributes. It allows system administrators to change features such as name or group ID (GID), which helps ensure simple and successful user management.

In this article, we’ll explore the common options and practical examples of the groupmod command. This will help us manage groups effectively in our Linux systems.

2. groupmod Command Syntax and Common Options

The basic syntax of the groupmod command is simple:

$ groupmod [options] GROUP

Let’s break down the command’s components:

  • [options]: specific options we use to modify the group’s attributes
  • GROUP: the name of the group to be modified

The groupmod command provides several options for managing and modifying group attributes:

Options

Description

-n

Changes the name of the group

-g

Changes the group ID

-o

Allows using duplicate group ID

-h

Displays help information about groupmod command usage

-p

Sets the group password to PASSWORD (encrypted)

-U

Lists members of a group

With these options, we can apply the groupmod command to modify group attributes when necessary.

3. Common groupmod Examples

Now, let’s explore a few practical examples of using the groupmod command with different options. It’s also crucial to always apply sudo before each command because the process requires root privileges.

3.1. Renaming a Group

First, we can use the groupmod command to rename an existing group. We can achieve this by using the -n option, then the new group name and the current group name.

For example, let’s rename an existing group called jupiter:

$ sudo groupmod -n newgroupname jupiter

After this command executes, the group formerly known as jupiter will be named newgroupname.

3.2. Changing the GID of a Group

In scenarios where we need to change the group ID (GID) of an existing group, we need to combine the -g option with the new GID and the current group name.

For example, let’s change the GID of our group:

$ sudo groupmod -g 11 newgroupname

After the command executes, the GID of the group changes to 11.

3.3. Setting a Group Password

The groupmod command can also aid in setting an encrypted password for a group. We use the -p option for this task.

However, before proceeding, let’s look at the general syntax for setting a group password:

$ sudo groupmod -p encrypted_password groupname

Let’s explain the command’s components:

  • sudo: runs the command with root privileges
  • groupmod: the command used to modify group properties
  • -p: specifies that the argument will be the encrypted password to set for the group
  • encrypted_password: the encrypted password to be set for the group
  • groupname: the name of the group for which the password is being set

Now that we understand the general syntax for setting a group password, let’s generate an encrypted password for the group using the openssl command:

$ openssl passwd -1

Password: 
Verifying - Password: 
$1$xrsO9bNT$pdObpKYcrS2MRt3unNk6X.

This command prompts us to input and verify the password of our choice, outputting an encrypted version at the end.

Further, let’s set the group’s password using the encrypted password generated:

$ sudo groupmod -p '$1$xrsO9bNT$pdObpKYcrS2MRt3unNk6X.' newgroupname 

After running this command, the encrypted password becomes the group password. Notwithstanding, while it’s possible to set a group’s password using the -p option, doing so is not ideal. That’s because the password can be visible to users listing processes or command histories on the system.

3.4. Allowing Duplicate GIDs

If we need our groups to have non-unique GIDs, we can achieve this by using the -o option along with the -g option

For example, let’s see how to allow duplicate GID with the groupmod command:

$ sudo groupmod -o -g 1001 newgroupname

In effect, the GID of the group newgroupname is set to 1001, even if another group already has this GID.

3.5. Appending New Users to a Group

Through the groupmod command, we can append new users specified by the -U option to an existing group.

For example, let’s append a user to a group:

$ sudo groupmod -a -U nancy newgroupname

This command successfully adds the user nancy to the group newgroupname.

3.6. Setting New Users for a Group

Alternatively, we can remove all current users and set new users for a group using the groupmod command. To achieve this, we use the -U option without -a.

For example, let’s replace the existing users of a group with new users:

$ sudo groupmod -U linda newgroupname

Consequently, the user linda automatically becomes the only member of the group, replacing any existing members.

4. Conclusion

In this article, we looked into different aspects of the groupmod command in Linux. We covered its fundamental purpose of modifying group attributes and provided practical examples of its application.

Ultimately, getting a grasp of how it works is crucial, as it enables us to efficiently manage group properties within Linux systems.