Beginners Guide for Usermod Command in 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
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Prerequisitesusermod, grep, which, su, groups
Internet RequiredNo
Discussed Tools in this Articlevim, nano, zsh, fish

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.

Also Read: How to Create a Normal User in Linux

Changing a Username

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

$ sudo usermod -l newuser olduser

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
newuser:x:1005:1009::/home/olduser:/bin/sh

Also Read: Beginners Guide for ID Command in Linux

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.

#User UID before changing
$ grep newuser /etc/passwd
newuser:x:1005:1009::/home/olduser:/bin/sh

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

#User with new UID
$ grep newuser /etc/passwd
newuser:x:666:1009::/home/olduser:/bin/sh

While specifying the new UID, make sure it’s not assigned to another user.

Also Read: 3 Ways to Change Userโ€™s Login Shell in Linux

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
/usr/bin/zsh

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
newuser:x:666:1009::/home/olduser:/usr/bin/zsh

Also Read: How to Add User to Sudo Group

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
newuser: newuser sudo

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
usermod: directory /home/newhomedirectory exists

To verify the changes check the “/etc/passwd” file.

$ grep newuser /etc/passwd
newuser:x:666:1009::/home/newhomedirectory:/usr/bin/zsh

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
Password: 
su: Authentication failure

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

$ sudo usermod -U newuser

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
newuser:newpassword:19321:0:99999:7:::

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
newuser: newgroup sudo

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
newuser:x:666:1004:This is Linux TLDR:/home/newhomedirectory:/usr/bin/zsh

Also Read: Beginners Guide for Gpasswd Command in Linux

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
Last password change				                                   	: Nov 25, 2022
Password expires				                                       	: never
Password inactive					                                        : never
Account expires					                                   	: Dec 26, 2023
Minimum number of days between password change		: 0
Maximum number of days between password change		: 99999
Number of days of warning before password expires	        : 7

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

$ sudo usermod -e "" newuser

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.