How to Delete Group in Linux Using groupdel Command

How to Delete Group in Linux Using groupdel Command

In Linux, groups are collections of user that share some common attributes, like permissions or access to certain resources.

Groups are an important functionality because, for example, they enable assigning privileges on groups of users, and these privileges will apply to every user in a particular group.

This makes it much easier to manage user attributes, than having to assign them individually.

Groups can be managed in a variety of ways, such as creating, viewing existing groups, displaying user and group information, adding/removing users from groups or deleting groups.

At times, some groups may become unnecessary and can be removed.

In this tutorial we’ll explain how to delete groups using the groupdel command.

Before You Begin

You cannot remove the primary group of an existing user. In order to remove the group you will have to first remove the user.

Before removing a group you should check for any files that might still be owned by that group.

groupdel Command Syntax

The syntax for groupdel is:

groupdel [OPTIONS] [GROUP_NAME]

The groupdel command has a few options that are rarely used. You can find them in the groupdel man page.

To remove a group you need root access or sudo privileges. Going forward with the tutorial we’ll assume you’re acting either as root or a **non-root sudo user**.

Delete a Group Using the groupdel Command

Deleting groups is straightforward. We just need to run the groupdel command, followed by a group name.

The following command will remove the staff group:

sudo groupdel staff

If the group was successfully deleted there won’t be any output.

If you don’t have permissions you will get an error:

groupdel: Permission denied.
groupdel: cannot lock /etc/group; try again later.

The successful run of groupdel alters two files – /etc/group and /etc/gshadow.

Check if a group still exists using the following command:

getent group | grep staff

If the group doesn’t exist then usually output will be shown.

If a group exists you’ll get output containing some information on the group:

bytexd_group:x:1007:bytexd_user

groupdel Exit Codes

groupdel exits with the following codes:

0 success
2 invalid command syntax
6 specified group doesn’t exist
8 can’t remove user’s primary group
10 can’t update group file

Conclusion

In this tutorial you learned how to delete groups using the groupdel command in Linux.

This command should work on any Linux distribution, including Ubuntu, Debian, Linux mint, CentOS, RockyLinux, Fedora, etc.

If you have any questions or issues then feel free to leave us a comment and we’ll get back to you as soon as we can.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like