Commands

Modify Groups in Linux With groupmod Command

Learn how to modify group properties like group name and group ID with the groupmod command in Linux.

Abhishek Prakash
Abhishek Prakash

Table of Contents

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.

Groupmod Command

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.

[email protected]:~$ 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)

[email protected]:~$ sudo groupmod -n testgroup test_group

It ‘changes’ the group ownership of the file to reflect the new group name:

[email protected]:~$ 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.

5 Commands for Group Management in Linux
Group is an essential part of Linux system management and security. Check out various commands that you can use for managing groups in Linux.

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.

[email protected]:~$ ls -l a.txt 
-rw-r--r-- 1 abhishek testing 57 Feb 24 15:57 a.txt
[email protected]:~$ sudo groupmod -g 1020 testing
[email protected]:~$ 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.



Join the conversation.