Add a User to a Group: -aG
A warning first: always use -a together with -G. Without -a, -G replaces the user's whole list of extra groups, and the user leaves every group you did not name. If that includes sudo on Debian and Ubuntu or wheel on AlmaLinux and Rocky Linux, the account loses its admin rights. The safe form:
id deploy
sudo usermod -aG docker deploy
sudo usermod -aG www-data,adm deploy
id deploy
Check the groups with id before and after. To remove a user from one group only, use sudo gpasswd -d deploy docker. The full walkthrough is in how to add a user to a group in Linux.
Group Changes Apply at the Next Login
Running sessions keep the group list they started with. The user has to log out and back in, or run newgrp docker to open a shell with the new group. id deploy reads the account database and shows the new list at once; plain id shows the groups of your current session.
Change the Login Name: -l
sudo usermod -l newname oldname
sudo groupmod -n newname oldname
-l changes only the login name. The home directory and the user's own group keep the old name, which is why the second line renames the group. usermod refuses while the user is logged in or has running processes, so do this from another admin account. Afterwards, check the sudoers file for rules that name the old user.
Move the Home Directory: -d and -m
sudo usermod -d /home/newname -m newname
-d alone only changes the path stored for the account. -m also moves the contents, including ~/.ssh with the user's keys. For a large or important home directory, take a backup before the move.
Change the Login Shell: -s
cat /etc/shells
sudo usermod -s /bin/bash deploy
sudo usermod -s /usr/sbin/nologin backup
/etc/shells lists the valid login shells. A service account that should never get a shell gets nologin; on current releases of all four distributions, /sbin points to /usr/sbin, so either path works.
Lock and Unlock: -L and -U
sudo usermod -L deploy
sudo passwd -S deploy
sudo usermod -U deploy
-L puts an exclamation mark in front of the password hash, so password logins fail. With the default SSH settings of these distributions it does not stop SSH key logins, because key authentication does not check the password. To block the account completely, also expire it, which you undo with -e '':
sudo usermod -L -e 1 deploy
-e 1 sets the expiry date to the second day of 1970, which is long past. Never lock or expire the account you log in with, or the only account that has sudo: you could lose all access to the server.
Change the UID: -u
sudo usermod -u 1500 deploy
usermod updates the ownership of files inside the user's home directory. Files elsewhere, such as a web root or data directory, keep the old number and show it in ls -l instead of a name. Fix them with the old UID, here 1001:
sudo find / -xdev -user 1001 -exec chown -h deploy {} +
The user must not be running processes during the change. Group IDs are changed with groupmod -g.
Options at a Glance
| Option | Changes |
|---|---|
-aG grp | Adds groups |
-l new | Login name |
-d dir -m | Home, moves files |
-s shell | Login shell |
-L, -U | Locks, unlocks password |
-e date | Account expiry |
-u uid | User ID |
-c text | Comment field |
To create accounts in the first place, see useradd vs adduser.