usermod Command in Linux: Modify User Accounts, Shells, and Groups
Learn usermod command in linux with syntax, essential flags, practical terminal examples, real-world sysadmin workflows, security tips, and troubleshooting.
12 min read
Learn usermod command in linux with syntax, essential flags, practical terminal examples, real-world sysadmin workflows, security tips, and troubleshooting.
- What usermod Does and When to Use It
- Availability and Installation Across Linux Distributions
- Command Syntax, Arguments, and Exit Status Codes
Mastering usermod command in linux is an essential skill for Linux system administrators, developers, and DevOps engineers. Mastering the usermod command in Linux is essential for managing the lifecycle of user and service accounts without deleting and recreating them. Over the life of a production server, engineers change teams, switch preferred interactive shells from /bin/bash to /bin/zsh, require new supplementary group permissions, or need their home directories migrated onto dedicated encrypted storage volumes. Running usermod updates these account attributes atomically across the system's authentication databases.
The usermod command in Linux modifies existing user account properties stored in /etc/passwd, /etc/shadow, and /etc/group. System administrators use sudo usermod to append supplementary groups (-aG), change login shells (-s), relocate home directories (-d /new/path -m), rename login usernames (-l), and lock or unlock accounts (-L / -U).
Part of the shadow-utils suite, usermod is the direct counterpart to useradd and userdel. Instead of manually editing /etc/passwd, /etc/shadow, /etc/group, and /etc/gshadow—where a single typo can corrupt system authentication—usermod acquires mandatory file locks, validates target UIDs, GIDs, and directory paths, and synchronizes changes across all four files.
Beyond changing group memberships (-aG), usermod handles advanced operations such as migrating the contents of a user's home directory (-d /new/home -m), changing a numeric UID while recursively updating file ownership inside the user's home directory (-u), and disabling compromised accounts during security incidents (-L -e 1 -s /usr/sbin/nologin).
What usermod Does and When to Use It
When usermod executes, it acquires exclusive advisory locks on /etc/passwd, /etc/shadow, /etc/group, and /etc/gshadow. If you invoke an operation that alters the user's numeric UID (-u), login name (-l), or home directory location (-d -m), usermod first scans the /proc process table to verify that no running processes currently belong to that UID. This safeguard prevents active daemons or interactive shells from breaking mid-session.
When you change a user's numeric UID with -u NEW_UID or primary GID with -g NEW_GID, usermod automatically walks the directory tree inside the user's home directory and updates the inode UID/GID of every file owned by the old ID to the new ID. However, usermod intentionally does not scan filesystems outside the user's home directory (such as /tmp, /var, or /srv), so administrators must use find / -uid OLD_UID -exec chown ... for external paths.
Linux enforces discretionary access control (DAC) by evaluating the effective User ID (EUID), effective Group ID (EGID), and supplementary groups against inode permission bits. When usermod executes, the Virtual File System (VFS) and active Linux Security Modules (SELinux or AppArmor) validate the caller's privileges before committing changes to disk or account databases.
Availability and Installation Across Linux Distributions
The usermod utility is packaged in shadow-utils / passwd across mainstream Linux distributions. It is pre-installed on standard Ubuntu, Debian, RHEL, Rocky Linux, AlmaLinux, Fedora, and Arch Linux systems, while minimal container images may require manual installation:
Before running administrative tasks or automation scripts, verify whether usermod is installed and inspect which binary path and version your shell resolves:
type -a usermod
usermod --version 2>/dev/null || man -f usermod
# Illustrative output (reviewed against documentation)
usermod is /usr/bin/usermod
usermod (shadow-utils / passwd)
# Debian / Ubuntu
sudo apt-get update && sudo apt-get install -y passwd
# RHEL / Rocky / AlmaLinux / Fedora
sudo dnf install -y shadow-utils
# Arch Linux
sudo pacman -S --noconfirm shadow
# Alpine Linux
sudo apk add --no-cache shadow
Command Syntax, Arguments, and Exit Status Codes
usermod [OPTIONS] USERNAME
The usermod command follows the syntax usermod [OPTIONS] USERNAME, where USERNAME is the current login name of an existing local account in /etc/passwd and at least one modification flag is provided. Because modifying system account databases requires write access to /etc/passwd and /etc/shadow, usermod must always be run as root or via sudo.
Multiple flags can be combined in a single invocation—for example, sudo usermod -l newname -d /home/newname -m -c "Updated Name" oldname renames the account, moves the home directory on disk, updates the home path in /etc/passwd, and updates the GECOS comment atomically. Notice that when renaming a user with -l NEW_NAME, the final positional argument on the command line is always the current (oldname) username.
0— Account attributes modified successfully.1— Cannot update/etc/passwdor/etc/shadow.2— Invalid command syntax or unrecognized option.3— Invalid argument supplied to an option.4— Specified UID is already in use (and-owas not passed).6— Specified user or group does not exist.8— Target user is currently logged in or has active running processes.9— Target new username (-l) is already in use.10— Cannot update/etc/groupor/etc/gshadow.12— Unable to move the home directory (-m) to the new destination.
Important Options and Flags Reference
| Option / Flag | Long Form / Argument | Technical Behavior and Purpose |
|---|---|---|
-aG | --append --groups GROUPS | Append the user to one or more comma-separated supplementary groups without removing existing groups. |
-s | --shell SHELL | Change the user's login shell in /etc/passwd (e.g., /bin/bash, /bin/zsh, /usr/sbin/nologin). |
-d | --home HOME_DIR | Update the user's home directory path in /etc/passwd (combine with -m to move existing files). |
-m | --move-home | Move the contents of the user's current home directory to the new location specified by -d. |
-l | --login NEW_LOGIN | Rename the user's login username from USERNAME to NEW_LOGIN. |
-L | --lock | Lock the user's password by prefixing the encrypted hash in /etc/shadow with an exclamation mark (!). |
-U | --unlock | Unlock the user's password by removing the leading '!' prefix from the hash in /etc/shadow. |
-e | --expiredate EXPIRE_DATE | Set the account expiration date in YYYY-MM-DD format (or pass 1 to expire immediately, '' to clear). |
-u | --uid UID | Change the numeric User ID (UID) of the account and update ownership inside the home directory. |
-c | --comment COMMENT | Update the GECOS comment field (full name, team, or description) in /etc/passwd. |
Two flag combinations deserve special attention in production: -aG and -d ... -m. First, whenever adding supplementary groups, always combine -a with -G (-aG); passing -G alone replaces the user's entire supplementary group list. Second, whenever changing a user's home directory with -d /new/path, always include -m (--move-home) if the old directory already exists; without -m, usermod only rewrites the string in /etc/passwd and leaves the user's files stranded at the old path.
When locking a user account for security reasons, understand that usermod -L only disables password authentication by prepending ! to the hash in /etc/shadow—it does NOT block SSH public key authentication! To completely disable both password and SSH key logins, combine -L with -e 1 (account expiration) and -s /usr/sbin/nologin.
When combining short flags on the command line, POSIX and GNU utilities allow clustering boolean flags behind a single hyphen, provided any option taking an argument appears last. In automated shell scripts, using long-form GNU options where portable makes self-documenting code that is easier to audit during peer review.
Practical Basic and Advanced Usage Examples
1. Change a User's Interactive Login Shell (-s)
Switch a user's default login shell to /bin/zsh (or lock a daemon account with /usr/sbin/nologin).
sudo usermod -s /bin/zsh alex
getent passwd alex
# Illustrative output (reviewed against documentation)
alex:x:1001:1001:DevOps Engineer:/home/alex:/bin/zsh
The seventh field of /etc/passwd is updated to /bin/zsh, which will launch automatically the next time alex connects via SSH.
2. Move a User's Home Directory to a New Location (-d and -m)
Migrate alex's home directory from /home/alex to /srv/homes/alex, moving all hidden dotfiles and subdirectories automatically.
sudo mkdir -p /srv/homes
sudo usermod -d /srv/homes/alex -m alex
getent passwd alex
ls -ld /srv/homes/alex
# Illustrative output (reviewed against documentation)
alex:x:1001:1001:DevOps Engineer:/srv/homes/alex:/bin/zsh
drwxr-x--- 4 alex alex 4096 Oct 22 16:20 /srv/homes/alex
Combining -d /srv/homes/alex with -m moves the directory contents across filesystems if necessary, deletes /home/alex, and updates /etc/passwd.
3. Rename a User Account, Home Directory, and Primary Group (-l, -d -m)
When an employee changes their username from alex to alexander, rename the login, move the home directory to match, and rename their primary group with groupmod -n.
sudo usermod -l alexander -d /home/alexander -m -c "Alexander Chen" alex
sudo groupmod -n alexander alex
id alexander
# Illustrative output (reviewed against documentation)
uid=1001(alexander) gid=1001(alexander) groups=1001(alexander),4(adm),27(sudo),1005(developers)
usermod -l alexander renames the user in /etc/passwd, /etc/shadow, and supplementary lists in /etc/group, while keeping UID 1001 unchanged so all file ownership remains intact.
4. Lock and Unlock a User Account (-L, -U, and -e)
Temporarily disable an account during a security investigation and subsequently restore access.
# Lock password and expire account immediately (blocks SSH keys too):
sudo usermod -L -e 1 alexander
sudo passwd -S alexander
# Unlock password and remove expiration date:
sudo usermod -U -e "" alexander
sudo passwd -S alexander
# Illustrative output (reviewed against documentation)
alexander L 10/22/2026 0 99999 7 -1
alexander P 10/22/2026 0 99999 7 -1
In passwd -S output, L indicates a locked password hash (! prefix in /etc/shadow) and P indicates a usable password. Passing -e 1 sets the account expiration to January 2, 1970 (expired), and -e "" clears the expiration.
5. Change a User's Numeric UID and Primary GID (-u and -g)
Synchronize a local user's UID and GID to 2001 so permissions align with a shared NFSv3 export or Docker volume.
sudo groupmod -g 2001 alexander
sudo usermod -u 2001 -g 2001 alexander
id alexander
# Illustrative output (reviewed against documentation)
uid=2001(alexander) gid=2001(alexander) groups=2001(alexander),4(adm),27(sudo),1005(developers)
groupmod -g 2001 updates the group's GID first, and usermod -u 2001 -g 2001 updates the user's UID/GID in /etc/passwd while recursively updating file ownership inside /home/alexander.
6. Grant Linux Sub-UIDs and Sub-GIDs for Rootless Podman/Docker (-v and -w)
Modern usermod supports adding subordinate UID (-v / --add-subuids) and subordinate GID (-w / --add-subgids) ranges in /etc/subuid and /etc/subgid for rootless containers.
sudo usermod --add-subuids 100000-165535 --add-subgids 100000-165535 alexander
grep alexander /etc/subuid /etc/subgid
# Illustrative output (reviewed against documentation)
/etc/subuid:alexander:100000:65536
/etc/subgid:alexander:100000:65536
Allocating 65,536 subordinate IDs allows alexander to run rootless Podman or Docker containers with user namespace isolation.
Real-World Workflows, Pipelines, and Shell Automation
Migrate a User's UID and GID Across the Entire Filesystem Safely
Because usermod -u only updates file ownership inside the user's home directory, files owned by the old UID/GID in /var, /srv, /opt, or /tmp must be updated with find. This workflow performs a complete system-wide UID/GID migration.
#!/usr/bin/env bash
set -euo pipefail
USER_NAME="alexander"
OLD_UID="$(id -u "$USER_NAME")"
OLD_GID="$(id -g "$USER_NAME")"
NEW_UID=3001
NEW_GID=3001
sudo loginctl terminate-user "$USER_NAME" 2>/dev/null || true
sudo groupmod -g "$NEW_GID" "$USER_NAME"
sudo usermod -u "$NEW_UID" -g "$NEW_GID" "$USER_NAME"
sudo find /var /srv /opt /tmp -xdev -uid "$OLD_UID" -exec chown -h "$NEW_UID" {} +
sudo find /var /srv /opt /tmp -xdev -gid "$OLD_GID" -exec chgrp -h "$NEW_GID" {} +
id "$USER_NAME"
# Illustrative output (reviewed against documentation)
uid=3001(alexander) gid=3001(alexander) groups=3001(alexander),4(adm),27(sudo),1005(developers)
Passing -h (--no-dereference) to chown and chgrp ensures symbolic links themselves are updated rather than their targets, preventing symlink traversal vulnerabilities.
Emergency Account Quarantine Script for Incident Response
During a suspected credential compromise, immediately quarantine a user account so neither password logins, SSH keys, nor cron jobs can execute.
#!/usr/bin/env bash
set -euo pipefail
COMPROMISED_USER="${1:?Usage: $0 USERNAME}"
sudo usermod -L -e 1 -s /usr/sbin/nologin "$COMPROMISED_USER"
sudo loginctl terminate-user "$COMPROMISED_USER" 2>/dev/null || true
sudo pkill -9 -u "$COMPROMISED_USER" 2>/dev/null || true
sudo chage -l "$COMPROMISED_USER" | grep "Account expires"
# Illustrative output (reviewed against documentation)
Account expires : Jan 02, 1970
Combining -L (password lock), -e 1 (PAM account expiry), and -s /usr/sbin/nologin (shell block) shuts down every interactive and non-interactive authentication vector while preserving forensic evidence in the user's home directory.
Pipelines, Redirection, Quoting, and Shell Expansion
When embedding usermod inside shell pipelines (|), conditional chains (&& and ||), or command substitutions ($(...)), the calling shell performs tilde expansion, variable interpolation, and globbing before invoking the binary. Always wrap paths, patterns, and variables in quotes so whitespace and wildcard characters (*, ?) are not expanded prematurely, and use set -euo pipefail in unattended scripts.
Permissions, Security Implications, and Safe Execution
Running usermod -G without -a overwrites all existing supplementary groups. Furthermore, running usermod -L alone does NOT prevent SSH public-key logins—always combine -L with -e 1 and -s /usr/sbin/nologin when disabling an account.
When renaming an account with usermod -l newname oldname, verify /etc/sudoers and /etc/sudoers.d/* as well as /var/spool/cron/crontabs/. While usermod -l updates /etc/passwd, /etc/shadow, and /etc/group, it does NOT rewrite hardcoded usernames inside sudoers rules or personal crontab filenames.
Whenever assigning a new login shell with usermod -s, ensure the target binary path is listed in /etc/shells (for interactive shells) so PAM modules like pam_shells.so and FTP/SSH daemons recognize it properly.
GNU/Linux, BSD, and BusyBox Implementation Differences
usermod is pre-installed on all standard GNU/Linux distributions (Ubuntu, Debian, RHEL, Rocky Linux, Fedora, SUSE, Arch). The -rG flag for removing supplementary groups requires shadow-utils 4.11 or newer (Ubuntu 22.04+, RHEL 9+); on older releases, use gpasswd -d user group.
On Alpine Linux, BusyBox does not include usermod by default—run apk add shadow to install it. On FreeBSD, the equivalent command is pw usermod, and on macOS account attributes are modified via dscl . -change /Users/username ....
Troubleshooting Common Errors and Diagnostics
usermod: user username is currently used by process 1842
Root Cause: You attempted to change the user's UID (-u), login name (-l), or home directory (-d -m) while the user still has active processes running (such as an SSH session or systemd --user instance).
Resolution: Run this command from a separate root/admin account after terminating the target user's sessions with sudo loginctl terminate-user username.
sudo loginctl terminate-user username
sudo pkill -9 -u username
sudo usermod -l newname -d /home/newname -m username
Locked user with 'usermod -L' can still log in over SSH
Root Cause: usermod -L only invalidates the password hash in /etc/shadow. OpenSSH PubkeyAuthentication bypasses password hashes and checks account expiration via PAM instead.
Resolution: Expire the account in PAM using sudo usermod -L -e 1 -s /usr/sbin/nologin username.
sudo usermod -L -e 1 -s /usr/sbin/nologin username
usermod: directory /home/newpath exists
Root Cause: When running usermod -d /home/newpath -m, usermod refuses to move the home directory if the destination directory /home/newpath already exists on disk.
Resolution: Ensure /home/newpath does not exist before running usermod -d /home/newpath -m (only the parent directory /home should exist).
sudo rmdir /home/newpath
sudo usermod -d /home/newpath -m username
Alternatives, Related Commands, and Related Tutorials
- How to Create Users in Linux with the useradd Command — Practical syntax, flags, and examples for
useradd. - How to Remove a User in Linux with the userdel Command — Practical syntax, flags, and examples for
userdel. - How to Add a User to a Group in Linux (usermod and gpasswd) — Practical syntax, flags, and examples for
usermod. - passwd Command in Linux: Change User Passwords and Manage Expiration — Practical syntax, flags, and examples for
passwd. - 25 Best Linux Commands Every Sysadmin Should Know — Core command-line utilities for daily Linux server administration.
- How to Find a File in Linux from the Command Line — Practical file search patterns across Linux directories.
- Linux Commands Knowledge Hub (#linux-commands) — Browse all Linux command-line tutorials and system administration references.
Authoritative References and Official Documentation

The Linux Command Line, 2nd Edition: A Complete Introduction
Frequently Asked Questions
Quick answers to common questions and configurations
/home/new_username -m old_username followed by sudo groupmod -n new_username old_username./etc/shadow with '!', which blocks password authentication. To also block SSH key logins, expire the account with sudo usermod -L -e 1 -s /usr/sbin/nologin username./bin/zsh username (or /bin/bash). Alternatively, users can change their own login shell interactively using chsh -s /bin/zsh./tmp, /var, or /srv) must be updated manually using find / -uid OLD_UID -exec chown NEW_UID {} +.Editorial Disclosure: Website Vidya is supported by our readers. When you purchase hosting, tools, or software through our verified partner links, we may earn an affiliate commission at no extra cost to you. We only recommend solutions we trust and rigorously verify.
Rajesh Ray
I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.
Most Read Guides
TrendingUbuntu Server Not Booting? 10 Simple Steps to Fix It Fast
How to Create a 3D Button in HTML and CSS: Easy Steps
How to Install Python and Pip on Ubuntu 24.04 LTS
How to Install Nginx on Ubuntu 24.04
How to Install Python on Ubuntu 24.04 LTS
Related Technical Guides
Tested runbooks, server administration walkthroughs, and troubleshooting guides

Join the Conversation & Perspective
Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.
No comments on this guide yet. Be the first to share your thoughts or ask a question!