Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Can a Linux user belong to more than one group?

If it is allowed, then one use may access files from two groups, that would be quite nice!

If not, is there any alternative way to have this function?

This issue is not the same as Ensuring new files in a directory belong to the group or Help me understand Ubuntu user/group permissions

share|improve this question
add comment

3 Answers

Yes, a user can be member of multiple groups:

Users are organized into groups, every users is in at least one group, and may be in other groups. Group membership gives you special access to files and directories which are permitted to that group.

For example, you can add the user username to groups group1 and group2 with the following command:

usermod -a -G group1,group2 username

share|improve this answer
add comment

Yes, a regular unix user can be a member of multiple groups.

However, there's only one group of which is the user's primary group.

When adding a user, for example using adduser, one can specify the primary group using the --ingroup option, and add multiple secondary groups like this in Debian/Ubuntu and alike:

$ # would create user gert and group gert
$ sudo adduser gert

$ # same, but no group 'gert' will be created, but made member of the existing
$ # group 'adm'
$ sudo adduser gert --ingroup adm

$ # secondary groups
$ sudo adduser gert superusers
Adding user `gert' to group `superusers' ...
Adding user gert to group superusers
Done.
$ sudo adduser gert debianfans

Checking which user you're a member of can be done using id:

$ id
uid=1000(gert) gid=1000(gert) groups=1000(gert),4(superusers),5(debianfans)
               ^^^            ^^^^^^
               primary        secondary
               group          groups

Also for other users, just by passing their username as a first argument to id.

You can change the primary group of a user by using the -g (--gid) option usermod

$ usermod -g new_primary_group username
share|improve this answer
add comment

Yes, see other answers. and An alternative is Access Control Lists.

setfacl
getfacl

Each file can have a list of users and groups that can access it.

A person is one user and many groups (groups can only be set by admin/root user). A file is normally one owner user and one group, with ACLs it can be one owner user, plus many other users, plus many groups.

Which you use depends on what you need. ACLs can be a pane to use but don't need admin (except to install and enable, it is not usually installed my default). The other way is simpler but less powerful.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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