Skip to content

usermod Command Cheat Sheet

usermod modifies the system account files to reflect the changes that are specified on the command line.


Synopsis

usermod [options] LOGIN

Managing Groups

Add User to Group (Append) - CRITICAL

Always use -a (append) with -G (groups). If you omit -a, the user is removed from all other groups not listed!

# Correct: Adds john to sudo
sudo usermod -aG sudo john

# Incorrect: Removes john from everything else, sets only sudo
sudo usermod -G sudo john

Account Status

Lock Account (-L)

Disable password login (puts a ! in /etc/shadow).

sudo usermod -L john

Unlock Account (-U)

sudo usermod -U john

Set Expiration Date (-e)

Format: YYYY-MM-DD.

# Expire account on Jan 1, 2026
sudo usermod -e 2026-01-01 john

modifying Properties

Change Login Name (-l)

# Rename 'john' to 'jonathan'
sudo usermod -l jonathan john

Change Home Directory (-d)

# Change config to point to new location
sudo usermod -d /var/www/homes/john john

# Move contents (-m) from old home to new home
sudo usermod -m -d /new/home john

Change UID (-u)

sudo usermod -u 1005 john
Note: Updates file ownership in the user's home directory automatically.


Notes

  • Modifying Active Users: You cannot change the name or UID of a user who is currently logged in.