Linux : Notes on usermod command with 10 examples

  • Post author:
  • Post category:IT / RedHat

usermod is a command-line tool for modifying a user’s login details.It is used to change the information of an existing user account, such as the username, user ID, home directory location, user groups, password, default login shell, and so on.
The usermod command modifies the user information stored in the following configuration files.

• /etc/passwd - user information
• /etc/shadow - user security information
• /etc/group - user group information
• /etc/gshadow - information related to group security
• /etc/login.defs - default user configuration information.

Here is the basic syntax for the usermod command:

$usermod [options] [username]

Usermod command examples
Now that you understand the usermod command’s basic syntax and what it does, let’s look at some usermod practical examples.

Add Information about user

To add descriptive information about the user such name, room number, work phone, home phone, email address, and so on use the usermod -c option:

$ sudo usermod -c "Peter system admin" peter

Set User’s Account Expiry Date

In Linux, user accounts never expire by default. To find out how old a user’s accounts are, use the “chage -l command”:

$ sudo chage -l peter
Last password change                                    : never
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : never
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

The account in the preceding example is set to never expire. This can be changed by running usermod with the -e option. It allows you to define the account’s expiry year, month, and day. Use this format YYYY-MM-DD when setting the expiry time:

$sudo usermod-e 2024-10-14 peter

Change User Primary Group

To modify a user’s primary group, use the usermod command with the -g option, preceded by the name of the group and the username. Each system user can be assigned to precisely one primary group and zero or more secondary groups.

Here’s an example of changing the user Tom’s main group to managers:

$sudo usermod -g managers peter

Add a User to a secondary Group

To add a user to a supplementary(secondary) group, use the -a -G options followed by the name of the group and the username.

Here is an example of adding the user tom to a secondary group sudo:

$ usermod -a -G sudo tom

If you wish to add the user to multiple groups at the same time, provide the groups following the -G option, separated by, (commas), with no whitespace in between.When adding a user to a new group, always use the -a (append) option. If the -a option is not used, the user will be removed from all groups that are not included after the -G option.

Changing a User Home Directory

When you create a user in Linux, the system creates a home directory for the user in /home/ and name it after the user by default. To change the location of the user’s home directory Use the -d option :

$ sudo usermod -d /home/others/peter peter

The command does not, by default, migrate the contents of the user’s home directory to the new one. Use the -m option to move the contents. If the new directory does not exist already, the usermod command will create it:

$ sudo usermod -d /home/other/peter -m peter

Changing a User Default Login Shell

The default shell is the one that is invoked when you log in to the system. The default shell on most Linux systems these days is ZSH Shell rather than Bash Shell.To change the user’s default shell, issue the usermod command with the -s option, followed by the absolute path of the shell and the user’s name. Most shells are located in the /usr/bin/directory.

$udo usermod  -s /usr/bin/bash peter

Changing the User Name

Though it is uncommon, you may want to change the name of an existing user on occasion. To change the username, use the -l option:

$sudo usermod -l peter iampeter

Changing the User ID

A UID (User Identifier) ​​is a number assigned to each user by the system. This is used by the operating system to track and manage each user’s activities.
To change the user UID, run the usermod command with the -u option followed by her new UID and username:

$sudo usermod -u 2000 peter

Setting user password

usermod also has a dedicated -p option for creating passwords.
However, passwords are stored in clear text (unencrypted) in the /etc/shadow file, so creating passwords this way is not recommended. It is recommended to use the passwd command:

$sudo usermod -p secretpassword peter

Locking and Unlocking a User Account

To lock out a user account use the -L option. If the account is locked with -L, the user’s login attempt will fail when entering the password.

$sudo usermod -L peter

To unlock a locked account, specify the user name after the usermod -U option:

$sudo usermod -U peter

Note: Above notes are posted by @Linuxopsys: see below