You can use the usermod command on Linux and change the properties of a user through the command line utility. Read the article and learn how you can seamlessly modify sets of information like username, directory, and more.
Once you create a user on Linux or any other OS in general, you may sometimes need to bring alterations. This includes changing information about that user like password, login name, login directory, user ID, etc.
Managing the information mentioned above about the user account is a relatively easy task on Linux. This is made possible through the usermod command. In case you don’t already know, this command is supported in most of the Linux distros, if not all.
The syntax for the usermod command is generally basic in nature. This is what it looks like:
$ usermod [options] username
The usermod Command: Different Options
Remember, there are many options available. Before you learn how to use the usermod command on Linux, it is crucial to understand the following options.
-l: It helps change the name of the target user
-d: This flag is used to modify the home directory of the existing user
-L: With this, you can lock the user account (disabling the password)
-Uflag is used for unlocking purposes.
-m: Employ the
-mflag to move contents from the existing home directory to a new location
-u: It helps change the user id of the target user
-g: This helps change the group of the users
-G: it loads a list of supplementary groups (of which the user is also a member)
-s: This helps create a shell for a new account
-e: With this, you can change the expiry date of a user
All you need is a Linux OS with two or more users where we can test things. Also, you must have
sudo access to use the commands.
Adding new users to your PC might differ depending on the distro you use. However, it’s in the setting; you can easily search for them.
How to Use the usermod Command on Linux?
Now that you’ve got enough information, let’s dive in and learn how to use the usermod command on Linux.
Changing the Username
Sometimes, needing to change a user’s name is just your personal choice, to be honest. In setting, you can get it done through the Setting’s panel of the
GUI (Graphical User Interface). But we are not here for that. Instead, we will use CLI (Command Line Interface) and employ the usermod command.
You can test the output by asking for the ID of both usernames, like this:
Here you can see that there is no such user named test123.
Use the usermod Command and Change the Primary Group
On Linux, the collection of users is called a group. The primary purpose of a group is to define certain privileges like reading, writing, executing, etc. These are done concerning the shared resources revolving around the group’s users.
By default, a user’s primary group has the same name as that of the username. With the usermod command, we can easily change that and even add the user to another group. Here is how:
Step 1: Check the Existing Group
First, check for existing groups. Launch the Terminal using the “Ctrl+Alt+T” key combination followed by passing the following command:
Step 2: Use the id Command
To change the primary group of a user, you will simply need to employ the
id command and set the group’s name to what is desired.
$ id [username]
Here the primary group is
realuser, which can be changed using usermod using the following command.
$ sudo usermod -g [groupname] [username]
Now you can check the group status using the
id [username] command.
Adding New Group to An Existing User
The user account can belong to more than one group in the Linux system. Of course, it was only one primary group, but many secondary groups can be assigned to the same users.
$ sudo usermod -G [new Group] [username]
And with that, we’ve successfully added realuser to the dip group.
You can add a new group as a secondary group using the
-a option. Here the
-aoption stands for append.
-a flag before the
-G option will add the group as a secondary group. It happens without even changing the existing primary group.
$ sudo usermod -a -G [group] [user]
Changing the UID of a User
Uid is the unique numerical value assigned to every user by Linux. The system identifies the user with its unique UID. Usually, UID zero is given to the root user.
$ sudo usermod -u [new_ID] user
Here we are going to change the user’s UID to some desired value, let’s say 536.
You can verify once done using the
id [user] command, just like earlier.
Locking the Users
Suppose you can’t lock or restrict access to the system for a particular user. In that case, you can lock the password of that particular user. If done so, when a user tries to log in with the password, he will not be granted access to the system.
$ sudo usermod -L [user_name]
We will lock our realuser and see what happens.
Here you can see there is “!” symbol, which indicates it has been locked.
Unlocking the Users
Once a user is locked, you may need it to be unlocked soon. Thankfully, the process is pretty straightforward. Just use the
-U option in the command.
$ sudo usemod -U [username]
Set User Expiry Date
With the usermod command, you can pick the usage period for a particular user. Using the usermod command, you can seamlessly set expiry times for certain users.
$ sudo usermod -e [YYYY-MM-DD] [user-name]
To check the expiry date of the user account, use the following command (Syntax)
$ chage -l [username]
We will set the expiry date for our realuser, using the following command:
Changing the User Shell
The Linux shell is a unique interactive utility. It allows users to start programs, manage the process, permissions, read/write files, etc. The shell contains internal commands you use to control things that the GUI generally administers.
You can change the shell user using the usermod command and the
$ sudo usermod -s /bin/sh [user]
Changing The Home Directory
When you log in to your system, your regular session starts in your home directory, which is unique to your user account. The system assigns this unique directory when the user account is created. Most of the time, the home directory is the default.
You can change this to anything you want using the following syntax:
$ sudo usermod -d [new_directory] [username]
To verify the changes, invoke the user grep command.
So, in this tutorial, we looked at what the usermod command is all about. We’ve also learned how to use the usermod command on Linux and perform different tasks as per our needs.
If this guide helped you, please share it.