5

What is the difference between the primary and secondary group? Why is there a need for a primary group?

Are the user's permissions the same as the primary group permission?

Assume there is a user A with primary group grp1 and user B with primary group grp2 and secondary group grp1. Then can B have the same permission as A for the files created by A?

1
  • As an integration to the two answers, also note that some systems grant special privileges to the members of some groups. For example, in Ubuntu, in order to be able to run sudo, a user must belong to group sudo. In FreeBSD, in order to use some video drivers, a user must belong to group video. Maybe you don't want (or can't) have sudo as primary group, but you can have it as secondary group. Also, the primary group of a user is listed in /etc/passwd. In /etc/group, instead, for each group is specified a list of users having the group as secondary group.
    – BowPark
    Oct 25 '20 at 17:18
5

What is difference between primary and secondary group? What is the need of primary group ?

From Unix Groups:

Primary group – Specifies a group that the operating system assigns to files that are created by the user. Each user must belong to a primary group.

Secondary groups – Specifies one or more groups to which a user also belongs. Users can belong to up to 15 secondary groups.

What is the need of primary group ? Are the user's permissions the same as primary group permission?

Imagine there is no primary group assigned to my User and I'm in 10 secondary groups. I create a new file... what group this file belongs to? which one of these 10? primary group tackles this issue and defines which group the file belongs to by default. You don't want the files you are creating in your home directory to be owned by development group so anyone on that group can do the stuff allowed to do with them as the group.

Assume there is a user A with primary group grp1 and user B with primary group grp2 and secondary group grp1. Then can he have same permission as A for the files created by A ?

No! only permissions defined by group part. Of course it is possible to do it. Using umask for example.

3

Every file is owned by one user and one group, no more, no less. So when you create a file it will be owned by your user and current group. When a user logs in their current group will be their primary group.

A user can temporarily switch group to any of their secondary groups with sg or newgrp. The user can do this without having a the group password as long as it is listed as a secondary group for that user. While switched, files created will be owned by the new group, not the user's own

For permissions, primary and secondary groups are used together, there is no need to switch groups to access a file owned by one of your secondary groups.

Users can also change a file's group to any of their groups (primary or secondary) with chgrp.

In this sense, the primary group is little more than a "default" current group. Except that a user must have a primary group, there's no reason why they must have any other. This it is common to create a new group for every new user, that way the new user has no default access to other than their own user permissions.


From your question:

Assume there is a user A with primary group grp1 and user B with primary group grp2 and secondary group grp1. Then can they have the same permission as A for the files created by A ?

User B will have the group permissions assigned to files created by A. In this scenario, A is only creating files with grp1. User A can take away any / all permissions from the group.

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.