How to Delete a Group in Linux
This post will guide you how to delete or remove a group from the command line in your Linux operating systems. How do I delete a group in your current Linux system using groupdel or delgroup command.
- Groupdel Command
- Delgroup Command
- Display All Group Names
- Delete a Group using Groupdel Command
- Delete a Group using delgroup command
- Print the Groups of a User
- Remove the User from a Group
Groupdel Command
The groupdel command modifies the system account files, deleting all entries that refer to GROUP. The named group must exist.
The syntax for the groupdel command is as followss:
sudo groupdel [options] GROUPNAME
Note: you can not remove the primary group of any existing user. And you must remove the user before you remove the group. You should manually check all file systems to ensure that no files remain owned by this group.
Delgroup Command
Delgroup command also can be used to remove a group from the system according to command line options and configuration information in /etc/deluser.conf and /etc/adduser.conf.
SYNOPSIS deluser [options] [--force] [--remove-home] [--remove-all-files] [--backup] [--backup-to DIR] user deluser --group [options] group delgroup [options] [--only-if-empty] group deluser [options] user group
Display All Group Names
Before removing a particular group from your system, and you can view all group names in your Linux system by issuing the following command:
$ cat /etc/group
Outputs:
devops@devops:~$ cat /etc/group root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4:syslog,devops tty:x:5: disk:x:6: lp:x:7: mail:x:8: news:x:9: uucp:x:10: man:x:12: proxy:x:13: kmem:x:15: dialout:x:20: fax:x:21: voice:x:22: cdrom:x:24:devops floppy:x:25: tape:x:26: sudo:x:27:devops audio:x:29:pulse dip:x:30:devops www-data:x:33: backup:x:34: operator:x:37: list:x:38: irc:x:39: src:x:40: gnats:x:41: shadow:x:42: utmp:x:43: video:x:44:plex sasl:x:45: plugdev:x:46:devops staff:x:50: games:x:60: users:x:100: nogroup:x:65534: systemd-journal:x:101: systemd-network:x:102: systemd-resolve:x:103: input:x:104: crontab:x:105: syslog:x:106: messagebus:x:107: netdev:x:108: mlocate:x:109: ssl-cert:x:110: uuidd:x:111: avahi-autoipd:x:112: bluetooth:x:113: rtkit:x:114: ssh:x:115: lpadmin:x:116:devops whoopsie:x:117: scanner:x:118:saned saned:x:119: pulse:x:120: pulse-access:x:121: avahi:x:122: colord:x:123: geoclue:x:124: gdm:x:125: devops:x:1000: sambashare:x:126:devops mysql:x:127: lxd:x:128:devops wildfly:x:999: couchdb:x:129: plex:x:130: cassandra:x:131: ntp:x:132: rdma:x:133: mydev:x:1002: mytest2:x:1003: mytest3:x:1004: mytest4:x:1005: incron:x:134: mytest5:x:1006: mytest1:x:1008: mytest6:x:1009: mytest:x:1010: devops@devops:~$
Delete a Group using groupdel command
If you want to remove a group called mytest in your Linux system,and you can use a command called groupdel. just type the following command:
$ sudo groupdel mytest
Delete a Group using delgroup command
You can use another command called delgroup to delete a group called mytest from your Linux system, just type the following command:
$ sudo delgroup mytest
Print the Groups of a User
If you want to print group memberships for a given user, and you can use one of the following commands:
$ groups UserName $ id UserName $ groups mytest $ id mytest
Outputs:
devops@devops:~$ groups mytest mytest : mytest mygroup1 devops@devops:~$ id mytest uid=1006(mytest) gid=1010(mytest) groups=1010(mytest),1011(mygroup1)
Remove the User from a Group
If you want to delete a user from a given group in your Linux system, and you can use the deluser command, the syntax is as followss:
$ sudo deluser UserName GroupName $ sudo deluser mytest mygroup1
Outputs:
devops@devops:~$ sudo deluser mytest mygroup1 Removing user `mytest' from group `mygroup1' ... Done. devops@devops:~$ id mytest uid=1006(mytest) gid=1010(mytest) groups=1010(mytest) devops@devops:~$ groups mytest mytest : mytest
From the above outputs, you would see that the user called mytest has been removed from a group called mygroup.
Conclusion
You should know that how to use groupdel or delgroup command to delete or remove a group in CentOS or RHEL or Ubuntu Linux server.