usermod Linux Command
Modifies an existing user account (groups, shell, home directory, etc.).
What does usermod do?
Modifies an existing user account (groups, shell, home directory, etc.).
Syntax
usermod [options] usernamePractical example
sudo usermod -aG sudo johnQuestions about usermod
How do I add a user to a group?
Use `sudo usermod -aG groupname username` to add a user to a group. The `-a` flag appends without removing from other groups.
What does usermod -aG do?
The `-a` flag means append and `-G` specifies supplementary groups. Without `-a`, the user would be removed from all other groups.
How do I change a user's shell?
Use `sudo usermod -s /bin/zsh username` to change the login shell. Use `chsh` as an alternative command.
How do I lock a user account?
Use `sudo usermod -L username` to lock the account. This disables password login while keeping the account intact.
How do I unlock a user account?
Use `sudo usermod -U username` to unlock a previously locked account.
How do I change a user's home directory?
Use `sudo usermod -d /new/home -m username` to change and move the home directory. The `-m` flag moves existing files.
How do I change a user's primary group?
Use `sudo usermod -g groupname username` to set the primary group. This is different from adding supplementary groups with `-G`.
How do I set an account expiry date?
Use `sudo usermod -e YYYY-MM-DD username` to set when the account expires and becomes disabled.
Related User Management
useraddCreates a new user account on the system.
userdelDeletes a user account. Use -r to also remove the home directory.
passwdChanges the password for a user account.
groupaddCreates a new group on the system.
sudoExecutes a command with superuser (root) privileges.
groupdelDeletes an existing group from the system.