Primary and secondary groups

So I’ve got the basics of primary and secondary groups down, but still have some questions I can’t seem to find solid answers to:

  1. Can many users belong to the same primary group?
  2. Can one user’s primary group be a secondary group of another user?

Thanks

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

  1. Yes, they can.

    $ id foo
    uid=1002(foo) gid=1002(foo) groups=1002(foo)
    $ id bar
    uid=1003(bar) gid=1003(bar) groups=1003(bar)
    

    Changing the primary group of user foo to bar which is the primary group for user bar:

    $ sudo usermod -g bar foo
    

    Now:

    $ id foo
    uid=1002(foo) gid=1003(bar) groups=1003(bar)
    $ id bar
    uid=1003(bar) gid=1003(bar) groups=1003(bar)
    
  2. Yes, it can be.

    $ id foo
    uid=1002(foo) gid=1002(foo) groups=1002(foo)
    $ id bar
    uid=1003(bar) gid=1003(bar) groups=1003(bar)
    

    Adding user bar to group foo which is the primary group of user foo:

    $ sudo usermod -a -G foo bar
    

    Now:

    $ id foo
    uid=1002(foo) gid=1002(foo) groups=1002(foo)
    $ id bar
    uid=1003(bar) gid=1003(bar) groups=1003(bar),1002(foo)
    

Solution 2

From the perspective of the user, he has a primary group and 0 or more secondary groups.

From the perspective of the group, it has 0 or more members.

A group that is the primary group for one or more users can be both a secondary or primary group for other users.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply