usermod
Modify an existing user account
By CMD Script Team · 4 min read · Last updated
usermod [OPTIONS] USERNAMEOptions
| Flag | Description |
|---|---|
-aG GROUP1,GROUP2 | Append the user to one or more supplementary groups without removing existing ones |
-G GROUP1,GROUP2 | Set the user's supplementary groups, replacing all current group memberships |
-s SHELL | Change the user's login shell |
-l NEW_NAME | Change the username (login name) |
-d NEW_HOME | Change the user's home directory path (use with -m to move existing contents) |
-L | Lock the account by prefixing the encrypted password with ! |
-U | Unlock a previously locked account |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS (not available; use dscl instead)
What it does
usermod modifies attributes of an existing user account — shell, home directory,
username, supplementary group memberships, account lock status, and more — by editing
/etc/passwd, /etc/shadow, and /etc/group. Unlike useradd, it operates on an
account that already exists.
Beginner examples
sudo usermod -aG docker alice— addaliceto thedockergroup, keeping her existing groupssudo usermod -s /bin/zsh alice— changealice's login shell to zshsudo usermod -L alice— lock the account so it can't log in with a passwordsudo usermod -U alice— unlock it againgroups alice— check current group memberships before and after a change
sudo usermod -aG sudo alice
Advanced examples
- Rename a user and relocate their home directory in one step:
sudo usermod -l bob -d /home/bob -m alice(must be logged out;-mmoves the contents of the old home directory to the new path). - Add a user to several groups at once while preserving existing membership:
sudo usermod -aG docker,sudo,www-data alice. - Change a user's UID (rare, and requires fixing file ownership afterward):
sudo usermod -u 2000 alice && find / -user 1500 -exec chown -h 2000 {} \;. - Lock an account pending investigation without deleting it:
sudo usermod -L suspicious_user, reversible later with-U.
sudo usermod -aG docker,sudo alice && groups alice
Common mistakes
- The single most common
usermodmistake: runningusermod -G docker aliceintending to add thedockergroup, but actually replacing all ofalice's supplementary groups with justdocker, silently dropping her fromsudoor any other group she was in. Always use-aG(append) unless you specifically intend to reset the full group list. - Expecting a group change to apply to an already-open shell session — it won't until the
user logs out and back in (or starts a fresh session with
su - user). - Changing
-dwithout-mand leaving the old home directory's contents behind at the old path while the account now points somewhere empty. - Renaming a user with
-land forgetting that cron jobs, file ownership by old username-derived paths, or scripts hardcoding the old name may break.
Tips
- Always verify with
groups usernameorid usernamebefore and after a-G/-aGchange to confirm the result matches your intent. - Use
getent group groupnameto confirm a group exists before trying to add a user to it. - When in doubt about
-Gvs-aG, default to-aG— it's non-destructive, whereas-Galone requires you to list every group the user should remain in.
Best practices
- Make
-aG(append) the default habit for adding group memberships in scripts and runbooks; reserve plain-Gfor cases where you deliberately want to set an exact, complete group list. - Script account changes idempotently — check current state with
id/groupsbefore modifying, especially in configuration management tools that may runusermodrepeatedly. - Document any UID/username changes and grep for hardcoded old usernames or UIDs in cron, systemd units, and application configs before renaming or renumbering an account.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Granting a new team member access to the
dockerorsudogroup after their account already exists, without disturbing their other group memberships. - Locking a departing or compromised account immediately (
usermod -L) while a full offboarding (userdel) is still pending review. - Correcting a user's login shell after installing a new shell (e.g. switching from
/bin/bashto/usr/bin/zsh).
Common interview questions
- What's the difference between
usermod -aGandusermod -G?-aGappends the specified groups to the user's existing memberships;-Galone replaces the entire supplementary group list, removing the user from any group not explicitly listed. - Why doesn't a new group membership take effect immediately in an open terminal? Group membership is resolved at login/session start, so an already-running shell keeps its old group list until the user logs out and back in or starts a new session.
- How would you safely add a user to a new group without risking their existing
access? Use
usermod -aG newgroup username— never plain-G, which would silently drop them from any groups not listed.
Frequently Asked Questions
What's the difference between usermod -aG and usermod -G?
-aG appends the listed groups to the user's existing supplementary group memberships. -G alone replaces the entire supplementary group list with only what you specify, silently removing the user from any group not listed — a very common and dangerous mistake.
Why did usermod -G docker alice remove alice from other groups?
Because -G without -a sets the complete supplementary group list. If alice was previously in sudo and docker, and you run usermod -G docker alice, she is removed from sudo. Always use -aG group when you only want to add, not replace.
Do I need to log out for group changes to take effect?
Yes, typically. Group membership is evaluated when a login session starts, so an already-open shell or session won't see the new group until the user logs out and back in (or starts a new session, e.g. via su - username).
How do I rename a user with usermod?
Use usermod -l new_name old_name to change the login name. This does not rename the home directory by default; combine with -d /home/new_name -m to also move and rename the home directory.
Cheat sheet
Download a quick-reference cheat sheet for usermod.