A Complete Guide to Usage of 'usermod' command


As a system administrator or an advanced user, you might come across scenarios where you need to modify user accounts on your Linux system. One such command that can help you with this task is 'usermod' command. In this article, we'll provide you with a complete guide to using 'usermod' command.

What is Usermod Command?

The 'usermod' command is a Linux utility that enables system administrators or advanced users to modify user account information. This command can be used to change user account properties such as username, user ID (UID), group ID (GID), home directory, login shell, and more.

The 'usermod' command works by modifying '/etc/passwd' and '/etc/shadow' files that store user account information on your Linux system.

Basic Syntax of Usermod Command

The basic syntax of 'usermod' command is as follows −

usermod [options] username

Here, 'username' is name of user account you want to modify. 'options' parameter represents various options that you can use with 'usermod' command to change user account properties.

Modifying User Account Properties with Usermod Command

The 'usermod' command offers a range of options that you can use to modify user account properties. Here are some of most commonly used options −

Changing Username

To change username of a user account, you can use '-l' or '--login' option. Here's an example −

sudo usermod -l newusername oldusername

This command will change username of 'oldusername' account to 'newusername.'

Changing UID

To change UID of a user account, you can use '-u' or '--uid' option. Here's an example −

sudo usermod -u 1001 username

This command will change UID of 'username' account to '1001.'

Changing GID

To change primary group ID (GID) of a user account, you can use '-g' or '--gid' option. Here's an example −

sudo usermod -g newgroupname username

This command will change primary GID of 'username' account to 'newgroupname.'

Changing Home Directory

To change home directory of a user account, you can use '-d' or '--home' option. Here's an example −

sudo usermod -d /new/home/directory username

This command will change home directory of 'username' account to '/new/home/directory.'

Changing Login Shell

To change login shell of a user account, you can use '-s' or '--shell' option. Here's an example −

sudo usermod -s /bin/bash username

This command will change login shell of 'username' account to '/bin/bash.'

Locking or Unlocking User Account

To lock a user account and prevent it from being used for login, you can use '-L' or '--lock' option. Here's an example −

sudo usermod -L username

This command will lock 'username' account and prevent it from being used for login.

To unlock a user account and enable it for login again, you can use '-U' or '--unlock' option. Here's an example −

sudo usermod -U username

This command will unlock 'username' account and enable it for login again.

Here are some additional tips and best practices to keep in mind while using 'usermod' command −

1. Always Use Sudo

To run 'usermod' command, you need to have root or sudo privileges. Running command without necessary permissions can result in errors or unexpected behavior. Make sure to prefix 'usermod' command with 'sudo' or run it as root user.

2. Verify Changes

After making changes to a user account using 'usermod' command, it is always a good practice to verify changes. You can do this by running 'id' command to display UID and GID of modified account, or 'cat /etc/passwd' command to view updated user account information.

3. Use '-a' Option

The '-a' or '--append' option can be used with 'usermod' command to add supplementary groups to a user account without removing existing ones. This is useful when you want to add a user to an additional group without affecting their primary group membership.

4. Changing Password

The 'usermod' command does not allow you to change password of a user account directly. To change password, you can use 'passwd' command. Here's an example −

sudo passwd username

This command will prompt you to enter a new password for 'username' account.

5. Back Up User Account Information

Before making any changes to a user account using 'usermod' command, it is always recommended to back up user account information. This can be done by making a copy of '/etc/passwd' and '/etc/shadow' files or by using a backup tool.

The 'usermod' command is a powerful tool that allows you to modify user account properties on your Linux system. By using various options provided by this command, you can easily change username, UID, GID, home directory, login shell, and more.

It is important to note that while using 'usermod' command, you should exercise caution and make sure that you understand implications of changes you are making. Incorrectly modifying user account properties can cause unexpected errors and issues on your Linux system.

We hope that this complete guide to using 'usermod' command has been helpful in understanding how to modify user account properties on your Linux system. With knowledge gained from this article, you can now confidently use 'usermod' command to manage user accounts on your Linux system.

Conclusion

In conclusion, 'usermod' command is a powerful tool that can be used to modify user account properties on your Linux system. By using various options provided by this command, you can easily manage user accounts and ensure that they meet your requirements. However, it is important to use command with caution and always verify changes made. With these best practices in mind, you can effectively use 'usermod' command to manage user accounts on your Linux system.

Updated on: 20-Apr-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements