Can a Linux user belong to more than one group?

18

5

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

hugemeow

Posted 2012-09-19T15:03:48.853

Reputation: 1 819

Answers

18

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 usermod command:

usermod -a -G group1,group2 username

slhck

Posted 2012-09-19T15:03:48.853

Reputation: 182 472

14

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

gertvdijk

Posted 2012-09-19T15:03:48.853

Reputation: 3 396

1

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.

ctrl-alt-delor

Posted 2012-09-19T15:03:48.853

Reputation: 1 886