Account
Categories

usermod command


Definition:

This command modifies the information of an existing user account in the system.

Whenever there is a need to update details such as the home directory, shell, group, or password settings, the admin uses this command.

It makes it easy to update and manage user accounts without deleting or removing them.

Syntax:

usermod [options] username

Examples:

Example 1 – Change the login name of a user

usermod -l shobha_new shobha

Output: (no output if successful)

Here, before the login name of a user, shobha is changed to shobha_new using the option -l.

Example 2 – Change the home directory of a user

usermod -d /home/prabha_new -m prabha

Output: (no output if successful)

Here, the home directory of the user Prabha was changed to /home/prabha_new using the -m option.

Example 3 – Add a user to a new group

usermod -aG developers ravi

Output: (no output if successful)

Here, Ravi was added to the developers group as a secondary group, allowing him to access resources assigned to this group.

Example 4 – Change the login shell of a user

usermod -s /bin/bash kavita

Output: (no output if successful)

To check it:

cat /etc/passwd | grep kavita

Output:

kavita:x:1004:1004::/home/kavita:/bin/bash

Here, Kavita’s default shell was changed to /bin/bash, allowing her to use this shell when she logs in.

Options:

  • -l NEW_LOGIN → If you use this option, it changes the login name of the user.
  • -d /path/to/home → If you use this option, it changes the home directory of the user.
  • -m → If you use this option with -d, it moves the content of the old home directory to the new one.
  • -s SHELL → If you use this option, it changes the default shell of the user.
  • -g GROUP → If you use this option, it changes the primary group of the user.
  • -aG GROUP1, GROUP2 → If you use this option, it adds the user to additional (secondary) groups without removing the existing ones.
  • -u UID → If you use this option, it changes the user ID (UID) of the user.
  • -c comment → If you use this option, it updates the description or comment for the user.
  • -L → If you use this option, it locks the user account.
  • -U → If you use this option, it unlocks the user account.