usermod: Appending to Additional Groups

Written by Benjamin Cane on 2011-07-08 02:08:00 | 1 min read

This is one of those things that I found out the hard way long long ago.

When using the usermod command in Linux to add additional groups if you want to only list the new groups the user is in you must use the -aG flags rather than a simple -G.

When specifying -G by itself you are telling usermod to change the users additional groups to the groups specified and only those groups. The user will be removed from any other group. If you use -aG then the groups mentioned are appended and not removed.

Here are some examples:

[[email protected] ~]# id bcane  
uid=500(bcane) gid=500(bcane) groups=500(bcane),100(users)

My user has a primary group of bcane and an additional group of users

[[email protected] ~]# usermod -G wheel bcane  
[[email protected] ~]# id bcane  
uid=500(bcane) gid=500(bcane) groups=500(bcane),10(wheel)

My user now has a primary group of bcane and an additional group of wheel. It was removed from the users group via the usermod command.

[[email protected] ~]# usermod -aG users bcane  
[[email protected] ~]# id bcane  
uid=500(bcane) gid=500(bcane) groups=500(bcane),10(wheel),100(users)

Now my user is part of both the users and wheel group because I asked usermod to append the additional groups.

-G, --groups _GROUP1_[_,GROUP2,_[_,GROUPN_]]] 

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. The groups are subject to the same restrictions as the group given with the -g option. If the user is currently a member of a group which is not listed, the user will be removed from the group. This behavior can be changed via -a option, which appends user to the current supplementary group list.


Picture of Benjamin Cane

Benjamin is a Systems Architect working in the financial services industry focused on platforms that require Continuous Availability. He has been working with Linux and Unix for over 10 years now and has recently published his first book; Red Hat Enterprise Linux Troubleshooting Guide.

Publications

Identify, capture and resolve common issues faced by Red Hat Enterprise Linux administrators using best practices and advanced troubleshooting techniques

What people are saying:
Excellent, excellent resource for practical guidance on how to troubleshoot a wide variety of problems on Red Hat Linux. I particularly enjoyed how the author made sure to provide solid background and practical examples. I have a lot of experience on Red Hat but still came away with some great practical tools to add to my toolkit. - Amazon Review

Sponsored by