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

Output:

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

Output:

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

Output:

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

Output:

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

Output:

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

Output:

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

Output:

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

Output:

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

Output:

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

Output:

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

Output:

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

Output:

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

Output:

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.