You can create a new user with useradd command but if you mis-configured the account, you don’t necessarily have to delete the user. You can change the user account settings with usermod command in Linux.
The usermod (short for user modification) enables you to modify various aspects of a user account. You can change the home directory and shell of the user. You can add the user to different groups or lock the account.
8 Practical examples of the usermod command
Since you are dealing with user account management, you must be either root or have sudo rights to run the usermod command.
sudo usermod [options] username
Let me show you some examples.
1. Change user name
You can change the username with the option -l of usermod command:
sudo usermod -l new_username old_username
Keep in mind that apart from the user name, nothing else changes on its own. You’ll have to manually change the home directory and mail spool.
2. Change the home directory of user
You can change the home directory of the user to another directory.
sudo usermod -d new_home_dir user_name
Keep in mind that it will change the home directory even if the directory doesn’t exist but it will not create it on its own.
A better way to change the home directory of the user is to use -m option with -d. This way, it will create the new home directory if it doesn’t exist. Apart from that, it will also move the content of the old home directory to the new home directory with correct file permissions and ownership.
sudo usermod -md new_home_dir user_name
Here’s an example:
abhishek@linuxhandbook:~$ sudo usermod -md /home/mynewhome myuser
abhishek@linuxhandbook:~$ ls -l /home/mynewhome/
-rw-r--r-- 1 myuser myuser 8980 Apr 16 2018 examples.desktop
-rw-rw-r-- 1 myuser myuser 0 Jan 17 11:15 morefile.txt
-rw-rw-r-- 1 myuser myuser 0 Jan 17 11:15 myfile.txt
Order of m and d is also important here.
3. Change the login shell of the user
sudo usermod -s /bin/zsh username
4. Change the default user group
You can also change the default group of a user. This is particularly helpful when you have changed the user name. It would be good idea to change the default user group as well.
sudo usermod -g new_default_group_name username
5. Add the user to other groups
This is perhaps the most common usage of the usermod command. If you want to add a user to the sudoer list, all you have to do is to add the user to the sudo group.
sudo usermod -aG group_name username
Did you notice that I used option -aG here and not just -G. It’s because if you use only -G option, it will replace the user groups with the new group you provided.
This is why you should use the append option -a so that the user is added to the new group additionally and not removed from its previous groups.
6. Lock and unlock user account
You can lock a user account in Linux with usermod command option -L. A locked user cannot log in to the system.
sudo usermod -L username
You can also unlock the user with option -U:
sudo usermod -U username
7. Set an expiry date to the user account
Suppose an intern joins your organization for two months. You’ll have to remove the user account once the intern departs. You can play smart here and set an expiry date to the intern’s account so that it automatically disables the account.
To set an expiry date to an user account, you can use the option -e with date in YYYY-MM-DD format.
sudo usermod -e 2020-04-01 username
8. Change the UID of a user
You can change the UID (user ID) of a user with the option -u:
sudo usermod -u UID username
In the end…
There are a few more options that you may explore by referring to its man page.
In my opinion, what you just saw is the most common examples of usermod command. If you have any questions or suggestions, do let me know.