How to change primary group

I want to remove user pserver from the group apache.

#deluser pserver apache
/usr/sbin/deluser: You may not remove the user from their primary group.

Now I want to add a new primary group, so I ran the following;

#usermod -G pserver pserver

Which returned the following:

pserver` is now in group `pserver

#groups pserver
pserver : apache pserver

Now I want to remove the user again from group apache, but I get same error again. How can I delete pserver from the primary group apache?


The usermod option -G adds supplementary groups. You want option -g to change the primary group. I.e. your command should have been:

# usermod -g pserver pserver

Note, this will also change group ownership of files in the home directory, but not elsewhere.

More generally, the syntax for changing user 'user' to have primary group 'group' is:

# usermod -g group user

Late, but a bit clear (at least for me);

sudo usermod -g <NewPrimaryGroupName> <TheTargetUserName>

And then check id

id <TheTargetUserName>

More details here: http://manpages.ubuntu.com/manpages/precise/man8/usermod.8.html

Credits: http://www.htpcbeginner.com/safely-change-primary-group-group-in-linux/

Thank for reply.


The question as placed by the OP is misleading. The title reads "How to change primary group", however in the question body its clear the intent is to not just replace the primary group but also remove the previous primary.

In case anyone wants to just replace the primary but not remove it from the list, one simply has to add the previous primary back to list.

So, after following StarNamer's post, one only needs to do a

adduser user previous-primary-group

because the usermod command will have thrown out your previous primary group from the list of groups you belong to.