The Ultimate Guide to usermod Command in Linux: 15 Key Options You Must Know

The usermod command in Linux is a powerful utility for system administrators and users alike. If you’re looking to modify user accounts in Linux, whether it’s changing a user’s home directory, adding them to groups, or altering other account attributes, usermod is your go-to tool.

In this comprehensive guide, we’ll dive deep into the usermod command and explore all of its options, their syntax, and practical examples. This detailed post will cover everything from basic usage to advanced options, giving you all the information you need to manage user accounts like a pro.

Let’s explore how the usermod command works and discover how you can apply it to your system to improve user management.

usermod command in Linux


What is the usermod Command in Linux?

The usermod command in Linux is used to modify a user account. Whether you need to change the user’s login name, update their home directory, or modify password expiration, usermod allows you to make these changes from the command line interface (CLI).

Syntax of the usermod Command:

usermod [options] LOGIN
  • LOGIN: The username of the account you want to modify.
  • options: The various flags and options that allow you to modify the user’s properties.

Key Options for the usermod Command

There are many options available with the usermod command that allow you to make specific changes to user accounts. Below, we’ll explore each of the most important options.

1. -a, –append

The -a option is used to add the user to supplementary group(s) without removing them from any existing groups. This option must be used with the -G option.

Example:

usermod -a -G sudo,developers john

This command adds the user john to both the sudo and developers groups without removing them from other groups.

2. -b, –badname

This option allows you to use names that don’t conform to the system naming standards.

Example:

usermod -b user@domain john

This command will allow the user john to have an email-style name, even if it doesn’t meet the typical Unix naming conventions.

3. -c, –comment

The -c option allows you to change the user’s comment field in the password file, which is often used for the user’s real name or description.

Example:

usermod -c "John Doe, Developer" john

This will change the comment field of the john user to “John Doe, Developer”.

4. -d, –home HOME_DIR

With this option, you can change the home directory of a user.

Example:

usermod -d /home/john_new john

This command changes john‘s home directory to /home/john_new.

If you want to move the contents of the old home directory to the new location, use the -m option (explained below).

5. -e, –expiredate EXPIRE_DATE

This option sets an expiration date for a user account in the format YYYY-MM-DD.

Example:

usermod -e 2025-12-31 john

This sets the expiration date for john‘s account to December 31, 2025.

6. -f, –inactive INACTIVE

The -f option sets the number of days after a password expiration until the account is disabled. A value of 0 disables the account immediately after the password expires.

Example:

usermod -f 7 john

This command disables the john user’s account 7 days after their password expires.

7. -g, –gid GROUP

This option allows you to change the user’s primary group.

Example:

usermod -g admin john

This changes john‘s primary group to admin.

8. -G, –groups GROUP1[,GROUP2,…]

This option allows you to specify a list of supplementary groups that the user will belong to. If the user is already a member of some groups that aren’t listed, they will be removed from those groups unless you use the -a (append) option.

Example:

usermod -G sudo,developers john

This adds john to the sudo and developers groups while removing them from other groups (unless -a is used).

9. -l, –login NEW_LOGIN

The -l option allows you to change the user’s login name.

Example:

usermod -l john_doe john

This changes the login name of john to john_doe.

10. -L, –lock

Locking a user’s password effectively disables login for the user without deleting the account.

Example:

usermod -L john

This locks john’s password and prevents them from logging in.

11. -m, –move-home

The -m option moves the contents of the user’s home directory to a new location when combined with the -d option.

Example:

usermod -d /home/john_new -m john

This moves the contents of john‘s old home directory to /home/john_new.

12. -o, –non-unique

This option allows you to assign a non-unique user ID when changing the user ID (-u option).

Example:

usermod -u 1001 -o john

This command sets john’s user ID to 1001, even if another user already has that ID.

13. -p, –password PASSWORD

This option changes the user’s password. The password should be encrypted using the crypt function.

Example:

usermod -p '$6$rounds=5000$saltsalt$encryptedpassword' john

This changes the password for john to the provided encrypted string.

14. -R, –root CHROOT_DIR

This option allows you to apply changes to a user account in a specified chroot directory.

Example:

usermod -R /chroot/dir john

This applies changes for the user john within the /chroot/dir directory.

15. -s, –shell SHELL

The -s option lets you change the user’s login shell.

Example:

usermod -s /bin/bash john

This changes john’s login shell to /bin/bash.


Advanced User Management with usermod

Now that we’ve covered the basic options, let’s take a look at some advanced features that the usermod command offers for managing user accounts.

Subordinate UIDs and GIDs

For users who require additional sets of UIDs or GIDs, the usermod command provides options to add or remove subordinate IDs. These options are useful when you need to allocate UIDs and GIDs from a specific range for services like containers or virtualized environments.

  • –add-subuids FIRST-LAST: Adds a range of subordinate UIDs to a user’s account.
  • –del-subuids FIRST-LAST: Removes a range of subordinate UIDs from a user’s account.
  • –add-subgids FIRST-LAST: Adds a range of subordinate GIDs to a user’s account.
  • –del-subgids FIRST-LAST: Removes a range of subordinate GIDs from a user’s account.

These options are typically used in conjunction with certain types of applications or advanced containerization setups.


Commonly Asked Questions About usermod

1. How do I change a user’s password using usermod?

You can change a user’s password using the -p option followed by the encrypted password. Example:

usermod -p 'encryptedpassword' john

2. Can I add a user to multiple groups?

Yes, use the -G option to specify multiple groups separated by commas:

usermod -G group1,group2 john

3. How do I lock a user’s account?

Use the -L option to lock a user’s account:

usermod -L john

4. What is the purpose of the -a option?

The -a option is used with -G to add a user to supplementary groups without removing them from other groups.

5. Can I change a user’s home directory with usermod?

Yes

Apologies for the oversight! Here is the conclusion section with internal and external links, as requested.


Conclusion

In this comprehensive guide, we’ve explored the powerful usermod command in Linux, highlighting its numerous options for modifying user accounts. Whether you’re adding users to groups, changing their login name, updating their home directory, or locking their account, the usermod command is an essential tool for any system administrator or advanced Linux user.

The flexibility and versatility of usermod make it one of the most commonly used commands for user management. Understanding how to use it effectively can streamline user account management and improve the overall security and organization of your system.

By mastering the options we’ve covered, you’ll be able to tailor user accounts to fit your system’s needs. From setting account expiration dates to changing the login shell, usermod has got you covered. And don’t forget to explore more advanced options like subordinate UIDs and GIDs for even greater control.

If you want to dive deeper into Linux administration, be sure to check out more of our detailed guides on GeekersHub, where we provide comprehensive content to help you master the Linux operating system and its powerful commands.

For additional resources on user management in Linux, you can also explore the official Linux man page for usermod, which provides even more in-depth information and usage examples.


Thank you for reading this guide! If you found it helpful, don’t forget to share it with others or drop a comment if you have any questions.