How to use Usermod Command in Linux

Usermod Command in Linux

Usermod is a powerful command-line tool that allows you to make modifications to an existing user’s login information on your system. With usermod, you can add a user to a specific group, change their login name, home directory, shell, and much more.

In this comprehensive guide, we will explain how to use the usermod command effectively and efficiently. Whether you are a system administrator or a Linux enthusiast, this article will provide you with valuable insights on how to leverage the power of usermod to manage your system’s user accounts. By the end of this article, you will have a clear understanding of how usermod works, its various options and features, and how you can use it to streamline user management on your Linux system. So, let’s dive in!

The usermod Command

To use the usermod command, the syntax is as follows:

usermod [options] USER

Only those users with sudo access can use usermod and modify a user account. On success, the command won’t display any output.

Add a User to a Group

To add a user to a group, use the following command with the -a -G options, followed by the group name and username:

usermod -a -G GROUP USER

To add a user to multiple groups at once, specify the groups after the -G option separated with , (commas) with no intervening whitespace. For example:

sudo usermod -a -G games trioguide

It’s advisable to include the -a (append) flag while adding a user to a fresh group. If you leave out the -a flag, the user might get removed from any groups not mentioned after the -G flag.

The command will issue a warning if either the user or group is non-existent.

Change User Primary Group

To change a user’s primary group, use the following command with the -g option, followed by the group name and username:

sudo usermod -g GROUP USER

In this instance, we are modifying the primary group of the user “trioguide” to “developers” by using the command:

usermod -g developers trioguide

It’s worth noting that a user can be a part of only one primary group, but they can be a member of several secondary groups.

Changing the User Information

To update the GECOS (user’s full name) details, utilize the following command with the -c flag, followed by the new username and comment:

usermod -c "GECOS Comment" USER

For instance, if you want to add more information for the trioguide user, execute the command as follows:

usermod -c "Test User" trioguide

The /etc/passwd file is where this information is saved.

Changing a User Home Directory

Most Linux systems create user home directories under the /home directory using the user’s name as the directory name. To change a user’s home directory, use the usermod command with the -d option followed by the absolute path of the new home directory and the user’s name as follows:

usermod -d HOME_DIR USER

The command does not move the content of the original home directory to the new one by default. To move the content, use the -m option. If the new directory doesn’t exist, it will be created:

usermod -d HOME_DIR -m USER

For instance, to change the home directory of the user www-data to /var/www, you can run the command:

usermod -d /var/www www-data

Changing a User Default Shell

Upon logging in to the system, the default shell is executed. Typically, the Bash Shell is designated as the default shell on most Linux systems. If you wish to alter the default shell of a particular user, you may utilize the command with the -s option, providing the absolute path of the shell and the user’s name as arguments:

usermod -s SHELL USER

For instance, if you want to change a user’s shell to Zsh, you may use the following example command:

sudo usermod -s /usr/bin/zsh trioguide

To determine which shells are available on your system, you can view the contents of the /etc/shells file.

Changing a User UID

The UID (user identifier) is a unique number assigned to each user, which the operating system uses to identify them.

To modify the UID of a user, use the usermod command followed by the -u option and the new UID, as well as the name of the user:

usermod -u UID USER

For instance, the command below demonstrates how to change the UID from “UID” to “1050”:

sudo usermod -u 1050 trioguide

The files owned by the user in their home directory, as well as their mailbox file, will be automatically updated with the new UID. However, ownership of all other files must be manually changed.

Changing a User Name

Occasionally, there may be a need to modify the name of an already existing user. To do so, you can use the -l option with the usermod command:

usermod -l NEW_USER USER

For instance, the following command will change the username from trioguide to alex for user “1050”:

sudo usermod -l trioguide alex

If you change the username, you may also want to update the user’s home directory to match the new name.

Setting a User Expiry Date

The date when a user’s account will be disabled is known as the expiry date. To specify the expiry date for a user, use the -e option as follows:

sudo usermod -e DATE USER

Note that the expiry date must be in the format of YYYY-MM-DD.

For instance, to disable the trioguide user on 2023-02-22, the following command would be executed:

sudo usermod -e "2023-02-22" trioguide

If you want to remove the expiry date for a user account, set an empty expiry date using the following command:

sudo chage -l trioguide

To check the expiry date of a user, use the chage -l command:

sudo chage -l trioguide
Output:

Last password change					: Jan 20, 2020
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 output will provide information about the user’s password and account expiry.

The date of expiration is saved in the /etc/shadow file.

Locking and Unlocking a User Account

The option -L enables you to lock a user account in the following way:

usermod -L USER

When this command is executed, an exclamation mark (!) is added at the beginning of the encrypted password in the /etc/shadow file. This mark will prevent the user from logging into the system through password authentication. However, the user will still be able to log in using other methods, such as key-based authentication or switching to the user. If you wish to disable all login methods and lock the account, you must set the expiration date to 1.

For example, to lock the user “trioguide,” you can use the following commands:

sudo usermod -L trioguide

sudo usermod -L -e 1 trioguide

To unlock a user account, you can use the usermod command with the -U option:

usermod -U USER

Conclusion

In this article, we have provided a comprehensive guide on using the usermod command to set user account information in a Linux environment. By following the steps outlined above, you can easily modify user account details such as their username, home directory, default shell, and more.

By mastering the usermod command, you can effectively manage user accounts and improve the security and functionality of your system. If you have any questions or comments regarding the usermod command, feel free to leave them below.

For more helpful tips and tutorials on Linux administration and system management, stay tuned to our blog. Don’t forget to share this article with your colleagues and friends who might find it useful.

Alex

Alex

Hey there! My name is Alex and I'm a professional content writer. I'm also lucky enough to be part of an amazing trio team! I absolutely love what I do and I'm passionate about creating content that engages, informs, and entertains. Whether it's writing blog posts, website copy, or social media content, I always strive to deliver high-quality work that exceeds expectations.

We will be happy to hear your thoughts

Leave a reply

TrioGuide
Logo