Delete every users from a group

In general you shouldn't delete a group because some files may belong to that group but it may be sufficient for your specific sudo usecase.

A more general usecase is to use gpasswd (1) at it allows you to set the members of a specific group, so it should be enough to run:

gpasswd sudo -M ''

To only run this if the group sudo exists you can combine it with a getent call, i.e.:

getent group sudo && gpasswd sudo -M ''

There's another way using command /usr/sbin/groupmems ("administer members of a user's primary group") on systems that have this command:

groupmems --purge --group <group_to_clean>

or using short options:

groupmems -p -g <group_to_clean>

All members will be removed from the group.

Tested in CentOS and Fedora.

Tags:

Bash

Users

Group