groupmod Command Examples
In earlier articles, you learned groupadd and groupdel commands. In this tutorial, you’ll learn about the groupmod command.
The groupmod command in Linux modifies the given group by changing its name or the group ID (GID).
The syntax of the command is this:
groupmod [options] GROUP
Modifying group is an administrative task this is why you need to be a sudo user or use the root account to run this command.
I’ll use sudo in groupmod command examples here.
Change group name with groupmod command
If you want to change a group’s name, you can do that in this fashion:
sudo groupmod -n new_groupname old_groupname
The good thing here is that even though you have changed the group name, it doesn’t affect any files owned by the group.
Let’s see it by real examples.
So, here’s a file belonging to user abhishek and group test_group.
abhishek@nuc:~$ ls -l ab.txt
-rw-r--r-- 1 abhishek test_group 0 Feb 24 16:39 ab.txt
Now, if I modify the group name from test_group to testgroup (removing the underscore from the name)
abhishek@nuc:~$ sudo groupmod -n testgroup test_group
It ‘changes’ the group ownership of the file to reflect the new group name:
abhishek@nuc:~$ ls -l ab.txt
-rw-r--r-- 1 abhishek testgroup 0 Feb 24 16:39 ab.txt
Actually, the group ID remains the same even though the group name has been changed. Let’s see how to change the group ID.
Change group ID with groupmod command
You can change the GID of a group in the following manner:
sudo groupmod -g GID groupname
Now this is a risky modification and should be avoided. First, there will be no checks performed regarding the login defs like GID_MIN, GID_MAX, SYS_GID_MIN, SYS_GID_MAX values to distinguish system groups and normal groups.
Second and more important thing is that the files owned by the group will be left with group ownership by a nonexistent group.
See it in the example below where I changed the group ID of group testing to 1020 and the file owned by this group still reflects the old GID.
abhishek@nuc:~$ ls -l a.txt
-rw-r--r-- 1 abhishek testing 57 Feb 24 15:57 a.txt
abhishek@nuc:~$ sudo groupmod -g 1020 testing
abhishek@nuc:~$ ls -l a.txt
-rw-r--r-- 1 abhishek 1008 57 Feb 24 15:57 a.txt
In case you are wondering, you can see the GID in the /etc/group file. You can also use the id command to display GID, UID and other details.
I hope you have a better understanding of the groupmod command for group management in Linux. Any questions or suggestions are welcome.