Linux Commands – groupmod
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed groupadd command in Linux which is used to create a new group in Linux.
https://cloudaffaire.com/linux-commands-groupadd/
In this blog post, we will discuss groupmod command in Linux. groupmod command can be used to modify an existing group attribute. The groupmod command modifies the definition of the specified GROUP by modifying the appropriate entry in the group database. You need root or sudo privileges to modify a group.
Linux Commands – groupmod:
You can use groupmod -n or –new-name NEW_GROUP options to rename an existing group.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
############################### ## Linux Commands | groupmod ## ############################### ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ##--------- ## groupmod ##--------- ## groupmod [options] group ## groupmod -n or --new-name NEW_GROUP options sudo groupadd mygroup1 ## create a group named mygroup1 sudo grep "mygroup*" /etc/group ## returns mygroup1:x:1001: sudo groupmod -n mygroup2 mygroup1 ## rename mygroup1 to mygroup2 sudo grep "mygroup*" /etc/group ## returns mygroup2:x:1001: |
You can use groupmod -g GID or –gid GID options to change the group id of an existing group. The value of GID must be a non-negative decimal integer and must be unique, unless the -o option is used. Users who use the group as primary group will be updated to keep the group as their primary group. Any files that have the old group ID and must continue to belong to GROUP, must have their group ID changed manually. No checks will be performed with regard to the GID_MIN, GID_MAX, SYS_GID_MIN, or SYS_GID_MAX from /etc/login.defs.
1 2 3 4 5 6 7 8 9 |
## groupmod -g GID or --gid GID options sudo groupadd mygroup1 ## create a group named mygroup1 sudo grep "mygroup*" /etc/group ## returns mygroup1:x:1002: ## mygroup2:x:1001: sudo groupmod -g 1003 mygroup1 ## change the GID of mygroup1 from 1002 to 1003 sudo grep "mygroup*" /etc/group ## returns mygroup1:x:1003: ## mygroup2:x:1001: |
You can use groupmod -o or –non-unique options to change the group GID to a non-unique value when used with the -g option.
1 2 3 4 5 6 7 8 9 10 11 |
## groupmod -o or --non-unique options sudo grep "mygroup*" /etc/group ## returns mygroup1:x:1003: ## mygroup2:x:1001: sudo groupmod -g 1001 mygroup1 ## groupmod: GID '1001' already exists sudo groupmod -o -g 1001 mygroup1 ## changes the GID of mygroup1 from 1003 to 1001 sudo grep "mygroup*" /etc/group ## returns mygroup1:x:1001: ## mygroup2:x:1001: |
You can use groupmod -p or –password PASSWORD options to set or change a password to a group. Please note that group password is not secure and can be listed.
1 2 3 4 5 6 7 8 9 |
## groupmod -p or --password PASSWORD options sudo groupmod --password mypwd mygroup1 ## set new password for mygroup1 sudo grep "mygroup1" /etc/gshadow ## returns mygroup1:mypwd:: sudo groupmod --password mynewpwd mygroup1 ## reset password for mygroup1 sudo grep "mygroup1" /etc/gshadow ## returns mygroup1:mynewpwd:: |
You can use groupdel command to delete a group. In order to delete a group using groupdel command, the group must exist and should not be the sole primary group of a user. If the group is the sole primary group of a user, then you need to change the primary group to another group. The groupdel command modifies the system account files, deleting all entries that refer to GROUP. You should also manually check all file systems to ensure that no files remain owned by this group.
1 2 3 4 |
## groupdel command sudo groupdel mygroup1 ## delete both the groups sudo groupdel mygroup2 |
Hope you have enjoyed this article. In the next blog post, we will discuss useradd command in Linux.