In this article, we are going to learn the Group Management command in Linux. We have Four useful commands in Linux to Manage Groups i.e. groupadd, groupdel, groupmod, gpasswd. Managing Groups means we have to perform tasks such as Create Group, Delete Group, Modify Created Group, Add User into a Group, Delete User from a Group, and so on. The main database file of the group is /etc/group and it takes default settings like GID information from /etc/login.defs.
The Linux Commands for Group Management are :
- groupadd : Linux groupadd command is used to create a new group.
- groupdel : Used to delete a Group
- groupmod : Used to Modify an already created group.
- gpasswd : Used to Perform administrative tasks such as Add user into a group, Remove User from a Group, etc.
Follow the below Group Management Commands:
Create a New Group using Linux groupadd command
Let’s start with our first group management command i.e. groupadd.
To create a new group we can use Linux groupadd command. Here I am creating a group developer.
[root@localhost ~]# groupadd developers # Create a New Group [root@localhost ~]# cat /etc/group | grep developers # Confirm the Setting developers:x:501:
Delete a Group
To delete a group we can use groupdel command. Here I am deleting the group developers.
[root@localhost ~]# groupdel developers # Delete a Group
Create a Group with your own GID (Group ID)
You can assign a GID (Group ID) of your own choice or as per your scenario. To do so you can use groupadd command with argument -g. Here I am creating a group with GID 555.
[root@localhost ~]# groupadd -g 555 admins # Assign a GID to a New Group [root@localhost ~]# cat /etc/group | grep admins admins:x:555:
Create a System Group
To create a System Group use Linux groupadd command with argument -r.
Now you might be thinking that what is the difference between a normal group and a system group. The only difference is GID of the normal group starts from 500 and above and the System groups from 1 to 499.
As you can see below the GID of our system group is 493 which is below 500.
[root@localhost ~]# groupadd -r workers # Create a System Group [root@localhost ~]# cat /etc/group | grep workers workers:x:493:
Create a Group with Duplicate GID (Group ID)
We can create multiple Groups with the same GID Number. to do so use groupadd command with argument -o. Here I have created some groups of i.e developers (GID – 501), admins (GID – 555), workers (GID – 493). Now I am going to create a new group by using the GID of admins. As you can see below the GID of group admins is 555.
[root@localhost ~]# tail -n 3 /etc/group developers:x:501: admins:x:555: workers:x:493:
So to create a group with duplicate GID refer to the below command.
[root@localhost ~]# groupadd -o -g 555 engineers # Create a Group with duplicate GID # Confirm the Changes [root@localhost ~]# cat /etc/group | grep 555 admins:x:555: engineers:x:555:
For more help on groupadd command, you can refer to the below command.
[root@localhost ~]# groupadd --help # For more help on groupadd command Usage: groupadd [options] GROUP Options: -f, --force exit successfully if the group already exists, and cancel -g if the GID is already used -g, --gid GID use GID for the new group -h, --help display this help message and exit -K, --key KEY=VALUE override /etc/login.defs defaults -o, --non-unique allow to create groups with duplicate (non-unique) GID -p, --password PASSWORD use this encrypted password for the new group -r, --system create a system account
Rename a Group
After Linux groupadd command our next group management command is groupmod.
To rename a group you can use groupmod command with argument -n. Here I a have group named admins with GID 493 and I am renaming the group from admins to workers but the GID will be the same.
[root@localhost ~]# cat /etc/group | grep admins admins:x:493:
So to Rename a group refers to the below command.
Syntax : groupmod -n [new name] [old name]
[root@localhost ~]# groupmod -n workers admins # Rename a Group [root@localhost ~]# cat /etc/group | grep workers workers:x:493:
Assign a Duplicate GID (Group ID) to an already created Group
We can create Multiple Groups with the same GID. Here I have some group as shown below.
[root@localhost ~]# tail -n 4 /etc/group helpdesk:x:500: developers:x:501: engineers:x:555: workers:x:557:
Now let’s change one of the groups GID and assign some other Groups ID. We can do so by using groupmod command with argument -o. Here I am going to change the GID of Group workers and will assign the GID of group engineers. Refer to the command below.
[root@localhost ~]# groupmod -o -g 555 workers # Create Multiple Groups with same GID [root@localhost ~]# tail -n 4 /etc/group helpdesk:x:500: developers:x:501: engineers:x:555: workers:x:555:
Also Read – Managing Users And Groups In Linux – A Complete Guide For Beginners
Change GID of a Group
groupmod is used to modify an already created group.
To change the GID of an already created group you can use groupmod command with argument -g. Here I am changing the GID of group workers.
[root@localhost ~]# groupmod -g 557 workers # Change GID of a already created Group [root@localhost ~]# cat /etc/group | grep workers workers:x:557:
For more Help on groupmod command, you can use below command.
[root@localhost ~]# groupmod -h # For more help on groupmod command. Usage: groupmod [options] GROUP Options: -g, --gid GID change the group ID to GID -h, --help display this help message and exit -n, --new-name NEW_GROUP change the name to NEW_GROUP -o, --non-unique allow to use a duplicate (non-unique) GID -p, --password PASSWORD change the password to this (encrypted) PASSWORD
Add User into a Group
Now our next group management command is gpasswd.
You can add users into a group using gpasswd command with argument -a. Here I am adding the user itsmarttricks in group developers.
[root@localhost ~]# gpasswd -a itsmarttricks developers # Adding user in to a Group Adding user itsmarttricks to group developers [root@localhost ~]# cat /etc/group | grep developers developers:x:501:itsmarttricks
Delete/Remove User from a Group
To delete or remove a user from a group you can use gpasswd with the argument -d. Here I am removing the user itsmarttricks from group developers.
[root@localhost ~]# gpasswd -d itsmarttricks developers # Remove user from a Group Removing user itsmarttricks from group developers [root@localhost ~]# cat /etc/group | grep developers developers:x:501:
Add Multiple Users into a Group
You can add multiple users into a group using gpasswd command with argument -M. Here I am adding users i.e. itsmarttricks, user1, and user2 in group developers.
[root@localhost ~]# gpasswd -M itsmarttricks,user1,user2 developers # Adding Multiple users in to a Group [root@localhost ~]# cat /etc/group | grep developers developers:x:501:itsmarttricks,user1,user2
Set Password for a Group
We can set a password for a Group using gpasswd command. Refer to the command below.
[root@localhost ~]# gpasswd developers # Set Password for a Group Changing the password for group developers New Password: Re-enter new password:
Make a Group Member as a Group Administrator
You can make a user as an administrator of Group. To do so you can use gpasswd command with argument -A. Here I am making user itsmarttricks as an administrator of Group developers.
[root@localhost ~]# gpasswd -A itsmarttricks developers # Make a User as a Group Administrator
That’s all, In this article, we have explained Group Management Command In Linux (Groupadd, Groupdel, Groupmod, Gpasswd). I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.