Lesson 56: usermod Command
In this lesson, you'll learn how to use the usermod command to modify the attributes of an existing user account in Linux.
The usermod command is used to modify or change any attributes of an already created user account via the command line.
The command usermod is similar to that of useradd or adduser but the login is granted to an existing user.
After creating user accounts, in some scenarios where we need to change the attributes of an existing user such as changing the user's home directory, login name, login shell, password expiry date, etc., in such cases the usermod command is used.
When we execute the usermod command in the terminal, the following files are used and affected:
/etc/passwd- User account information./etc/shadow- Secure account information./etc/group- Group account information./etc/gshadow- Secure group account information./etc/login.defs- Shadow password suite configuration.
usermod Command Syntax
usermod [OPTIONS] USERNAME
Requirements
Before using the usermod command, keep the following in mind:
- We must have existing user accounts to execute
usermodcommands. - Only the superuser (root) is allowed to execute the
usermodcommand. - The
usermodcommand can be executed on any Linux distribution. - Must have basic knowledge of the
usermodcommand with options.
usermod Command Options
| Option | Description |
|---|---|
-c |
Add a comment field for the user account |
-d |
Modify the home directory for any existing user account |
-e |
Make the account expire on a specific date |
-g |
Change the primary group for a user |
-G |
Add a supplementary group |
-a |
Append the user to the supplementary group(s) without removing from existing groups |
-l |
Change the login name of the user |
-L |
Lock the user account (disables the password) |
-m |
Move the contents of the home directory to the new directory |
-p |
Set an un-encrypted password for the user (NOT secure) |
-s |
Specify a login shell for the user |
-u |
Assign a specific UID to the user account |
-U |
Unlock the user account (removes the password lock) |
Let's explore some usermod commands with their practical examples and usage.
1. Add Information to a User Account
The -c option is used to set a brief comment (information) about the user account. For example, let's add information on the tecmint user using the following command.
# usermod -c "This is Tecmint" tecmint
After adding information on the user, the same comment can be viewed in the /etc/passwd file.