Remove Linux User From a Secondary Group ( Supplementary Groups )

Q. User tom is a member of a group called sales and printer. I'd like to remove tom from a group called printer without editing any user configuration text files stored at /etc/ directory?

A. /etc/groups file defines group membership for each user. usermod command has -G option to set a list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no intervening whitespace. If the user is currently a member of a group which is not listed, the user will be removed from the group.

Step # 1: Find out user group identity

Use id command:
# id -nG {user-name}
# id -nG tom

Output:

sales printer

Step # 2: Remove user from printer group

Use the following syntax:
# usermod -G {groupname1,groupname2,...} {username}
To keep membership for sales only group (remove user tom from printer group), enter:
# usermod -G sales tom
# id -nG tom

Output:

sales

The following example remove user vivek from all groups except admin, audio, video and powerdev group:
# id -nG vivek
Output:

vivek adm dialout cdrom floppy audio dip video plugdev scanner netdev lpadmin powerdev admin

Modify group membership, enter:
# usermod -G admin, audio, video, powerdev vivek
# id -nG tom

Sample output:

vivek audio video powerdev admin

For more information, read usermod(8) command man page:
$ man usermod

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 5 comments… read them below or add one }

1 test 03.25.09 at 3:21 pm

Thanks for your website.
it’s very helpful.

2 shingai 04.02.09 at 1:42 pm

lets say i want to add an existing user to an existing group. when the user belongs to other groups already. what command will i use.

using usermod-g “groupname” user will change the initial gropu of the user and delete him from another group.
usermod -G this will lead from the user being deleted ffrom all other groups except the one sspecified.

3 kailash bhandari 04.25.09 at 6:38 pm

A user (pop)who is belong to SALES group. i want that pop is remove from SALES group. how can i do it. not tell me manually. i want command to remove it..

4 BAReFOOt 04.28.09 at 3:41 pm

In one line (and nice for scripts):
Add a group: usermod -aG GROUP USER
Remove a group: usermod -G $(id -nG USER| sed -e ’s/GROUP //’ -e ’s/ /,/g’) USER

5 ashim 06.10.09 at 5:42 am

Can anyone tell me command(s) to add an user to a new group without mentioning all the groups the user previously belongs to? (I haven’t gone thru man)

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , ,

Previous post: How To Upgrade FreeBSD 6.3 to 7.0 Stable Release

Next post: Linux: ifdown eth0:0 brings down all other virtual network interfaces