- Delete Groups in Linux With groupdel Command
- Using groupdel command in Linux
- Keep in mind: You cannot delete primary group of a user without deleting the user
- Keep in mind: Files belonging to the deleted group will not change group ownership
- How to create, delete, and modify groups in Linux
- Training & certification
- Create and modify groups
- Change the group ID
- Great Linux resources
- Rename a group
- Add and remove users from a group
- Linux: How to Remove a Group in Ubuntu Linux Server 16.04 LTS
- Description
- Syntax
- Parameters or Arguments
- Prerequisites
- Remove a Group
- How to Delete Groups with groupdel Command in Linux
- How to Delete Groups with groupdel Command in Linux
- How to Delete a Group Using groupdel Command
- Conclusion
- About the author
- Kashif
Delete Groups in Linux With groupdel Command
Learn how to delete a group in Linux using groupdel command. Also learn what to do with files owned by the deleted groups.
In an earlier article, I discussed the groupadd command which is used for adding new groups to your Linux system.
In this article, I’ll discuss another group management command which is groupdel. It is used for deleting groups from your system.
Using groupdel command in Linux
The groupdel command is perhaps the simplest command in Linux with virtually no options (other than one for chroot).
All you have to do is to specify the group name. The group must exist, of course.
Did you notice that I used sudo with the groupdel command? It’s because it is an administrative command and you need to be root or sudo user to run this command.
How do you get the group name? You can list groups for a user or manually check all the groups present in your system by viewing the content of file /etc/group.
Keep in mind: You cannot delete primary group of a user without deleting the user
If you try to remove the primary group of an existing user, you should see an error like this:
groupdel: cannot remove the primary group of user ‘abhishek’
This is by design. You cannot delete the primary group of an existing user. You have two options here:
- Change the primary group of a user using usermod command
- Delete the user using userdel command and then delete the group
Keep in mind: Files belonging to the deleted group will not change group ownership
The groupdel command doesn’t change the group ownership of any files. It simply removes the group name from the system. If you list files owned by a deleted group, you’ll see the group id (GID) instead of group name.
I hope you are aware of the file permission and ownership concept in Linux. As you can see below, the file belongs to group tesla:
[email protected]:~$ ls -l a.txt -rw-r--r-- 1 abhishek tesla 57 Feb 24 15:57 a.txt
Once I delete the group, the file shows the group ID (998 in the example) instead of the group name:
[email protected]:~$ sudo groupdel tesla [email protected]:~$ ls -l new.txt -rw-r--r-- 1 abhishek 998 1717 Feb 24 15:58 new.txt
What do you do with such files, then? It’s up to you really. You can leave it like that or use a combination of find, xargs and chgrp command to change the group of files belonging to the GID of the deleted group like this:
find . -gid 998 | xargs chgrp another_group_name
Since we are speaking of file permissions and ownership, you might want to know about the stat command that can display these information in detail.
I hope this tutorial gave you a better understanding of the groupdel command in Linux. If you have any questions or suggestions, please let me know in the comments.
How to create, delete, and modify groups in Linux
Groups are an essential part of the Linux permission structure and a powerful way to manage file access on your system.
In Linux, groups are collections of users. Creating and managing groups is one of the simplest ways to deal with multiple users simultaneously, especially when dealing with permissions. The /etc/group file stores group information and is the default configuration file.
[ Keep your most commonly used commands handy with the Linux commands cheat sheet. ]
Linux admins use groups to assign access to files and other resources. Every group has a unique ID listed in the /etc/group file, along with the group name and members. The first groups listed in this file are system groups because the distribution maintainers preconfigure them for system activities.
Each user may belong to one primary group and any number of secondary groups. When you create a user on Linux using the useradd command, a group with the same name as the username is also created, and the user is added as the group’s sole member. This group is the user’s primary group.
Training & certification
Create and modify groups
To add a group in Linux, use the groupadd command:
When a group is created, a unique group ID gets assigned to that group. You can verify that the group appears (and see its group ID) by looking in the /etc/group file.
If you want to create a group with a specific group ID (GID), use the —gid or -g option:
$ sudo groupadd -g 1009 demo1
If group ID 1009 is already allocated to another group, you’re alerted that the GID is unavailable and the operation aborts. Rerun it with a different group ID number:
$ sudo groupadd -g 1010 demo1
Change the group ID
You can change the group ID of any group with the groupmod command and the —gid or -g option:
$ sudo groupmod -g 1011 demo1
Great Linux resources
Rename a group
You can rename a group using groupmod with the —new-name or -n option:
$ sudo groupmod -n test demo1
Verify all these changes from the /etc/group file.
Add and remove users from a group
Suppose you have existing users named user1 and user2, and you want to add them to the demo group. Use the usermod command with the —append —groups options ( -a and -G for short):
$ sudo usermod --append --groups demo user1 $ sudo usermod -aG demo user2
Look in the /etc/group file or use the id command to confirm your changes:
$ id user1 uid=1005(user1) gid=1005(user1) groups=100(users),1009(demo)
To remove a specific user from a group, you can use the gpasswd command to modify group information:
$ sudo gpasswd --delete user1 demo
Alternatively, manually edit the /etc/group file and remove the user from any number of groups.
Linux: How to Remove a Group in Ubuntu Linux Server 16.04 LTS
In this Linux system administration tutorial you will learn how to remove a group on an Ubuntu Linux Server 16.04 LTS (Xenial Xerus) system with screenshots and instructions.
Description
Groups allow system administrators to apply certain permissions or access to groups of users. It is sometimes necessary to remove a group from a system when it is no longer needed.
Before a group can be removed, any users assigned to the group should be reassigned to another group. To remove a group, you will use the groupdel command.
Syntax
The syntax for the groupdel command is:
Parameters or Arguments
The groupdel command requires the following parameter:
Group_Name The name of the group you would like to remove. Group names should be entered in lowercase and may contain underscores.
Prerequisites
To complete this tutorial you will require a running Ubuntu Linux Server 16.04 LTS system and an account with sudo administrative privileges. The sudo command is used to provide the superuser privileges required for the groupdel command.
Remove a Group
The following steps will guide you through removing a group on an Ubuntu Linux Server 16.04 LTS system.
IMPORTANT: Certain pre-installed groups are required for the system to operate properly. Do NOT remove a group from a system unless you are sure it is not needed!
- To remove an existing group from your system, you will need to be logged in using a valid user account. If you are unsure of how to do this, read our tutorial on Logging into Ubuntu Linux Server 16.04 LTS. In this tutorial, we have logged in as techonthenet on the host called ubuntu.
- Now that we’re logged in, we can remove the group with a Group Name of professors by entering the following groupdel command:
This screenshot demonstrates what you will see. When you have typed in the command, press the Enter key to execute the command.
sudo grep professors /etc/group
The following screenshot demonstrates what the command will look like after it is typed. When you have typed the command, press the Enter key to execute the command.
How to Delete Groups with groupdel Command in Linux
The “groupdel” command in Linux removes the account from Linux. It provides a way for administrators to remove a group and its associated permissions, making it no longer accessible for users or processes.
Using this command, we can manage and delete users that we no longer need. Here we will create a group of users and then delete it using the groupdel command.
How to Delete Groups with groupdel Command in Linux
To list all the groups run below-mentioned command:
Here we can see group1 displayed which we just created.
How to Delete a Group Using groupdel Command
Now to delete a specific group we will use the below-given command. For example, to delete the group1 from list run the groupdel command:
Once the group is deleting again, list all groups to confirm the deletion. Now again run the /etc/group command:
Here we can see the group1 is successfully removed using the groupdel command.
For example, if a certain group doesn’t delete, the “-f” flag will forcefully delete this group. To delete group1 using the “-f” flag, run the below command:
The grep command can also confirm whether a certain group is deleted or not.
If no output is displayed on the terminal, it means the group is already deleted.
For more info about groupdel run the given command:
Conclusion
The Linux “groupdel” command is a powerful tool for managing group accounts on a Linux system. It enables administrators to remove groups that are no longer needed, ensuring the security and efficiency of the system by reducing the number of unneeded access controls.
About the author
Kashif
I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.