Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

In Linux, groups provide a convenient way for a set of users to share files and directories with each other. There are two types of groups: primary groups and secondary groups.

In this tutorial, we’ll learn about user groups in Linux and the differences between the two types of groups. In addition, we’ll learn how to add users to a secondary group.

2. Definitions

Let’s see what each term means:

  • Group: A group is a collection of users who can share files and other system resources
  • Primary Group: Specifies a group that the operating system assigns to files that the user creates. Naturally, each user must belong to a primary group
  • Secondary Group: Specifies one or more groups to which a user also belongs. In addition, users can belong to up to 15 secondary groups

When we create a new user in Linux, a new group with the same name as the user will also be created and associated with the user. This new group is the user’s primary group.

After a user is created, it can also belong to up to 15 secondary groups. Moreover, belonging to secondary groups is optional, while a user must belong to one primary group.

3. Examples

Let’s see things with the help of examples.

3.1. Creating a New User

First, we should create a new user using useradd:

$ sudo useradd --create-home bob

–create-home will make useradd create a home directory for bob.

3.2. Listing a User’s Groups

The groups command lists all the groups that the specified user belongs to. And, if a user is not specified, the command uses the current user as input:

$ groups bob
bob : bob

We can see that the newly created user belongs to its own primary group with the same name as the user.

3.3. Finding a User’s Primary Group

The /etc/passwd contains each user and their primary group. For example, let’s find bob in it:

$ grep bob /etc/passwd
bob:x:1001:1001::/home/bob:/bin/sh

We found bob in the /etc/passwd file. Moreover, the output means:

  • the first 1001 is the user’s UID (user ID)
  • the second 1001 is the user’s primary group. So, the user’s primary group is also named bob, and its GID (group ID) is 1001
  • /home/bob shows the user’s home directory

3.4. Creating a File

Now let’s create a file owned by bob:

$ touch file.txt
$ ls -l
total 0
-rw-rw-r-- 1 bob bob 0 Jun  3 00:36 file.txt

ls -l will list the files in the current directory in a long listing format which shows more details about them:

  • the first bob is the user that owns the file
  • the second bob shows what group the file belongs to; certainly, it’s the primary group of the user that created it

We can see that the newly created file belongs to the user that created it and its primary group. Of course, we can change the owner using the chown command, and the group using the chgrp command.

3.5. Creating a Secondary Group

Now let’s create a secondary group and add a few users to it:

$ sudo useradd --create-home jane
$ sudo useradd --create-home alex
$ sudo groupadd teachers
$ sudo gpasswd -a jane teachers
Adding user jane to group teachers
$ sudo gpasswd -a alex teachers
Adding user alex to group teachers

groupadd creates a new group and gpasswd -a adds a user to a group. Certainly, each new user now belongs to its own primary group as well as the new secondary group.

3.6. Finding Secondary Group Members

The /etc/group file contains secondary group members:

$ grep teachers /etc/group
teachers:x:1005:jane,alex

The teachers group is a secondary group for both jane and alex. In addition, the teachers group’s GID is 1005.

4. Conclusion

In brief, we learned about primary and secondary groups in Linux and their differences.

Groups can be useful for sharing files and directories between users on a Linux machine. And, using the right commands, we can manage users and groups in Linux and use groups to their full potential.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are closed on this article!