Adding a user to a group is a root task and one command. The flags matter more than the command does:
sudo usermod -aG developers deploy
Never drop the -a
This is the single most important thing on this page. -G sets the user's secondary groups to exactly the list you give. -a makes it append instead of replace.
sudo usermod -aG developers deploy # CORRECT: adds developers, keeps the rest
sudo usermod -G developers deploy # DANGER: developers is now their ONLY secondary groupRun the second form and you have just removed deploy from sudo, docker, and every other group it was in. People lock themselves out of sudo this way constantly. Always -aG, never bare -G, unless you genuinely intend to replace the whole list.
Verify it, and know about the login catch
groups deploy # what the system says deploy is in
id deploy # same, with UIDs/GIDsThe catch: group membership is read at login. A user already logged in does not gain the new group in their current session. They must log out and back in. To pick it up without logging out, start a new shell with the group active:
newgrp developersFor your own session you may also need a fresh login for things like Docker socket access to work.
The gpasswd alternative
gpasswd operates from the group's side and is handy in scripts:
sudo gpasswd -a deploy developers # add deploy to developers
sudo gpasswd -d deploy developers # remove deploy from developersgpasswd -a is equivalent to usermod -aG for a single group, and gpasswd -d is the clean way to remove someone from a group, which usermod cannot do directly.
FAQ
See also
- How to Create a Group on Linux: make the group first, primary vs secondary.
- How to Create a User on Linux: assign groups at creation with -G.
- How to Give a User sudo Access: the most common reason to add someone to a group.
- List users and groups: confirm who is in what.
Sources
Authoritative references this article was fact-checked against.





