Supplementary Groups


Supplementary groups, also known as secondary groups, provide a mechanism to grant a user access to files and resources owned by other groups. A user can be a member of multiple supplementary groups, expanding their access permissions beyond their primary group.


Example 1: Add a user to a single supplementary group

# usermod -aG developers newuser2
# id newuser2

usermod -aG developers newuser2 adds the user newuser2 to the developers supplementary group. The -a (append) option is crucial.

id newuser2 displays the user's groups, now including developers.

Explanation The usermod command with -aG appends the user to the specified supplementary group. Without the -a, the user would be removed from all other supplementary groups.


Example 2: Add a user to multiple supplementary groups at once

# groupadd testers
# usermod -aG developers,testers newuser3
# id newuser3

groupadd testers creates a new group named testers.

usermod -aG developers,testers newuser3 adds newuser3 to both the developers and testers supplementary groups.

id newuser3 confirms the new group memberships.

Explanation To add a user to multiple supplementary groups, you can list them separated by commas after the -G option. The -a option ensures these are added to the user's existing supplementary groups.


Example 3: Create a new user and add them to supplementary groups simultaneously

# useradd -G developers,testers newuser4
# id newuser4

useradd -G developers,testers newuser4 creates newuser4 and adds them to the developers and testers supplementary groups.

id newuser4 verifies the group memberships.

Explanation The useradd command allows assigning supplementary groups at the time of user creation using the -G option. This is an efficient way to set up a new user with the required permissions.


Example 4: View all supplementary groups for a user

# groups newuser3

groups newuser3 lists all the groups the user newuser3 belongs to.

Explanation The groups command provides a straightforward way to see the group memberships for any user. The first group listed is the primary group, followed by all supplementary groups.


Example 5: Remove a user from a supplementary group

# gpasswd -d newuser3 testers
# id newuser3

gpasswd -d newuser3 testers removes the user newuser3 from the testers group.

id newuser3 confirms the removal.

Explanation The gpasswd utility is a convenient tool for managing group memberships. The -d option is used to delete a user from a specified group.


Example 6: Command for adding a user to a supplementary group in AIX (a different UNIX OS)

# chuser groups=group1,group2,developers aixuser

This command modifies the user aixuser and sets their supplementary groups to the comma-separated list.

Explanation In AIX, the chuser command is used to modify user attributes. To add a user to a supplementary group, you must provide the complete list of desired supplementary groups, as this command overwrites the existing list.


Example 7: Add a user to a group as a group administrator

# gpasswd -A newuser1 developers
# grep developers /etc/gshadow

gpasswd -A newuser1 developers makes newuser1 an administrator for the developers group.

grep developers /etc/gshadow shows the group administrators in the /etc/gshadow file.

Explanation A group administrator can add or remove members from that group. The gpasswd command with the -A option assigns this administrative privilege.


Example 8: Set the exact list of supplementary groups for a user

# usermod -G developers newuser4
# id newuser4

usermod -G developers newuser4 sets the supplementary group for newuser4 to only be developers, removing them from any others.

id newuser4 displays the updated group memberships.

Explanation When using usermod -G without the -a (append) option, you are replacing the user's entire list of supplementary groups with the new list provided in the command.