-   Slackware (
-   -   Canonical way to remove a user from one or several groups? (

kikinovak 11-05-2012 02:17 AM

Canonical way to remove a user from one or several groups?

This is one basic manipulation I always seem to forget. Here goes.

To add a user to a group or a series of groups, I normally use usermod with the -a (append) switch like this:


# usermod -a -G group1,group2 user
Let's say I want to add user joe to the lp group, I'd do:


# usermod -a -G lp joe
To add him to the lp and scanner groups, the command would be:


# usermod -a -G lp,scanner joe
Now what's the canonical way to remove a user from a group or a series of groups? This is the sort of command I rarely use and always seem to forget.

Didier Spaier 11-05-2012 02:43 AM

gpasswd -d<user> <group>

AFAIK this works only for a single group.

Else if you know the list of groups whom the user (should) belong(s)s to just write (without the -a option):

usermod -G <list of groups not including the one to remove> user

EDIT I use 'gpasswd -a<user> <group>' to add an user to a single group as well.

PS if you want to change the list of groups proposed when you create a new user with the /usr/sbin/adduser Slackware command, just edit the value of the AGID variable (line #110 in Slackware 13.37). But don't tell anybody ;)

PS2 This reminds me that this script should be localized too.

TracyTiger 11-05-2012 03:23 AM

I found your question intriguing so I looked around a little and I found the groupmems command. The man page shows the option:


      -d, --delete user_name
          Delete a user from the group membership list.

I just found it but never used it.

A backup plan would be:


usermod -G $(id -nG USER| sed -e ‘s/GROUP //’ -e ‘s/ /,/g’) USER
EDIT: Didier's "gpasswd" post looks to be a better solution than groupmems.

kikinovak 11-05-2012 07:50 AM

Thanks very much to the both of you !

All times are GMT -5. The time now is 08:15 AM.