Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I search a clean way to delete every users from the 'sudo' group. On several distributions the user created during the installation process has sudo rights, I don't want this. I search for an automated method working for (nearly) every situations.

Basically, I want to empty a group.

To remove every users : GROUP=my_group; for u in $(getent group $GROUP | sed -e 's/^.*:.*:.*://' -e 's/,/ /g'); do echo gpasswd --delete $u $GROUP; done
This command line works :
1. if the group doesn't exist
2. if the group is empty
3. if the group contains one user
4. if the group contains several users
So everything is okay. But is their something simplier ?

Another way is to delete the group, and recreate it. Ugly ?

share|improve this question
How about just deleting the group and then recreating it? – jordanm Dec 18 '12 at 19:32
Either make sure you have the root password set, or better leave at least one user in the sudo group that can keep this privilege. Also make sure you have an extra open root shell when experimenting with this, so you can always revert your changes. – jippie Dec 18 '12 at 19:35
Consider updating /etc/login.defs with your preferred defaults for future new users. – jippie Dec 18 '12 at 19:39
up vote 3 down vote accepted

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 ''
share|improve this answer
So simple. Thanks ! – Gregory MOUSSAT Dec 18 '12 at 20:09
Does this cover cases where sudo is a primary group for some users? – Nils Dec 21 '12 at 21:57
@Nils no as gpasswd is used to manage /etc/group but the primary group is specified in /etc/passwd – Ulrich Dangel Dec 21 '12 at 23:23

There's another simplier way with /usr/sbin/groupmems

groupmems -p -g group_to_clean

All members will be removed from the group.

Tested in CentOS.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.