Linux Commands

How to Delete Groups in Linux

This tutorial explains how to manage and delete groups in Linux, as well as their function when managing users and permissions.

This document is optimized for both users looking for a fast answer, and users looking for an understanding on Linux groups. The first section shows the practical solution to the question on how Linux groups can be removed. Below it are the general guide on groups.

All steps shown in this tutorial include screenshots, making it easy for every Linux user to follow them.

How to Delete a Group in Linux

As said in the introduction, the first part of this tutorial shows how to easily delete a Linux group.

It is important to clarify. Before deleting the primary group of a user, you need to remove the user first.

The syntax to remove the Linux groups is the following:

1
sudo groupdel <group-name>

In the following example, the group to be removed is named linuxhint4. The command to delete a group is groupdel. Executing it requires privileges, so it must be preceded by sudo or executed as a root.

To delete the hypothetical group named linuxhint4, run the following command where linuxhint4 must be replaced with the group you want to delete in your system:

1
sudo groupdel linuxhint4

If you don’t get an error message, the operation was successful. But you can check the existing groups listed in alphabetical order by running the following command:

1
getent group | cut -d: -f1 | sort

What Linux Groups Are, How to Create and Manage Them

Linux groups allow us to assign permissions or accesses to multiple users by adding them to the allowed group. The purpose of Linux groups is to ease or allow organizing and managing users their allowed and restricted resources. Groups also allow administrators to share certain privileges or all of them, for example, when adding users to the sudo group.

When we define permissions over a file or directory, we define the three types of permissions: permissions for the owner, for the group to which the file belongs, and for others.

Groups information like associated users, ID, and authentication are stored in the /etc/group file. You can read this file by executing the ls command followed by the path, as shown in the following screenshot:

1
less /etc/group

When reading the /etc/group file, you will see a similar list shown in the following figure. Interpret it as a table whose columns are two dots:

Let’s take the fifth line as an example. We have four columns separated by two dots as reflected in the following image:

The following table describes each column where adm is the group name, x refers to the password stored in the /etc/passwd file, 4 refers to the group ID, and Group users lists all the group members:

adm x 4 root, linuxhint
Group name Password status Group ID Group users

Note: The /etc/shadow file referred in the second column stores the user encrypted passwords.

Creating Groups in Linux

If you want to know how to delete groups in Linux, you should also know how to add them.

The command to add the Linux groups is groupadd followed by the group name. This command also requires privileges to be executed, so it must be run as a root or preceded by sudo.

The syntax is simple:

1
sudo groupadd <Group-Name>

For this example, we will create a group named linuxhint4.

1
sudo groupadd linuxhint4

If there is no error message, the group was created successfully. But you can check it by painting the groups list in alphabetical order as done in the previous section by running the following command:

1
getent group | cut -d: -f1 | sort

As you can see in the above image, the group exists.

How to Show and Change a File Group

This part of the document explains how to change a file group. By default, the files belong to the group of the user who created it.

The sample file used in this tutorial is named LinuxHintFile. The syntax to learn a file group is the following:

1
ls -lg <FileName>

Therefore, to see the LinuxHintFile group, we run the following command:

1
ls -lg LinuxHintFile

As you can see in the previous image, the file belongs to the linuxhint group.

To change the file group, the syntax is the following:

1
sudo chgrp <GroupName> <FileName>

In this case, we want to change the LinuxHintFile group from linuxhint to linuxhint4 by running the following command:

1
sudo chgrp linuxhint4 LinuxHintFile

You can check the result by running ls again.

1
ls -lg LinuxHintFile

As you can see, the group was successfully changed.

How to Add a User to a Secondary Group

Now, the LinuxHintFile belongs to the linuxhint4 group. Let’s add the linuxhint user to the linuxhint4 group. But it is important to clarify. The linuxhint user already has his primary group and we don’t want to replace it. We want to add the user to an additional group, granting him permissions over the file belonging to the new group (linuxhint4).

The syntax is simple as shown in the following:

1
sudo usermod -a -G <GroupName> <User>

Therefore, to add the linuxhint user to the linuxhint4 group, we execute the following command:

1
sudo usermod -a -G linuxhint4 linuxhint

As shown in the /etc/group file, the user was successfully added to the group.

1
less /etc/group

Keep reading to learn how to remove users from groups.

How to Remove a User From a Group

To remove a user from a group in Linux, use the following syntax:

1
sudo gpasswd --delete <User>  <Group>

Therefore, to remove the linuxhint user from the linuxhint4 group, we run the command shown in the following figure:

1
sudo gpasswd --delete linuxhint linuxhint4

I hope our content was useful for you to learn more about managing groups in Linux.

Conclusion

As you can see, deleting, adding and managing groups in Linux is very simple. Once the users understand a group’s purpose, administering them becomes basic and helpful, especially when dealing with many users. Examples described in this article are useful for every Linux distribution. It is recommended to practice all given examples to become familiar with the explained commands. Also, it is highly recommended to read about ACL permissions for Linux systems.

Thank you for reading our tutorial showing how to delete groups in Linux and other related tips. Keep following us for more Linux professional content.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.