Every user on a Linux system belongs to at least one group, call primary group. However, users can belong to many groups. Groups are used to organize users.

For example:

If members of the Front-end team need to access to some of the same files and directories, they can be placed in to the frontend group.

The groups command ( groups )

You can run the groups command to displays what groups you are a member of. You can also use id -Gn, as a synonym for groups and it will give the same output.

$ groups
staff everyone admin
$ id -Gn
staff everyone admin

If you supply another user's ID to an argument to the group's command. You will see a list of groups to which that user belongs.

$ groups john
staff sales

Working with Groups

If you are a member of multiple groups, then when you create a new file it will be in the primary group ( first group ) . If you would like to change the group of the file, use the chgrp command.

Here is an example that the test.txt file was created and put in my primary group , which is staff. I am also the member of the everyone and admin groups.

$ ls -l test.txt
-rw-r--r--@  1 robin  staff    411 16 Sep 22:50 test.txt
$ groups
staff everyone admin

So, to change the group to everyone for this file, you can type the command chgrp everyone test.txt.

$ chgrp everyone test.txt
$ ls -l test.txt
-rw-r--r--@  1 robin  everyone    411 16 Sep 22:50 test.txt

Then you can give the permissions in the everyone group can edit the file.

$ chmod g+w test.txt
$ ls -l test.txt
-rw-rw-r--@  1 robin  everyone    411 16 Sep 22:50 test.txt