Users

The usermod command

Modifies an account: groups, shell, home folder, expiry and password lock.

Syntax

sudo usermod [-aG grupos] usuarioTry it in the terminal →

Most useful options

-G, --groups <grupos>List of supplementary groups, separated by commas with no spaces. Without -a, it replaces the current ones
-a, --appendAdds the groups from -G to the ones it already has. Only works together with -G
-s, --shell <shell>Changes the login shell. An empty string returns the system default shell
-d, --home <carpeta>Changes the home folder in the account. Without -m, it does not move the files
-m, --move-homeMoves the contents of the home folder to the new one. Only valid together with -d
-L, --lockLocks the password by putting a ! in front of the hash. It does not close the account completely: for that, the manual advises also setting -e 1
-U, --unlockRemoves that ! and unlocks the password
-e, --expiredate <fecha>Date on which the account is disabled, in YYYY-MM-DD format. With -1 or an empty string it is removed

Copy-ready examples

$ sudo usermod -s /bin/bash alumnochanges alumno's shell to bash. It is the safest change to try.
$ sudo usermod -aG sudo alumno && groups alumnoadds alumno to the sudo group without removing the groups it already had, and checks it.

Example output

alumno : alumno sudo
$ sudo usermod -L alumnolocks alumno's password. To unlock it later: sudo usermod -U alumno.
$ sudo usermod -e 2027-06-30 alumnothe account is disabled from that date onwards.
$ sudo usermod -d /home/nuevo -m alumnomoves the home folder to /home/nuevo. First check that the account has no running processes, because the manual requires it when the home folder changes.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
usermod: user 'alumno' does not existThe account does not exist or the name is misspelled.Check the account with getent passwd alumno.
usermod: group 'equipo' does not existA group given with -G or -g does not exist. Groups must exist before they can be assigned.Create it with sudo groupadd equipo and run the command again.
usermod: -a flag is only allowed with the -G flagYou used -a without -G. Without a group list there is nothing to add.Write -aG group user to add. If you want to replace the groups, use -G with the full list.
usermod: no optionsYou ran the command without any change option.Add at least one option, for example -s or -aG. The help appears with usermod --help.

Typical mistakes

  • Without -a, -G replaces all supplementary groups and removes the user from any group you do not list. To add, always use -aG.
  • -L locks only the password. To fully close the account, also set the expiry with -e 1, as the manual says.
  • If you change the UID, the name or the home folder while the user has running processes, the result can be inconsistent. Close their session first.

Differences from similar commands

  • chsh: changes only the shell, the same as usermod -s, aimed at the user themselves. usermod also handles groups, expiry and locking.
  • passwd: changes the password. usermod -L and -U only add or remove the ! in front of the hash.
  • groupadd: creates groups. usermod only puts users into groups that already exist.

Related commands

FAQ

Why do I not see the new group in my session?

Groups are read at login. Log out and back in, or use newgrp group to activate it in the current session.

What happens to its files if I change the UID?

The files in its home folder and its mailbox change owner automatically. Files outside that folder you have to change yourself.

What is the difference between -d and -m?

-d changes the path stored in the account. Without -m, the files stay in the old folder. -m moves them, and it only works together with -d.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course