usermod

On this page 7

usermod is the Linux command-line utility that modifies an existing user account by rewriting its records in the system account databases. In the Permissions topic of Linux administration, it is the standard tool for changing group membership, the home directory, the login shell, the user identifier and the account’s locked or expired state without deleting and recreating the user.

Because group membership decides which files, devices and privileged commands a person can reach, usermod is one of the most consequential permission tools on a system. A single mistyped option can silently remove a user from every supplementary group, which is why experienced administrators treat it with the same care as chmod or sudoers edits.

How usermod rewrites account records

usermod works by editing the shadow-utils databases in place: /etc/passwd for the account entry, /etc/shadow for password aging and locking, /etc/group and /etc/gshadow for group membership. It belongs to the shadow-utils package together with useradd, userdel, groupadd and passwd, and it requires root privileges, normally obtained through sudo.

Before writing, the command takes lock files on the databases so that two administrative tools cannot corrupt them at the same moment. It then writes a new copy and renames it over the original, an atomic replacement that leaves either the old or the new version but never a half-written file. Settings such as the valid UID ranges and the mail spool location come from /etc/login.defs.

The following table maps each database to the usermod options that change it.

FileWhat it storesOptions that change it
/etc/passwdLogin name, UID, primary GID, comment, home, shell-l, -u, -g, -c, -d, -s
/etc/shadowPassword hash, aging, expiry, lock marker-L, -U, -e, -f, -p
/etc/groupGroup names, GIDs and member lists-G, -a, -r
/etc/gshadowGroup passwords and group administrators-G, -a, -r
Files changed by usermod

Key usermod options and what they change

Each option targets one attribute of the account, and several options can be combined in a single invocation. The most frequently used ones are summarized below with their practical effect.

OptionEffectTypical use
-aG groupAppends supplementary groupsGrant access to docker or wheel
-G groupReplaces the supplementary group listDefine the exact group set
-g groupChanges the primary groupAlign new files with a project group
-d dir -mChanges and moves the home directoryRelocate home to another volume
-s shellChanges the login shellSwitch to zsh or disable login
-l nameRenames the loginCorrect a naming convention
-u UIDChanges the user identifierHarmonize UIDs across servers
-L / -ULocks or unlocks the passwordSuspend a departing employee
-e dateSets the account expiry dateTime-limit contractor access
Common usermod options

Newer releases add -r to remove the user from listed groups and -v/-w to manage subordinate UID ranges for rootless containers. Option availability varies by shadow-utils version, so administrators should check the local manual page before scripting.

The append flag and group membership

The single most important rule about usermod is that -G without -a replaces the entire supplementary group list. Running usermod -G docker alice leaves alice in the docker group only, removing her from wheel, audio or any project group she held before. The safe form is usermod -aG docker alice, which appends without removing existing memberships.

Group changes also do not affect running sessions. A process inherits its group list at login, so the user must log out and back in, or start a new shell with newgrp or su -, before the new group appears in the output of id. Long-lived services must be restarted for the same reason.

The difference between the group-related options is easiest to see side by side.

Command formBeforeAfter
usermod -G docker alicewheel, audiodocker
usermod -aG docker alicewheel, audiowheel, audio, docker
usermod -rG audio alicewheel, audiowheel
usermod -g dev alicePrimary group alicePrimary group dev
Group options compared

How usermod differs from neighboring account tools

usermod changes accounts that already exist, while its siblings create, delete or adjust other parts of the identity model. Choosing the right tool avoids side effects such as orphaned files or bypassed policy checks.

CommandScopeWhen to prefer it
useraddCreates a new accountOnboarding a new user
usermodModifies an existing accountChanging groups, shell, home or UID
userdelRemoves an accountOffboarding after data handover
passwdPassword and its agingSetting or resetting a password
gpasswdSingle group membershipAdding or removing one member
chown / chgrpFile ownershipFixing ownership after a UID change
usermod and related commands

Distributions such as Debian also ship adduser and deluser, friendlier wrappers with their own defaults. On systems joined to LDAP, FreeIPA or Active Directory, usermod edits only local files and cannot change directory-managed accounts; those must be changed in the identity service.

Limitations, risks and common mistakes

usermod refuses to change an account with running processes for several options, notably -u, -l and -d -m, because live processes would keep the old identity. Administrators stop the user’s sessions first, or the command fails with a message that the user is currently used by a process.

A UID change rewrites ownership only inside the home directory and the mail spool. Files elsewhere, such as in /srv, /var/www or shared volumes, keep the old numeric owner and appear orphaned until fixed with find / -uid OLD -exec chown. Renaming a login with -l likewise does not rename the home directory or the user’s private group.

  • Forgetting -a with -G, which strips every other supplementary group.
  • Expecting immediate effect, although group changes apply only to new sessions.
  • Locking with -L only, which blocks password login but not SSH key authentication; combine it with an expiry date or a nologin shell.
  • Passing a cleartext password to -p, which expects an already hashed value and exposes secrets in shell history.
  • Editing directory-managed users locally, which has no effect on centrally managed accounts.

Put simply

Every person who uses a Linux computer has an account. Think of it as a profile card. It says who they are, where their folder lives and which teams they belong to. usermod is the tool an administrator uses to update that profile card without making a new one.

Belonging to a team, called a group, opens doors to shared folders and special abilities. The main rule is simple. usermod must be told to add a team rather than replace all teams. Otherwise the person suddenly loses access to everything else.

In practice

A systems administrator at a mid-sized software company manages a build server shared by twelve developers. A new developer, already in the wheel and dev groups, needs to run containers, so the administrator runs sudo usermod -aG docker mkovac and asks the developer to log out and back in. A quick id mkovac confirms all three groups are present.

Two months later a contractor’s engagement ends. Instead of deleting the account and losing audit history, the administrator runs usermod -L -e 1 -s /usr/sbin/nologin contractor1, which locks the password, expires the account and disables the shell. Because expiry also blocks SSH key logins, access is fully closed while the files remain available for handover.