14

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?

3 Answers 3

22

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
3
  • Not clear in this command which is the user and which is the group, since in this unique circumstance they're both identical, but it frequently won't be that way.
    – Kzqai
    Sep 13, 2016 at 17:18
  • Text updated to answer the above comment
    – StarNamer
    Oct 3, 2016 at 14:46
  • To see the immediate change of the user groups issue following commands: su - ${USER} and then id -nG
    – Nahid
    Aug 10, 2020 at 5:45
7

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.

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.