usermod command in Linux

usermod command in Linux

Learn the basics of the usermod command to manage users in a linux system

The usermod command comes in very handy when you need to change any of the default user information for a user on your Linux system. For example, the command can change the user's Password, login shell, home directory, and more.
This article shows you how to use the usermod command to modify a user's information.

Before we start, you should know that the usermod command only works with a root user. If you are not logged in as root, you will need to use the sudo command to gain root privileges.

Let's get started now that we have that out of the way.

Usermod Syntax

The usermod syntax is as follows:

usermod [options] username

Where "username" is the user's name you want to modify and "options" specify what information you want to change.

Usermod options

Change username with usermod

To change the username with usermod, you would use the following command:

usermod -l newname oldname

This would change the username from oldname to newname

Example:

sudo usermod -l Jay James

Change Password with usermod

To change a user's Password with usermod, you will need to use the -p option followed by the new Password. For example, to change the Password for the user john to "123456", you would run the following command:

sudo usermod -p 123456 john

Change home directory with usermod

The -d option allows you to change the location of a user's home directory. For example, to change the home directory for the user john from "/home/john" to "/home/newdir," you would use the following command:

sudo usermod -d /home/newdir john

Be sure to move any files in the old home directory to the new one before changing them. Otherwise, they will be lost.

You can add the -m flag to move the content like this.

sudo usermod -d /home/newdir -m john

Add Information to a User

The -c option adds comments to a user's entry. It helps add information about the user, such as their full name or job title.

For example, to add the comment "System Administrator" to the user john's entry, you would use the following command:

sudo usermod -c "System Administrator" john

You can view a user's entry by using the grep command.

grep john /etc/passwd

This will return all of the information for that user in the /etc/passwd file.

Set User’s Account Expiry Date

You can use the -e option to set an expiry date for a user's account. For example, to set the expiry date for the user john's account to December 01, 2022, you would use the following command:

Syntax:

sudo usermod -e [YYYY-MM-DD] username
sudo usermod -e 2022-12-01 john

If you want to remove a user's expiry date, you can use the -e option with an empty date. For example, to remove the expiry date for the user john's account, you would use the following command:

sudo usermod -e '' -john

You can check the account aging with the following command:

sudo chage -l john

Change default shell with usermod

The -s option allows you to change the user's default shell. For example, to change the default shell for the user john from "/bin/bash" to "/bin/sh", you would use the following command:

sudo usermod -s /bin/sh john

Get a list of all available shells on your system by running the following command:

cat /etc/shells

This will return a list of all the shells available on your system.

Lock or unlock a user account with usermod

The -L option will lock a user's account so they cannot log in. To lock the user john's account, you would use the following command:

sudo usermod -L john

You would use the -U option to unlock a user's account. For example, to unlock the user john's account, you would use the following command:

sudo usermod -U john

As you see, there are quite a few options that you can use with the usermod command. In most cases, you will only use a few of them. You can learn more about the usermod in the man pages:

man usermod

Conclusion

This article has shown you how to use the usermod command to modify a user's information on your Linux system. We also showed you some examples of how to use this command. Remember that usermod can only be used by the root user. If you are not logged in as root, you will need to use sudo.

I hope this has been helpful.

Thank you for reading!

💡
Get GeekBits premium free for 3 months. Badges (geekbits.io)
Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.