use the usermod command 1

How to Use usermod Command on Linux

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.

Basic Syntax

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)
  • -U: The -U flag is used for unlocking purposes. 
  • -m: Employ the -m flag 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

Prerequisite 

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.

Syntax:

use usermod command

Example:

use usermod command

You can test the output by asking for the ID of both usernames, like this:

use the usermod command and testing usermod command

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:

checking the group

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]
id command

Here the primary group is realuser, which can be changed using usermod using the following command.

Basic Syntax:

$ sudo usermod -g [groupname] [username]

Example input:

checking the id command

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.

Basic syntax:

$ sudo usermod -G [new Group] [username]

Example:

adding a new group

Output :

use the usermod command

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. 

Using the -a flag before the -G option will add the group as a secondary group. It happens without even changing the existing primary group.

Syntax:

$ 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.

Syntax:

$ sudo usermod -u [new_ID] user

Example:

Here we are going to change the user’s UID to some desired value, let’s say 536.

Input:

changing the UID

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.

Syntax:

$ sudo usermod -L [user_name]

Example:

We will lock our realuser and see what happens. 

luse the usermod command

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.

Syntax:

$ 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. 

Syntax:

$ 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]

Example:

We will set the expiry date for our realuser, using the following command:

setting the expiry date

Verify:

verifying the expiry date

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 -s flag.

Syntax:

$ sudo usermod -s /bin/sh [user]

Example:

changing user shell

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:

Syntax:

$ sudo usermod -d [new_directory] [username]

To verify the changes, invoke the user grep command. 

Output:

changing the directory

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.

Leave a Reply
Related Posts