- How to modify groups with the groupmod command in Linux
- How to modify the groups with the groupmod command in Linux
- Conclusion
- Modify Groups in Linux With groupmod Command
- Change group name with groupmod command
- Change group ID with groupmod command
- 3 basic Linux group management commands every sysadmin should know
- Great Linux resources
- groupadd
- groupmod
- groupdel
- Wrap up
- Linux containers
- Change Linux group information — groupmod
- Change Linux group information — groupmod Contents
- 1. Usage and options of «groupmod» command
- Usage
- OPTIONS
- 2. Changing the group name and GID with the groupmod command.
- 3. Files that the «groupmod» command changes.
How to modify groups with the groupmod command in Linux
In Linux, different groups are created to add the users, these groups are granted specific permissions of editing, viewing, and executing the files. These groups can be modified using the groupmod command at any instant of time. The groupmod command can only be used either by the root user or the system users by using the sudo command.
The groupmod command can be used at any instant of time to modify the group definition, in this follow-up, we will discuss the groupmod command usage in Linux.
How to modify the groups with the groupmod command in Linux
The groupmod command is used to change the definition of the group which includes its name, its GID (group identification), and the name of the group. The general syntax of using the groupmod command:
$ groupmod [options] [groupname]
The options which can be used with the groupmod command are:
Options | Explanation |
-n | It is used to change the group name |
-g | It is used to change the GID of the group |
-o | It is used to change the already assigned GID to the group |
-p | It is used to change the password of the group |
-h | It is used to display the help menu of the groupmod command |
The group details are present in the /etc/group, we can use the tail command to display the last ten lines of the file to display the groups created in Linux:
We have a list of the groups, if we want to change the name of the group, “myGroup” with “myLinuxFossGroup”, we have to use the “-n” option in the command:
$ sudo groupmod -n myLinuxFossGroup myGroup
While changing the name of the group using the “-n” flag, first type the new name which you want to assign the group and then type the name of the group of which you want to change it. Similarly, we can change the GID of the groups by using the “-g” flag, for this purpose, we will change the GID of “NewGroup” from 1003 to 1012 using the command:
$ sudo groupmod -g 1012 NewGroup
And if we want to assign the GID, which is already assigned to any group then we will use the “-o” flag, for example, if we want to assign GID 1010 to the mygroup1, which is already assigned to myNewGroup, run the command:
$ sudo groupmod -og 1010 mygroup1
Likewise, if we want to change the password of the group, we have to use the “-p” flag. For example, we change the password of “mygroup3” and write a new password “itsnewpassword”:
$ sudo groupmod -p itsnewpassword mygroup3
The password has been changed successfully. To confirm all the changes we made using the groupmod command, we will again display the file /etc/groups using the tail command:
In the above output, we can see that the name “myLinuxFossGroup” replaced the old name, “myGroup”, similarly, the “NewGroup” has been allotted the new GID that is 1012. The mygroup1 has also assigned the new id “1010” which is already being used by the myNewGroup. To get more information about the groupmod command, execute the command using the “-h” flag:
Conclusion
The groupmod command is used to modify the existing definitions of the groups in Linux which includes its name, GID, and password. This command can be used at any instant of time. There are different options that can be used with the groupmod command to implement these modifications. In this write-up, we have explained the usage of the groupmod command using its different options.
TUTORIALS ON LINUX, PROGRAMMING & TECHNOLOGY
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.
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:
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.
[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.
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.
3 basic Linux group management commands every sysadmin should know
Groups make it easy to associate users with similar access-control requirements, so managing users and groups is a key responsibility for sysadmins. As I mentioned in my article on managing users, I like simple commands with a logical syntax. Such commands are easier to remember, particularly for new administrators.
Great Linux resources
The three commands used to manage group accounts on Red Hat Enterprise Linux (RHEL) and RHEL-like distributions fit this description nicely:
As in my overview on user account administration, these concepts apply to any distribution of Linux.
Groups make it easier to associate users that have similar access control requirements. Tyler Carrigan’s article Managing local group accounts in Linux covers some of this content.
groupadd
Sysadmins are likely to add groups that represent specific and varied needs to Linux servers. Most of these needs center on controlling access to files, directories, or other resources. You can even use groups help manage sudo access.
To create a group, use the following command:
Note: These commands require root or administrative privileges.
This command adds projectA to the /etc/group file. The file contains the group name, password (if any), group ID, and a list of existing members. The group membership is empty by default. The fields are separated by colons.
There are a few options for groupadd , but a common one is —gid ( -g for short), to specify a group ID (GID) number.
[ You might also be interested in the Bash shell scripting cheat sheet. ]
groupmod
Creating a group is only the first step. You might also need to modify the group by using the groupmod command. The most common group modifications consist of name changes.
Note: Adding or removing a user from a group modifies the user, not the group, by using the usermod command.
Group modification options include:
Therefore, if you need to update a group name from projectA to projectZ, use:
$ sudo groupmod --new-name projectZ projectA
The projectA string is the command’s argument.
After modifying the group, use the tail or cat commands to check the /etc/group file contents to ensure the desired settings are in place.
groupdel
You may need to delete a group. Deleting a group does not delete the user accounts that are members of the group.
To delete the projectA group, type:
Other than the —help option for help, the other option sysadmins commonly use is —force , which forces the group deletion.
Wrap up
Linux containers
The three commands used to manage groups are quite similar to the user management commands. There are few options to remember, yet these six commands to manage users and groups are logical and straightforward.
Because user and group management are such common and important tasks for Linux sysadmins, you would be wise to learn and memorize these commands.
Change Linux group information — groupmod
You can change group information with the groupmod command. The groupmod command can change group name and GID.
Last Update : December 25, 2018
Change Linux group information — groupmod Contents
1. Usage and options of «groupmod» command
Usage
groupmod [ -g gid [-o ] ] [ -n new_group_name ] group_name
group_name is the name of the group to be changed
OPTIONS
OPTIONS | Description |
---|---|
-g | Change the GID of group_name to gid specified |
-n | Changing the group name of the specified group_name to new_group_name . |
-o | allow to change the group GID to a non-unique value. |
2. Changing the group name and GID with the groupmod command.
To change the group name with the groupmod command, use the command as follows.
[root@localhost ~]# tail -1 /etc/group before_group:x:515: ↑ before_group group exists [root@localhost ~]# groupmod -n after_group before_group ← Change group name [root@localhost ~]# tail -1 /etc/group after_group:x:515: ↑ Changed to after_group group
To change the GID with the groupmod command, do the following.
[root@localhost ~]# tail -1 /etc/group after_group:x:515: ↑ GID is 515 [root@localhost ~]# groupmod -g 520 after_group ← Change GID to 520 [root@localhost ~]# tail -1 /etc/group after_group:x:520: ↑ GID changed to 520
Changing the GID of a group does not change the group set in the existing file or directory.
[root@localhost tmp]# tail -1 /etc/group after_group:x:515: ↑ The GID of after_group is 515 [root@localhost tmp]# ls -l -rw-r--r-- 1 hoge1 after_group 0 Oct 27 03:57 test.txt ↑ The "after_group" group is set in the file [root@localhost tmp]# groupmod -g 520 after_group← Change the GID of "after_group" group to 520 [root@localhost tmp]# ls -l -rw-r--r-- 1 hoge1 515 0 Oct 27 03:57 test.txt ↑ Groups of files are not changed. The old GID of the group will be displayed.
To change the group of files and directories at once, you can do as follows.
[root@localhost ~]# find / -gid 515 -exec chgrp after_group <> \;
I am executing «chgrp» command (command to change group ownership of file) for GID 515 files and directories.
3. Files that the «groupmod» command changes.
The files that the groupmod command changes are the following two files.
Like the groupadd command, the system recognizes group changes even if you modify these files directly without using commands.
RSS 1.0 (RDF)