Linux6 min readPublished: 21 Jun 2024Updated: 9 Nov 2026

How to Add a User to a Group on Linux

Add a user to a Linux group with usermod without wiping their existing groups. Covers primary and secondary groups, and why changes need a fresh login.

G7Cloud Engineering
Platform team
Share:
Abstract illustration representing how to add a user to a group on linux.
  • sudo usermod -aG groupname username adds a user to a group. Always include the -a.
  • Without -a, usermod replaces every secondary group the user had. That is how people remove their own sudo access by accident.
  • A group change does not reach a shell that is already open. Log out and back in, or run newgrp.
  • Every user has one primary group (which owns the files they create) and any number of secondary groups.
  • Check the result with id username, which shows the primary group and every secondary group on one line.

How do I add a user to a group in Linux?

Run sudo usermod -aG groupname username. The -a means append and -G sets the user's secondary groups (the extra groups beyond their own). Together they add one more group and keep the ones the user already had. Check the result with id username, which lists every group the account is in.

Add and verify
# Add alice to the www-data group
sudo usermod -aG www-data alice

# Check it worked
id alice
# uid=1001(alice) gid=1001(alice) groups=1001(alice),33(www-data)

# Add to several groups in one command (comma-separated, no spaces)
sudo usermod -aG www-data,docker,sudo alice
No spaces after the commas. -aG www-data, docker is read as two arguments and will not do what you meant.

Leaving out -a removes every other group

sudo usermod -G docker alice sets alice's secondary groups to exactly docker and quietly drops the rest, including sudo. If that was your own account, you can no longer run admin commands on the machine. Always type -aG, never -G on its own.

The safer alternative
# gpasswd only ever adds, so it cannot wipe anything
sudo gpasswd -a alice www-data

# And to remove
sudo gpasswd -d alice www-data

# On Debian and Ubuntu, adduser does the same and reads more clearly
sudo adduser alice www-data
gpasswd -a has no destructive form. On a production machine, it is the one to use.

Why does the group change seem to have no effect?

Group membership is read once, at login. Your current shell still carries the group list it was given when it started. The change has been made; it just has not reached the session you are in. This catches almost everyone the first time.

Making the change apply
# What the system now says (updated immediately)
id alice

# What your current shell believes (still the old list)
groups

# Three ways to pick up the change:

# 1. Log out and back in. Always works.
exit

# 2. Start a shell with the new group active
newgrp www-data

# 3. Re-exec your login shell
exec su - $USER
If id shows the group but groups does not, the change worked and you need a new session.

Services need restarting too

A running service (daemon) keeps the groups it had when it started, just like your shell. If you add the web server user to a group so it can read a directory, restart the service (sudo systemctl restart php8.3-fpm). Otherwise the new permission waits until the next reboot.

What is the difference between a primary and a secondary group?

Every user has exactly one primary group and any number of secondary groups. New files the user creates belong to their primary group. Secondary groups grant access to things but do not change who owns new files. Mixing the two up causes a lot of confusion on shared web servers.

Primary groupSecondary groups
How manyExactly oneAny number, including none
Owns new filesYesNo
Stored in/etc/passwd (the GID field)/etc/group
Set withusermod -gusermod -aG
Typical useThe user's own private groupAccess to www-data, docker, sudo

The flag gives it away: lowercase -g is the primary group, uppercase -G is the secondary list.

Changing the primary group
# Show current primary group
id -gn alice

# Change it (lowercase g, no -a: a user has only one)
sudo usermod -g developers alice

# Files alice creates from now on are owned by developers
sudo -u alice touch /tmp/testfile && ls -l /tmp/testfile
Changing a primary group does not change the owner of files that already exist. Use chgrp -R for those.
Pro Tip

On a shared document root, the setgid bit is usually better than changing anyone's primary group. chmod g+s on the directory makes new files take the directory's group, whoever created them. Linux file permissions for web servers covers the pattern.

How do I create a group and see who is in it?

Creating and inspecting groups
# Create a group
sudo groupadd developers

# Create one with a specific GID (useful when matching IDs across servers)
sudo groupadd -g 2000 developers

# Every group on the system
getent group

# One group, and its members
getent group www-data
# www-data:x:33:alice,deploy

# Every group a user belongs to
groups alice
id alice
Use getent group rather than reading /etc/group directly. It also returns groups from LDAP or another directory service.

getent group can look empty and still be right

The member list in /etc/group shows only secondary members. A user whose primary group is www-data will not appear there, even though they are in it. id username is the reliable check because it reports both.

Removing users and groups
# Remove a user from one group
sudo gpasswd -d alice www-data

# Delete a group entirely (fails if it is anyone's primary group)
sudo groupdel developers

# Find files owned by a group before you delete it
sudo find / -group developers -ls 2>/dev/null | head
Run that last command first. Deleting a group leaves its files owned by a bare group number (GID), which is confusing for whoever comes next.

Which groups matter on a web server?

A handful of groups do nearly all the work on a typical web server. Knowing what each one grants tells you which to add someone to, and which to keep them out of.

GroupGrantsAdd someone when
sudo (or wheel)Administrative commands via sudoThey administer the server
www-data (or apache, nginx)Access to files the web server can reachThey deploy or edit site files
dockerEffectively full root, via the container runtimeAlmost never: treat it as granting root
admRead access to files in /var/logThey need to read logs without sudo
ssh or sshusersPermission to log in over SSH, where configuredThey need shell access at all

sudo is called wheel on AlmaLinux, Rocky and RHEL. The docker row is not an exaggeration.

The docker group is root access

Anyone in the docker group can start a container that mounts the host's files and read or write anything on it. They need no sudo and leave nothing in the sudo logs. Grant it only to people you would give root to anyway.

A typical new-developer setup
# Create the account
sudo adduser --disabled-password --gecos '' bob

# Shared team group for the document root
sudo usermod -aG webdev bob

# Web server group so they can read what the site serves
sudo usermod -aG www-data bob

# Log access without handing over sudo
sudo usermod -aG adm bob

# Confirm, then have them log in fresh
id bob
--disabled-password means the account can only log in with an SSH key, which is what you want. Turn password logins off entirely.

Groups are one half of access control. The other half is how the files themselves are owned. Linux users, groups and file permissions covers the whole model. If you would rather not manage server accounts at all, our managed WordPress hosting looks after the operating system for you.

Frequently asked questions

What does the -aG flag mean in usermod?

-a means append and -G names secondary groups. Together they add the user to the named group and keep their existing groups. Using -G without -a replaces the user's whole secondary group list, which is how people remove their own sudo access by accident.

Why is my user still not in the group after running usermod?

Group membership is read at login, so your current shell still has the old list. Run id username to confirm the system has recorded the change, then log out and back in, or run newgrp groupname to start a shell with the new group active.

How do I see all the groups a user belongs to?

id username is the most complete answer. It shows the primary group and every secondary group, with names and numbers. groups username gives just the names. Reading /etc/group directly misses primary group membership.

How do I remove a user from a group?

sudo gpasswd -d username groupname removes one membership and leaves the others alone. Avoid usermod for this: you have to retype every group the user should keep, which is easy to get wrong.

What is the difference between usermod, gpasswd and adduser for groups?

All three can add a user to a group. usermod -aG works everywhere but is dangerous if you forget the -a. gpasswd -a only ever adds, so it cannot remove anything. adduser user group is a Debian and Ubuntu shortcut that reads most clearly. On production, prefer gpasswd.

Put this into practice on G7Cloud

Every site runs in its own dedicated container behind ScaleShield, with daily backups that are restore-tested every night. Start on the free plan, no card needed.

About G7Cloud Engineering

Platform team

Articles written by the engineers who build and run G7Cloud: UK managed hosting and the AI Website Builder. We write about what we operate every day: containers, backups, databases, and the small-business websites that run on them.

More about G7Cloud →