The “groupmod” Command in Linux [5+ Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Sometimes, you need to change or modify the group’s own attributes. At that time, you can use the groupmod command to modify those group attributes. Here, In this article, I will describe all the features and uses of the groupmod command in Linux with multiple examples.

A. Description

The groupmod command is a useful tool to modify the attributes of an existing group. This command allows the root user or superuser to modify the group name, GID, password and other important information of an existing group. Generally, the groupmod command provides the opportunity to modify the file like /etc/group, /etc/gshadow, /etc/login.def & /etc/passwd.

Note: You must be logged in as the root user or you must use the “sudo” command to execute the “groupmod” command with root privileges.

B. Syntax

The syntax of the groupmod command in Linux is pretty simple, as shown below.

groupmod [OPTION]... GROUP_NAME
Note: In the syntax above, the OPTION enclosed by the square bracket means it is not mandatory and three dots after the square bracket mean multiple OPTIONS can be used after the groupmod command.

C. Options

There are numerous options available for the groupmod command. I have listed a few of them. You can learn more about the groupmod command, its options and their uses by checking the man page.

man groupmod

Useful Options

  • -g, –gid: Use to change the GID of a given group
  • -h, –help: Display help message
  • -n, –new-name: Use to change the name of an existing group
  • -p, –password: Enable password modification
  • -o, –non-unique: Allow changing the group GID to non-unique value
  • -R, –root CHROOT_DIR: Apply changes in the CHROOT_DIR directory and use the configuration files from the CHROOT_DIR directory.
Note: Commands and their options are case-sensitive in Linux. So, be careful while using them.

Practical Examples of the “groupmod” Command in Linux

In Linux, the groupmod command is a helpful tool to modify the group’s attributes such as group name, group ID, password etc. In the section below, I will show you some of the most useful applications for the groupmod command in Linux. Here, in this section, I am adding an image that will show you the last ten groups in my system database. And I will be working with these groups in this article.Showing the last ten groups in my system database.

Example 1: Rename the Group With the “groupmod” Command in Linux

The groupmod command in Linux allows you to modify an existing group name by simply running the groupmod command followed by the option -n. Here, I will modify a group name from “group1” having a GID 2026 to “firstgroup” with the groupmod command. You can do the same by following the steps below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

 Run the command below:

sudo groupmod -n firstgroup group1

➌ Now, press the ENTER button.

➍ Type your Password.

➎ Tap ENTER again.

➏ Now, type the following command into the command prompt:

getent group firstgroup

➐ Hit ENTER button.

Output >

In the following image, you can see that I have modified the name of group1 to firstgroup.Showing that the name of group1 is changed to firstgroup.


Similar Readings


Example 2: Modify the Group ID With the “groupmod” Command in Linux

You can modify the GID of any group by running the groupmod command with the option –gid or -g. In this case, I’ll modify group2‘s GID (which is currently 2027) and assign GID to 2036 using the groupmod command. By using the steps listed below, you can achieve the same results.

Steps to Follow >

➊ Open the Ubuntu Terminal first.

 Type the following command into the command prompt

getent group group2

In the following image, you can see the GID of group2 which is 2027.Showing the GID of group2 which is 2027.➌ Now execute the command below to alter its GID.

sudo groupmod --gid 2036 group2

Alternatively,

sudo groupmod -g 2036 group2

➍ Tap the ENTER button.

➎ Type your Password.

➏ Hit ENTER again.

➐ You can check the GID by typing the following command.

getent group group2

➑ Now, press ENTER key

Output >

You can see in the image below that I replaced the GID of group2 with the GID 2036“.Showing that the GID of group2 is replaced with the GID "2036" with groupmod command in linux.


Similar Readings


Example 3: Modify the Group ID With a Non-Unique Value

The group ID needs to be an individual, non-negative number. However, you can create groups with duplicate (non-unique) group IDs by using the -o or –non-unique option. Here, I will replace the GID of “group3 with the same GID as “group2” using the groupmod command. You can do the same by following the steps below.

Steps to Follow >

➊ Launch the Ubuntu Terminal first.

 Type the following command into the command prompt:

getent group group2 group3

➌ Press the ENTER button.

In the following image, you can see that the group named group2 and group3 have GID values of 2036 and 2028, respectively.Showing that the group named group2 and group3 have GID values of 2036 and 2028, respectively.➍ Now, enter the following command into the command prompt:

sudo groupmod -o -g 2036 group3

Alternatively,

sudo groupmod --non-unique -g 2036 group3

➎ Type your Password.

➏ Press ENTER again.

➐ You can check the GID by typing the following command.

getent group group2 group3

➑ Press the ENTER key.

Output >

In the image below, you can see that I have changed the GID of group3 with the same GID as group2.Showing that the GID of group3 has changed with the same GID as group2 with groupmod command in linux.


Similar Readings


Example 4: Update the Group Password Using the “groupmod” Command in Linux

The groupmod command allows you to set a new password or change the existing password for a group. In this section, first I will show you the process to set a new password for a group and then I will change the existing password with a new one. To learn the whole process, stay with me til the end.

Case A: Set Password of a Group Without Having One

There is a group in my system named “group4” with no password. To check the password, you can run the command below.

sudo grep group4 /etc/gshadow

After executing the command, you will get the result shown in the following image.Showing that the group4 have no password.At this point, I will add a password to group4. And you can do the same by following the steps below.

Steps to Follow >

➊ First, open the Ubuntu Terminal.

 Type the following command into the command prompt:

sudo groupmod -p abcdef123 group4

Alternatively,

sudo groupmod --password abcdef123 group4

➌ Now, press the ENTER button.

➍ Type your Password.

➎ Tap ENTER again.

➏ Now type the following command into the command prompt:

sudo grep group4 /etc/gshadow

➐ Press ENTER key.

Output >

You can see that I have set a password to the group named “group4”.A password has been set for group4.

Case B: Change the Existing Password of a Group

The process of changing the password is pretty similar to setting a password for a group. First, I will show you that group4 already has an existing password by executing the command below.

sudo grep group4 /etc/gshadow

After executing the command, you will get the result shown in the following image.Showing the current password of group4.Now, I will show you the steps to changing the password for group4. You can follow the steps below to do the same.

Steps to Follow >

➊ Launch the Ubuntu Terminal.

 Type the following command into the command prompt:

sudo groupmod -p ghijklm123 group4

Alternatively,

sudo groupmod --password ghijklm123 group4

➌ Now, tap the ENTER button.

➍ Type your Password.

➎ Tap ENTER again.

➏ Now type the following command into the command prompt:

sudo grep group4 /etc/gshadow

➐ Press ENTER key.

Output >

You can see in the image below that I have changed the previous password of the group named “group4”.Showing the process of changing password of group4 with groupmod command in linux.


Similar Readings


Example 5: Get Help About the “groupmod” Command in Linux

In Linux, the help page for the groupmod command contains a brief description of the command. To see the help page, follow the steps that I mentioned below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

 Then, type the following command into the command prompt:

groupmod --help

➌ Now, press the ENTER button.

Output >

In the image below, you can see the help page for the groupmod command in Linux.Showing the help page for the groupmod command in linux.

Conclusion

In this article, I’ve discussed the groupmod command, its options and its usage. At the same time, I’ve provided some relevant examples. That will help you better understand the groupmod command and its use. Consequently, it will make your journey easier to become a power user of the Linux command line.


Similar Readings

Rate this post
S. M. Amdadul Islam

Hello everyone. I am S. M. Amdadul Islam, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Naval Architecture and Marine Engineering (NAME) graduate from the Bangladesh University of Engineering and Technology. In addition to my regular work, I enjoy watching Youtube, discovering new things, gossiping with friends, visiting new places, and taking photos of landscapes. Read Full Bio

Leave a Comment