How to create and delete groups in Linux with groupadd and groupdel

How to create and delete groups in Linux with groupadd and groupdel

In this article we are going to learn Group Management  commands 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 in to a Group, Delete User from a Group and so on. The main database file of group is /etc/group and it takes default settings like GID information from /etc/login.defs.

The Linux Commands for Group Management are :

  1. groupadd : Linux groupadd command is used to create a new group.
  2. groupdel : Used to delete a Group
  3. groupmod : Used to Modify a already created group.
  4. gpasswd : Used to Perform administrative task such as Add user in to 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 developers.

[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:

Also Read : 

Create a System Group

To create a System Group use Linux groupadd command with argument -r.

Now you might thinking that what is the difference between normal group and system group. The only difference is GID of normal group starts from 500 and above and System groups from 1 to 499.

As you can see below 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 Group’s with same GID Number. to do so use groupadd command with argument -o. Here I have created some groups i.e developers (GID – 501), admins (GID – 555), workers (GID – 493). Now I am going to create a new group by using 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 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 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 same.

[root@localhost ~]# cat /etc/group | grep admins
admins:x:493:

So to Rename a group refer 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 a already created Group

We can create Multiple Groups with 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 GID of Group workers and will assign the GID of group engineers. Refer 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:

Change GID of a Group

groupmod is used to modify a already created group.

To change GID of a 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 in to a Group

Now our next group management command is gpasswd.

You can add user in to a group using gpasswd command with argument -a. Here I am adding the user elinuxbook in group developers.

[root@localhost ~]# gpasswd -a elinuxbook developers   # Adding user in to a Group
Adding user elinuxbook to group developers
[root@localhost ~]# cat /etc/group | grep developers
developers:x:501:elinuxbook

Delete/Remove User from a Group

To delete or remove a user from a group you can use gpasswd with argument -d. Here I am removing the user elinuxbook from group developers.

[root@localhost ~]# gpasswd -d elinuxbook developers   # Remove user from a Group
Removing user elinuxbook from group developers
[root@localhost ~]# cat /etc/group | grep developers
developers:x:501:

Add Multiple Users in to a Group

You can add multiple users in to a group using gpasswd command with argument -M. Here I am adding users i.e. elinuxbook, user1 and user2 in group developers.

[root@localhost ~]# gpasswd -M elinuxbook,user1,user2 developers  # Adding Multiple users in to a Group
[root@localhost ~]# cat /etc/group | grep developers
developers:x:501:elinuxbook,user1,user2

Set Password for a Group

We can set password for a Group using gpasswd command. Refer 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 a administrator of Group. To do so you can use gpasswd command with argument -A. Here I am making user elinuxbook as a administrator of Group developers.

[root@localhost ~]# gpasswd -A elinuxbook developers  # Make a User as a Group Administrator

We tried to include as much possible group management commands. If something missedout then please feel free to comment on comment box below. So that we can include that in the article.

If you found this article useful then Like Us, Share Us, OR if you have something to say then feel free to comment on the comment box below.

Latest News:

 
Enjoyed this video?
How to create and delete groups in Linux with groupadd and groupdel
"No Thanks. Please Close This Box!"