Beginners Guide for Usermod Command on Linux

Linux TLDR
Last Updated:
Reading time: 4 minutes

The usermod command is a user management tool to modify user account information like username, user ID, default shell, home directory, and many more.

Tutorial Details

DescriptionUsermod (Modify User Information)
Difficulty LevelModerate
Root or Sudo PrivilegesYes
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesusermod, grep, which, su, groups
Internet RequiredNo

Syntax of the Usermod Command

The usermod command requires two arguments: one is the option, and the other is the username.

$ sudo usermod [OPTION] [USERNAME]

The above command has the ability to modify the following files (or databases):

  • /etc/passwd file stores the user account related information.
  • /etc/group file stores the system group information with GID.
  • /etc/shadow file stores user security-related information.
  • /etc/gshadow file stores group security-related information.
  • /etc/login.defs file is for shadow password suite configuration.
  • /etc/subgid file stores the per user subordinate group IDs.
  • /etc/subuid file stores the per user subordinate user IDs.

However, you can manually visit the above mentioned files and edit them using your choice of text editor, like Nano or Vim, but usermod gives you the power to manage each of these files using one command.

Changing a Username

Use the β€œ-l” or β€œ--login” flag with your new username (ex: β€œnewuserβ€œ) and current username (ex: β€œolduserβ€œ) to change.

You cannot change the username of the current logged-in user. In order to perform changes, switch to a different or root account.
$ sudo usermod -l newuser olduser


Changing the username of the current user using usermod command

The above command will replace the β€œolduser” username with β€œnewuser” that you can verify by searching the username in the β€œ/etc/passwd” file using the grep command.

$ grep newuser /etc/passwd


Checking the passwd file to verify the username is changed

Changing a User ID

To replace the existing user’s UID with a new one, specify the new UID with your username using the β€œ-u” or β€œ--uid” flag.

While specifying the new UID, make sure it’s not assigned to another user.
#User UID before changing
$ grep newuser /etc/passwd

#Changing user UID from 1005 to 666
$ sudo usermod -u 666 newuser

#User with new UID
$ grep newuser /etc/passwd


Changing the user UID with custom value using usermod command

Changing a User Login Shell

By default, the Bash shell is used by major Linux distributions; however, if you prefer to have another shell like ZSH or Fish, you can make them your default login shell.

But before changing your current shell to ZSH or Fish, first find their full path using the which command.

$ which zsh


Checking the absolute path of zsh shell using which command

Than use the β€œ-s” or β€œ--shell” flag by specifying the full shell path with your username.

$ sudo usermod -s /usr/bin/zsh newuser

Check the β€œ/etc/passwd” file to verify the default shell is changed with new one.

$ grep newuser /etc/passwd


Changing user current shell to zsh and than verifying the changes by reading the passwd content

Add a User to the Supplementary Group

You can easily append your existing user to another group (ex: sudo) using the following command.

$ sudo usermod -aG sudo newuser

Use the groups command to verify the user is added to the specified group.

$ groups newuser


Adding user to sudo group using usermod command

To append multiple users in a group, specify each of them with a comma (β€œ,β€œ) as a separator.

Change a User’s Home Directory

The home directory might be crucial to change because you will lose all your β€œ.config” and β€œ.bashrc” data.

If you know what you’re doing, then specify the location for your new home directory along with your username in the following command.

$ sudo usermod -md /home/newhomedirectory newuser

To verify the changes check the β€œ/etc/passwd” file.

$ grep newuser /etc/passwd


Changing user home directory to custom path using usermod command

Lock and Unlock a User

To block the user’s access to their account, you can lock it by using the β€œ-L” or β€œ--lock” flag with their username.

$ sudo usermod -L newuser

If the user tries to access their account, they will get the following error.

$ su - newuser

To unlock the user account, use the β€œ-U” or β€œ--unlock” flag with their username.

$ sudo usermod -U newuser


Locking and unlocking user account using usermod command

Set a Password for a User (Unsecure)

You can use the passwd command to change a user’s password, but usermod also gives you the option to change your password using the β€œ-p” or β€œ--password” flag with a new password and username.

$ sudo usermod -p newpassword newuser

The above command will change the password for the specified user but this method is not recommended because it will save the password in plain text in β€œ/etc/shadow” file.

$ sudo grep newuser /etc/shadow


Changing user password in insecure way using the usermod command

Change the User’s Primary Group

The β€œ-g” or β€œ--gid” flag can be used to forcely change the user’s primary group to a specified group.

$ sudo usermod -g newgroup newuser

Verify the change by using the following command.

$ groups newuser


Replacing the user primary group using usermod command

Adding Comments to the User Account

The comments are extra pieces of information that are added to the specified user account to help other administrators by giving them account descriptions like the purpose of that account.

Use the β€œ-c” or β€œ--comment” flag and specify your comment with a username.

$ sudo usermod -c "This is Linux TLDR" newuser

Use the following command to verify the comment is added to the specified user account.

$ grep newuser /etc/passwd


Adding comment to user field in passwd file using usermod command

Set User Account Expiry Date

You can use the β€œ-e” or β€œ--expiredate” flags with an expiry date in β€œYYYY-MM-DD” format to disable the specified user account on that date.

$ sudo usermod -e 2023-12-26 newuser

Execute the chage command with the username to verify the user account expiry date.

$ sudo chage -l newuser


Setting expiry date to auto disable user account on that day using usermod command

To disable the account expiration, specify the flags with an empty date.

$ sudo usermod -e "" newuser


Removing expiry date from user account using usermod command

Here the end comes.

The article went into great detail; however, if you have any questions, feel free to ask them in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.