How to Add Secondary Group in Linux
The standard way to how to add secondary group in linux is to use usermod -aG groupname username. This adds a user to a supplementary group without removing any existing group memberships, which is the safest option for most cases. In Linux, a secondary group is also called a supplementary group, and it gives a user extra permissions beyond their primary group.
What is a secondary group in Linux?
Every Linux user has one primary group and can belong to one or more secondary groups. The primary group is the default group tied to new files the user creates, while secondary groups provide additional access to files, directories, devices, or administrative tasks.
For example, if a user needs access to a shared project folder, a printer, or a Docker group, adding them to a secondary group is often the right approach.
Add a user to a secondary group
The safest command is:
sudo usermod -aG groupname username
Here is what each part means:
sudoruns the command with administrative privileges.usermodchanges an existing user account.-ameans append, so existing group memberships are preserved.-Gspecifies the supplementary groups to add.groupnameis the target secondary group.usernameis the user account you want to modify.
Example
If you want to add user alex to the docker group:
sudo usermod -aG docker alex
After running the command, the user is added to the group in the account database. The change usually takes effect fully after the user signs out and signs back in.
Why the -a option matters
This is one of the most important details. If you use -G without -a, you can replace the user’s current supplementary groups instead of adding to them. That can remove access you did not intend to remove.
Use this:
sudo usermod -aG groupname username
Not this, unless you intentionally want to replace all supplementary groups:
sudo usermod -G groupname username
Alternative: use gpasswd to add a user to a group
Another common command is:
sudo gpasswd -a username groupname
This also adds a user to a secondary group. It is useful on many Linux distributions and is easy to remember because it reads like “group password, add user.”
In practice, both usermod -aG and gpasswd -a are widely used. The key difference is that usermod is often preferred when managing user accounts directly, while gpasswd is a concise group-management tool.
| Command | Purpose | Important note |
|---|---|---|
usermod -aG | Add a user to one or more supplementary groups | Use -a to avoid removing existing groups |
gpasswd -a | Add a user to a group | Simple and direct for group membership changes |
Verify the group membership
After making the change, confirm that the user is in the group.
Check with id
id username
Example:
id alex
This shows the user’s UID, primary group, and all supplementary groups.
Check with groups
groups username
Example:
groups alex
This gives a simple list of groups the user belongs to.
Check the group file directly
You can also inspect membership in /etc/group:
grep '^groupname:' /etc/group
Example:
grep '^docker:' /etc/group
This is useful if you want to confirm that the system recorded the membership correctly.
If the change does not take effect
Adding a user to a secondary group does not always affect the user’s current session immediately. This is a common point of confusion.
Sign out and sign back in
Most group membership changes take effect after the user logs out and logs back in. For desktop sessions, this usually means a full logout and login. For SSH sessions, reconnecting is often enough.
Use a new shell session
If you need to test the group change right away, you can start a new login shell or use:
newgrp groupname
This changes the current shell’s active group context, but it is not a substitute for proper account membership. It is mainly useful for temporary testing.
Check whether access depends on the new group
Some permissions are evaluated when a process starts, so an already-open terminal, editor, or file manager may still use the old session groups. If access still fails after a fresh login, verify the file or directory permissions as well.
Primary group vs secondary group
Understanding the difference helps avoid mistakes.
| Type | Role | Typical use |
|---|---|---|
| Primary group | The main group associated with the user account | Default group ownership for new files |
| Secondary group | Additional group membership for extra permissions | Shared access, device access, admin tools, project folders |
When people ask how to add secondary group in linux, they usually mean supplementary group membership, not changing the primary group. Those are different tasks.
Common mistakes to avoid
- Forgetting
-awith-G, which can remove other supplementary groups. - Using the wrong group name, especially when the group has not been created yet.
- Expecting instant access in the current terminal session without logging out and back in.
- Assuming group membership alone is enough, when file ownership or permission bits may still block access.
- Mixing up primary and secondary groups, which can lead to unexpected file permission behavior.
Linux distribution notes
The basic commands above work on most common distributions, including Ubuntu, Debian, Fedora, and RHEL-based systems. The exact package names or admin group names may vary, but user and group management is generally the same.
On some systems, the administrative group used for privilege escalation may be sudo, while on others it may be wheel. That is separate from adding a secondary group, but it matters if you are granting admin access.
Quick checklist
- Confirm the group already exists.
- Add the user with
sudo usermod -aG groupname usernameorsudo gpasswd -a username groupname. - Verify membership with
id usernameorgroups username. - Ask the user to log out and log back in.
- Test the permissions again in a new session.
Frequently Asked Questions About how to add secondary group in linux
How do I add a user to a secondary group in Linux?
Use sudo usermod -aG groupname username to add the user without removing existing supplementary groups.
What is the difference between primary and secondary groups in Linux?
The primary group is the user’s default group for new files, while secondary groups are extra groups that grant additional access.
How do I check which groups a user belongs to?
Use id username for detailed output or groups username for a simple list.
Do I need to log out after changing group membership?
Usually yes. The new group membership often does not apply to existing sessions until the user logs out and logs back in.
Why is my user not getting access after being added to a group?
The most common reasons are an outdated session, the wrong group name, or file permissions that still do not allow the requested access.
Can I add a user to more than one secondary group at once?
Yes. With usermod, you can separate multiple group names with commas, for example sudo usermod -aG docker,libvirt alex.
Should I use usermod or gpasswd?
Both are valid. usermod -aG is common for account administration, while gpasswd -a is a compact way to add someone to a group.
Summary
If you want to add a secondary group in Linux, the safest default is sudo usermod -aG groupname username. The -a option matters because it preserves existing group memberships, and the change usually becomes fully effective after the user logs out and back in. Verify the result with id or groups, and if access still does not work, check both the session state and the underlying file permissions.
