LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   Canonical way to remove a user from one or several groups? (http://www.linuxquestions.org/questions/slackware-14/canonical-way-to-remove-a-user-from-one-or-several-groups-4175435668/)

kikinovak 11-05-2012 01:17 AM

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

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:

Code:

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

Code:

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

Code:

# 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 01: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 02:23 AM

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

Code:

      -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:

Code:

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 06:50 AM

Thanks very much to the both of you !


All times are GMT -5. The time now is 01:16 AM.