Tip: How to add an existing user to another group

This is about adding a user to additional groups, not changing the user's default group.
The classic SysV command is

usermod -G group1[,group2...] username

It adds a user to the named group(s).

But also it deletes the user from all not named groups!

Proposals exist to first list the user's existing groups with commands like

groups username
id -a username || id username

And then use the usermod -G with the existing groups plus the new group(s).
Alternatively use a text editor and in /etc/group add the user to the desired groups.
But why should you do all this? It's a computer isn't it?
Let's have a look at the different OSes:

AIX and HP-UX
They refer to their "smart" admin tools smitty and sam, respectively.
Nothing for the command line.

Linux
The usermod got -a ("add" option) to simply add a user to a group:

usermod -aG group1[,group2...] username

But - there is no corresponding delete option!
Well, meanwhile some distros have got a

usermod -rG group1[,group2...] username

And there is the gpasswd command that was enhanced with "add":

gpasswd -a username group1

and "delete":

gpasswd -d username group1

Solaris
Finally there is a nice implementation (in Solaris 11?):

usermod -G [+-]group1[,group2...] username

A prefix + means "add to the group(s)", a - means "delete from the group(s)".

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.