93

I have a user like this:

uid=501(ironsand) gid=500(foo) groups=500(foo),10(wheel),497(git),501(ironsand)

And to change primary group to ironsand like gid=501(ironsand), I typed this command:

sudo usermod -g ironsand ironsand

It changed groups order but didn't change main group like:

uid=501(ironsand) gid=500(foo) groups=501(ironsand),10(wheel),497(git),500(foo)

I thought someone already asked a question like this, but I couldn't find one.

How can I change primary group?

0

1 Answer 1

155

Usually you do it like the following.

To assign a primary group to an user:

$ usermod -g primarygroupname username

To assign secondary groups to a user (-a keeps already existing secondary groups intact otherwise they'll be removed):

$ usermod -a -G secondarygroupname username

From man-page:

...
-g (primary group assigned to the users)
-G (Other groups the user belongs to)
-a (Add the user to the supplementary group(s))
...
5
  • 8
    For secondary groups, if adding instead of given a exhaustive list, don't forget the flag -a
    – DrBeco
    Apr 17, 2016 at 2:35
  • 14
    And don't forget that you have to log out and back in again for the change to become effective
    – ISparkes
    Oct 3, 2016 at 14:42
  • 3
    The -a flag is very important, because you can accidentaly remove user accounts from the sudoers list by forgetting it.
    – Qqwy
    Feb 3, 2017 at 21:50
  • need close all login session, otherwise it will not work
    – Kamil
    Apr 19, 2021 at 1:06
  • 1
    How do you do this without root access? Apr 20, 2021 at 5:22

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