Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: June 26, 2024
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.
The basic syntax of the groupmod command is simple:
$ groupmod [options] GROUP
Let’s break down the command’s components:
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.
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.
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.
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.
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:
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.
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.
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.
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.
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.