How to Modify Linux Accounts with usermod
The usermod (user modify) command is a core
administrative utility in the Linux operating system designed to alter
existing user account properties. Operating with superuser
(root) privileges, this command updates system files such
as /etc/passwd, /etc/shadow, and
/etc/group directly from the terminal without requiring
manual file editing. This guide explains how usermod
functions internally to modify user accounts, detailing its primary
syntax and the standard options used to update account attributes,
manage group memberships, relocate home directories, and control account
security.
How usermod Works
Under the Hood
When a user account is created, Linux stores account properties across several key files:
/etc/passwd: Stores basic account details such as the username, User ID (UID), primary Group ID (GID), home directory path, and default login shell./etc/shadow: Stores encrypted password data, account expiration dates, and password aging policies./etc/group: Defines supplementary group memberships.
The usermod command parses the options supplied by an
administrator, performs integrity and validation checks (such as
verifying that a target UID or group exists), and safely applies the
updates directly to these files.
Managing User Identification and Names
To change a user's login name, use the -l (login) flag.
This updates the username in /etc/passwd while keeping the
user's UID intact:
sudo usermod -l new_username old_usernameTo alter the unique numeric identifier (UID) assigned to an account,
use the -u flag:
sudo usermod -u 1500 usernameNote: Modifying a UID with -u automatically updates
the ownership of files residing in the user's home directory, but files
located elsewhere on the filesystem will retain the old UID and require
manual ownership correction.
Managing Group Memberships
Users belong to one primary group and can belong to multiple supplementary groups.
Change the primary group (
-g):sudo usermod -g developers usernameThis alters the user’s primary GID in
/etc/passwd.Add to supplementary groups (
-aG):sudo usermod -aG sudo,docker usernameThe
-Gflag defines supplementary groups, while the-a(append) flag ensures the user is added without being removed from their existing groups. Omitting-awill overwrite existing group memberships.
Relocating the Home Directory and Changing Default Shells
Update the home directory (
-dand-m): To define a new home directory path and move existing files to the new location, combine the-d(directory) and-m(move) flags:sudo usermod -d /home/new_path -m usernameChange the default login shell (
-s): To assign a different default shell, point the-sflag to a valid executable listed in/etc/shells:sudo usermod -s /bin/zsh usernameUpdate user information/comment (
-c): The GECOS field in/etc/passwdstores metadata such as full names or department info. Update it using:sudo usermod -c "Jane Doe, IT Department" username
Managing Account Security and Expiration
usermod provides flags to manage login capabilities
directly within /etc/shadow:
Lock an account (
-L):sudo usermod -L usernameThis inserts an exclamation point (
!) in front of the encrypted password in/etc/shadow, disabling password-based authentication.Unlock an account (
-U):sudo usermod -U usernameThis removes the exclamation mark, restoring password access.
Set an account expiration date (
-e):sudo usermod -e 2026-12-31 usernameThis defines the exact date (in
YYYY-MM-DDformat) after which the account is disabled. Passing an empty string (-e "") removes the expiration requirement.