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.
B. Syntax
The syntax of the groupmod command in Linux is pretty simple, as shown below.
groupmod [OPTION]... GROUP_NAME
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.
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.
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.
Similar Readings
- The “time” Command in Linux [4 Practical Examples]
- The “tty” Command in Linux [4 Practical Examples]
- The “uptime” Command in Linux [5 Practical Examples]
- The “vmstat” Command in Linux [6 Practical Examples]
- The “uname” Command in Linux [11 Practical Examples]
- The “apt-get” Command in Linux [10 Practical Examples]
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.➌ 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“.
Similar Readings
- The “free” Command in Linux [8 Practical Examples]
- The “getent” Command in Linux [11 Practical Examples]
- The “groupadd” Command in Linux [7 Practical Examples]
- The “addgroup” Command in Linux [7 Practical Examples]
- The “passwd” Command in Linux [7 Practical Examples]
- The “firewall-cmd” Command in Linux [7 Practical Examples]
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.➍ 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.
Similar Readings
- The “groups” Command in Linux [6 Practical Examples]
- The “id” Command in Linux [7+ Practical Examples]
- The “service” Command in Linux [6 Practical Examples]
- The “sestatus” Command in Linux [4 Practical Examples]
- The “shutdown” Command in Linux [7 Practical Examples]
- The “finger” Command in Linux [6 Practical Examples]
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.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”.
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.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”.
Similar Readings
- The “dmesg” Command in Linux [7+ Practical Examples]
- The “install” Command in Linux [6+ Practical Examples]
- The “reboot” Command in Linux [3 Practical Examples]
- The “sync” Command in Linux [8 Practical Examples]
- The “chage” Command in Linux [7 Practical Examples]
- The “env ” Command in Linux [9 Practical Examples]
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.
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
- The “sudo” Command in Linux [8 Practical Examples]
- The “df” Command in Linux [11 Practical Examples]
- The “apt” Command in Linux [13+ Practical Examples]
- The “top” Command in Linux [8 Practical Examples]
- The “htop” Command in Linux [7 Practical Examples]
- The “enable” Command in Linux [6 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners