How to Use the usermod Command in Linux

March 4, 2021

Introduction

The usermod command is one of the several Linux commands system administrators have at their disposal for user management. It is used to modify existing user account details, such as username, password, home directory location, default shell, and more.

In this tutorial, you will learn how to use the usermod command in Linux to change user login information.

How to Use the usermod Command in Linux

Prerequisites

  • A system running Linux
  • Access to the command line

usermod Linux Syntax

The basic syntax for the usermod command is:

usermod [options] [username]

The usermod command modifies configuration files containing user account information. Those files are:

  • /etc/passwd – information regarding users’ accounts
  • /etc/shadow – user security-related information
  • /etc/group – information about groups
  • /etc/gshadow – group security-related information
  • /etc/login.defs – shadow password suite configuration

It is possible to edit the above-mentioned files directly, using a text editor such as nano or vim. However, usermod makes the process faster and more straightforward.

usermod Command Examples

Below is a list of usermod options, along with the option-specific syntax and examples.

Add Information to a User

Use usermod with the -c option to add a piece of information about a user to the /etc/passwd file. This helps identify the user and provides space for temporary user-related comments.

sudo usermod -c "[information]" [username]

Use the getent command to check the user-related entry in the /etc/passwd file.

Adding an informational message about a user using the usermod -c option

The entry now contains the comment added with usermod.

Set User’s Home Directory

When you create a user in Linux, the system automatically creates a home folder for them in /home/[username]. To change the location of the user’s home folder, use the -d option:

sudo usermod -d [directory-location] [username]
Changing the location of a user's home folder using the usermod -d option

The example above changed the user’s home folder from /home/mike to /var/mike. The getent command confirms the successful change.

The -d option does not move the home folder’s content to the new location. If the user has previously utilized the home folder to store their files, add the -m option to move the content of the user’s home directory:

sudo usermod -d [location] -m [username]

In the case of the user from the example above, the command looks like this:

sudo usermod -d /var/mike -m mike

Note: You cannot use usermod to change the name of the currently logged in user. Furthermore, when changing the user’s ID (UID), make sure the user is not currently executing any processes.

Set User’s Account Expiry Date

User accounts do not expire in Linux by default. Use the chage -l command to check account aging information for a user:

sudo chage -l [username]
Checking user account security information using the chage -l command

As the example above shows, the account is set not to expire. Change this by using usermod with the -e option. It allows you to specify the year, month, and day of the account expiry:

sudo usermod -e [YYYY-MM-DD] [username]

Confirm the successful change by checking the account with the chage -l command again:

Setting up a user account expiry date using the usermod -e command

Set User’s Shell

Use usermod -s to change the default shell for a single user:

sudo usermod -s [shell] [username]

If the change was successful, getent shows the new default shell at the end of the entry.

Changing the default shell for a single user using usermod -s

Change User’s UID

A UID (user identifier) is the unique number assigned to the user upon account creation with the useradd command. The /etc/login.defs file defines the range of UID values. Change a user’s UID with the usermod -u command:

sudo usermod -u [new-UID] [username]

In the passwd entry, find UID right after the username and password:

Changing user's UID using the usermod -u command

Change User’s Login Name

Change a username by adding the -l option. The syntax is as follows:

sudo usermod -l [newname] [oldname]
Changing a username using the usermod -l option

As the example above shows, using the old username to check the /etc/passwd file no longer returns data. However, the same data is now available under the new name.

Note: NAME_REGEX value in the /etc/adduser.conf file defines the standards for new username creation. However, usermod does not enforce the same standards for modifying usernames. While any string of characters is accepted as a new username with usermod -l, it is still advisable to adhere to standard naming conventions.

Lock and Unlock a User

Lock a user account by using the -L option.

sudo usermod -L [username]

Once the account has been locked with -L, the user’s login attempts fail upon typing the password:

Failing to authenticate an account after it has been locked with usermod -L

To unlock the account you previously locked, type usermod -U followed by the account name:

sudo usermod -U [username]
Unlocking an account with usermod -U

As you see in the example above, the password now works again, and the zsh shell initiates.

Set Password for a User

The most common way to set a password for a user is by using the passwd command. usermod also has the dedicated -p option for creating a password:

sudo usermod -p [password] [username]

However, this method of creating a password is not recommended since the password is visible in the /etc/shadow file. If you use the grep command to look for a username in the /etc/shadow file, the entry contains the plain-text version of the password created with usermod:

Creating a user login password with the -p option of the usermod command

Change User’s Primary Group

Change the primary group of a user with usermod -g:

sudo usermod -g [group] [username]

The id command confirms the successful change of the primary group:

Changing user's primary group with usermod -g

Add a User to a Supplementary Group

Aside from their primary group, users can be members of any number of supplementary groups. The -G option adds the user to a supplementary group:

sudo usermod -G [group] [username]
Adding a user to a supplementary group using the -G option

However, if the user already belongs to some supplementary groups, usermod -G removes them from those groups and adds only to the ones specified after the command.

In the example below, the user is already a member of a supplementary group. The combination of the -a and -G options adds the user to the specified group and leaves them in the supplementary group they were already a member of.

sudo usermod -a -G [group] [username]
Adding a user to a supplementary group using the usermod -a -G command

Change User’s Account Using Multiple Options

Use multiple options in one command for a more convenient way to edit a user. The example below shows a usermod command that:

  • changes the home folder and the shell,
  • sets an account expiry date,
  • adds a comment,
  • changes the UID, and
  • adds the user to a supplementary group.
sudo usermod -d [home-folder] -s [shell] -e [YYYY-MM-DD] -c "[comment]" -u [UID] -aG [group] [username]
Editing a user account using multiple options of the usermod command

Display All usermod Commands and Arguments

To read usermod help, use the --help argument:

usermod --help

For a more detailed list of options and functions, use the man command:

man usermod

Conclusion

This guide explained the use of the usermod command, along with its numerous options. After reading it, you should know how to use the command to modify user account settings and login information.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How To Use The Passwd Command In Linux
January 26, 2021

Passwords are the most important feature of security. This article explains and shows examples of how to use...
Read more
How to Search to Find a Word in Vim or Vi Text Editor
July 31, 2020

Searching in Vim or Vi allows you to easily navigate through large files. This guide teaches you how to...
Read more
Vim Commands Cheat Sheet
July 22, 2020

Vim is an open-source text editor found in most Linux distributions. This article lists all the commonly used...
Read more
How to Add User to a Group in Linux
March 4, 2021

By following the steps in this tutorial you will learn how to add a user to a group in Linux. Read the easy...
Read more