Skip to content

Linux usermod command

The Linux usermod command is used to modify user account details on a Linux system. It allows system administrators to change various attributes of a user account, such as username, user ID, primary group, home directory, and login shell. By using the usermod command, administrators can easily update user account information without having to delete and recreate the user account. This command provides a convenient way to manage user accounts and make necessary adjustments as needed.

usermod Syntax:

Terminal window
usermod [options] username

Options:

OptionDescription
-cAdd a comment for the user
-dChange the user’s home directory
-eSet account expiry date for the user
-gChange the user’s initial login group
-GAdd the user to supplementary group(s)
-lChange the user’s login name
-pSet the user’s encrypted password
-sChange the user’s login shell
-uChange the user’s ID

Parameters:

ParameterDescription
usernameThe username of the user to modify

usermod Usage:

Add User to Secondary Group

Terminal window
usermod -aG groupname username

This command adds the user “username” to the secondary group “groupname”.

Change User’s Home Directory

Terminal window
usermod -d /path/to/new/home username

Changes the user “username” home directory to the specified path.

Lock User Account

Terminal window
usermod -L username

Locks the user account for “username”, preventing login.

Set User Account Expiry Date

Terminal window
usermod -e 2023-12-31 username

Sets the expiry date for the user account “username” to December 31, 2023.

Common Questions on usermod Usage:

1. How do I use usermod to change a user’s home directory in Linux?

To change a user’s home directory in Linux using usermod, execute the following command:

Terminal window
usermod -d /path/to/new/home -m username

2. How can I add a user to additional groups with usermod in Linux?

To add a user to additional groups using usermod in Linux, run the following command:

Terminal window
usermod -aG group1,group2 username

3. How do I set the expiry date for a user account with usermod in Linux?

To set the expiry date for a user account in Linux using usermod, use the following command:

Terminal window
usermod -e YYYY-MM-DD username

4. How can I disable a user account with usermod in Linux?

To disable a user account in Linux using usermod, execute the following command:

Terminal window
usermod -L username

5. How do I unlock a locked user account with usermod in Linux?

To unlock a locked user account in Linux using usermod, run the following command:

Terminal window
usermod -U username

6. Can I change a user’s login name with usermod in Linux?

Yes, you can change a user’s login name using usermod in Linux by running the following command:

Terminal window
usermod -l newusername oldusername

7. How can I force a user to change their password at next login with usermod in Linux?

To force a user to change their password at the next login in Linux using usermod, use the following command:

Terminal window
usermod -e 1970-01-01 -f 30 username

Applications of the usermod command

  • Modify user attributes
  • Change user’s home directory
  • Change user’s login name
  • Add or remove user from a group
  • Set expiration date for a user account
  • Lock or unlock a user account