15 usermod command examples in Linux [Cheat Sheet]

Introduction to usermod command

usermod is a command-line tool in Linux to modify the details of an existing user in the system. You can create a new user in the Linux system by using useradd command. After adding a new user, you may need to change some details of the user. usermod command can change user name, user ID, groups, home directory, password, user shell, expiry date, and other user details.

 

Different examples to use usermod command

Only root user or sudo users can execute the usermod command. This command changes the details of a user in files such as /etc/passwd/etc/shadow/etc/group and /etc/gshadow.

Advertisement
  • /etc/passwd stores the information of a user.
  • /etc/shadow contains the encrypted password of each user.
  • /etc/group stores the group information.
  • /etc/gshadow contains the encrypted password of each group.

The basic syntax would be:

$ sudo usermod [option] user_name

In this article, we will go through the most used examples of usermod command in Linux.

 

1. usermod command to change the name of a user

-l or --login option allows you to change or rename the name of any existing user.

$ sudo usermod -l new_name old_name

Sample Output:

usermod command to change the user name

Here, we have changed the name of user brown to green. When we searched for the user brown, it is not found.

 

2. Change the user ID of a user with usermod command

While adding a new user, the system automatically provides the next available user ID. You can change the user ID with the help of -u or --uid option.

Advertisement
$ sudo usermod -u new_uid username

Sample Output:

usermod command to change user id of user

 

3. Set duplicate user ID using usermod command

Generally, every user has a unique user ID. But -o option allows you to set duplicate user ID for a user.

$ sudo usermod -o -u user_ID user_name

Sample Output:

usermod command to set duplicate user id of user

In the above output, the user ID of elliot is changed to the user ID of eliza. Now, they have same user ID 1235.

 

4. usermod command to change the primary group of a user

Sometimes, you may want to assign a different group to a user. You can change the group of a user using -g or --gid option.

$ sudo usermod -g new_group user_name

Sample Output:

usermod command to change the primry group of a user

You can also change the group using the group ID instead of the name.

usermod command to change group using group id

 

5. Add a user to secondary groups using usermod command

Each user can have only one primary group. But we can assign them to zero or multiple secondary groups. -G option allows you to specify the secondary group to a user.

Advertisement
$ sudo usermod -G secondary_group user_name

Sample Output:

usermod command to assign secondary group to a user

You can assign multiple secondary groups using the same command but you have to separate group names by a comma.

usermod command to assign multiple secondary groups to user

You might have noticed the user deepak has been removed from the group computer. It is because usermod command removes the user from other groups and adds to the listed groups only.

 

6. Append a user to secondary groups with usermod command

You can use -a option and add a user to the single or multiple secondary groups without removing the current groups.

$ sudo usermod -a -G secondary_group user_name

Sample Output:

usermod command to appendsecondary groups to a user

 

7. usermod command to modify the comment details of a user

The comment is a short information that can be added to a user account. -c option sets or changes the comment of a user.

$ sudo usermod -c "comment" user_name

Sample Output:

usermod command to modify the comment of a user

 

8. Change the home directory of a user with usermod command

The default home directory of a user is /home/username. You can simply change it by using -d or --home option.

$ sudo usermod -d directory_path user_name

Sample Output:

usermod command to change home directory of a user

IMPORTANT NOTE:
Changing home directory of an existing user using -d option will not migrate the data or create the new path. You must manually create the new home directory and arrange the permission so that your user is able to login into the new HOME folder.

The old home directory will not be deleted so you must manually restore any files from the old PATH to the new PATH

 

9. Move the content of user's home directory using usermod command

-m option allows you to move the content of the user's home directory to the new location. This option only works if it is used with -d or --home option.

$ sudo usermod -d new_dir_path -m user_name

This command will also create the new home directory (if not present already) and copy all the file contents from users existing home directory to new path.

 

10. usermod command to modify the login shell for a user

-s option allows you to specify or modify the shell for a user. The default user shell is /bin/bash in most of the Linux distributions.

$ sudo usermod -s new_shell_path user_name

Sample Output:

usermod command to change shell of a user

 

11. Modify the expiry date of a user account using usermod command

When adding a new user normally, the expiry date is set to never. You can change the expiry date using -e option with usermod command.

The date must be provided in YYYY-MM-DD format.

$ sudo usermod -e YYYY-MM-DD user_name

Sample Output:

usermod command to modify expiry date of a user

 

12. usermod command to set an unencrypted password for a user

-p option allows you to set an unencrypted password for a user. You will require a root permission to view password in /etc/shadow file.

$ sudo usermod -p password user_name

Sample output:

usermod command to modify an unencrypted password for user

 

13. Lock a user account with usermod command

You can lock  a user account using -L option. After locking an account, the exclamatory mark(!) will appear before the password. It indicates the password is disabled and you cannot login that user with password authentication.

$ sudo usermod -L user_name

Sample Output:

usermod command to lock a user account

 

14. usermod command to unlock an user account

After locking a user account, you may want to know how to unlock a account. To unlock a user account, you can use -U option.

$ sudo usermod -U user_name

Sample Output:

usermod command to unlock a user account

As you can see, the exclamatory mark(!) has disappeared now.

 

15. usermod command to set password to inactive after expiration

You can specify the number of days after a password expires until the account is permanently disabled using -f or --inactive option. A value of 0 disables the account as soon as the password has expired, and a value of -1 disables the feature.

$ sudo usermod -f NUM user_name

 

Conclusion

These are the examples of most used usermod commands in Linux. Now, you have an idea of using the usermod command and modifying an existing user’s details in the Linux system. It is a simple but very useful command. It helps system administrators to manage the user accounts in the system easily.

 

What's Next

15 most used useradd command examples in Linux

 

Further Reading

man page for usermod command

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment