The usermod command changes an existing account: its groups, login shell, home directory, user name, UID, expiry date and lock state. The option you use most is sudo usermod -aG groupname username, which appends a supplementary group without removing the others. This guide explains the usermod command options, the dangerous ones, and the order of operations when you rename or move an account.
It edits /etc/passwd, /etc/shadow and /etc/group for you, so it is safer than editing those files by hand.
usermod takes an option and the user name as the last argument. Run it as root. The user should not be logged in or running processes when you change the name, UID or home directory, otherwise the command refuses or leaves processes in an inconsistent state.
| Option | What it changes |
|---|---|
-aG LIST | Append supplementary groups |
-G LIST | Replace the complete supplementary group list |
-g GROUP | Primary group |
-s SHELL | Login shell |
-d DIR | Home directory (add -m to move the files) |
-l NEW | Login name |
-u UID | Numeric user ID |
-c TEXT | Comment (full name) |
-L, -U | Lock and unlock the password |
-e DATE | Account expiry date |
-f DAYS | Days after password expiry before the account is disabled |
The -a flag is essential. This command is a classic trap:
It silently drops alice from sudo, docker and anything else. If that happens, add the lost groups back with -aG. When you want to remove a single group, use gpasswd -d alice docker, because usermod has no remove-one option on most versions (recent shadow-utils releases add -r together with -G, but check your own man page).
Changes apply to new login sessions. Log out and in again to see them in the shell.
The new shell must be listed in /etc/shells for tools like chsh to accept it, and it must exist on disk, otherwise the user cannot log in. Set /usr/sbin/nologin to block interactive logins while keeping the account for services.
Renaming is a multi-step job, because the home directory and groups do not rename automatically.
sudo pkill -u alice.sudo usermod -l alicia alice.sudo usermod -d /home/alicia -m alicia.sudo groupmod -n alicia alice.sudoers entries, cron jobs, SSH authorized_keys comments and mail aliases.Without -m, -d only edits the path in /etc/passwd. The files stay where they were, and the user logs in to a missing directory.
usermod updates ownership of the files in the user's home directory, but not files elsewhere on the system. Find and fix those yourself, using the old UID that you noted before the change:
Because this is easy to get wrong, do it only for a good reason, such as aligning UIDs across servers that share an NFS export. Take a backup first, and never reuse a UID that belonged to a deleted account without checking for leftover files.
A lock only affects password authentication. A user with an SSH key can still log in on many configurations, so combine it with -e 1 or a nologin shell when you need to block access fully.
Always confirm the result from the system databases:
If something looks wrong, sudo pwck and sudo grpck check the consistency of the account and group files.
usermod -G and usermod -aG?-G sets the supplementary group list to exactly what you give, removing the rest. -aG appends the groups to the existing list, which is almost always what you want.
You are renaming or moving an account that has running processes. Log the user out, stop their processes, then run the command again.
Only when you add -m. Without it the path changes in /etc/passwd and the existing files stay in the old location.
Use sudo gpasswd -d username groupname, or on Debian and Ubuntu sudo deluser username groupname.
usermod edits an existing account safely. Use -aG to add groups, -s for the shell, -l and -d ... -m to rename and move, -L and -U to lock and unlock, and -e for expiry. Never use -G without -a unless you mean to replace all groups, and verify every change with id, getent and chage.