Linux groupdel command is used to delete a group. This is a very powerful command, so use it carefully. It’s a common Linux command and you can use it in all the Linux distributions such as Ubuntu, CentOS, Debian, Fedora, etc.
Let’s look at some examples of deleting a group in Linux using groupdel command.
Linux Delete Group with No Users
For the example, I have already created few groups and a test user. Let’s look at the simple example to delete a group which has no users.
1 2 3 4 5 6 |
root@localhost:~# getent group test_users1 test_users1:x:1005: root@localhost:~# root@localhost:~# groupdel test_users1 root@localhost:~# getent group test_users1 root@localhost:~# |
Deleting a Group with Users
I have created a group test_users
and added journaldev
user to it. Let’s confirm this using the getent
and id
commands.
1 2 3 4 5 6 7 |
root@localhost:~# getent group test_users test_users:x:1004:journaldev root@localhost:~# root@localhost:~# id journaldev uid=1002(journaldev) gid=1003(journaldev) groups=1003(journaldev),27(sudo),1004(test_users),1007(test_users_pwd) root@localhost:~# |
Let’s see what happens when we delete this group.
1 2 3 4 5 |
root@localhost:~# groupdel test_users root@localhost:~# id journaldev uid=1002(journaldev) gid=1003(journaldev) groups=1003(journaldev),27(sudo),1007(test_users_pwd) root@localhost:~# getent group test_users root@localhost:~# |
The group is deleted and removed from the user groups list.
Can we Delete System Group using groupdel command?
Can we remove a system level group using groupdel command?
Let’s try to remove sudo
group using the groupdel command and see what happens.
1 2 3 4 5 |
root@localhost:~# groupdel sudo root@localhost:~# id journaldev uid=1002(journaldev) gid=1003(journaldev) groups=1003(journaldev),1007(test_users_pwd) root@localhost:~# getent group sudo root@localhost:~# |
Looks like we were able to remove the “sudo” user group. Let’s see what happens to the superuser privileges of the user.
1 2 3 4 5 6 7 8 |
root@localhost:~# su - journaldev journaldev@localhost:~$ journaldev@localhost:~$ ls /root ls: cannot open directory '/root': Permission denied journaldev@localhost:~$ sudo ls /root [sudo] password for journaldev: journaldev is not in the sudoers file. This incident will be reported. journaldev@localhost:~$ |
Looks like sudo privileges are removed too, which is obvious because we removed the “sudo” group itself.
Deleting Primary Group of a User
When we create a new user, a new group with the same name is also created and assigned to it. This is called the primary group of the user.
Let’s see if we can remove the primary group of the user?
1 2 3 |
root@localhost:~# groupdel journaldev groupdel: cannot remove the primary group of user 'journaldev' root@localhost:~# |
Let’s look at the help option to see if there is any way to overcome this error message?
1 2 3 4 5 6 7 8 9 |
root@localhost:~# groupdel -h Usage: groupdel [options] GROUP Options: -h, --help display this help message and exit -R, --root CHROOT_DIR directory to chroot into -f, --force delete group even if it is the primary group of a user root@localhost:~# |
The output clearly states that we can use the -f option to delete the primary group of the user.
1 2 3 4 |
root@localhost:~# groupdel -f journaldev root@localhost:~# id journaldev uid=1002(journaldev) gid=1003 groups=1003 root@localhost:~# |
We were able to remove the primary group of the user. But, the “gid=1003” is still showing in the user information. So, I don’t understand what is the use case for this feature.
Linux groupdel command exit values
If you are using groupdel command in a shell script, it’s better to know the exit values to check whether command executed fine or got some error.
Exit Value | Description |
0 | success |
2 | invalid command syntax |
6 | the group doesn’t exist |
8 | can’t delete user primary group |
10 | can’t update group information files |
Conclusion
Linux groupdel command is very powerful and it can remove system groups too. If you are using this command to remove a group, please take extra care because it’s irreversible and it can create major issues if the group had many users.