CentOS Debian Mint openSUSE Red Hat Ubuntu

Usermod Command with Examples

Usermod Command with Examples

The Linux usermod command is a command-line utility that allows you to modify a user’s attributes on a Linux system. Such attributes include the groups that a user belongs to, login directory, password, and so on. In this guide, we showcase some of the usermod commands that you can use to change a regular user’s properties.

Usermod Syntax

The usermod command takes the following syntax:

$ usermod options username

Adding a regular user to a Group

Probably the common use case of the usermod command is adding or placing a user in another group. By default, a primary group is assigned to the user upon creation of the user account. Additionally, you can add the user to another group. To achieve this , use the -a -G ir simply -aG in the syntax shown:

$ usermod -aG group user

In the example below, we are adding a regular user bob to the sudo group.

$ usermod -aG sudo group

Thereafter, invoke the groups command to verify the groups that the user bob belongs to. Initially, the user only belonged to the group bob. After adding the user to the sudo group, the user now belongs to 2 groups: bob and sudo groups.

$ groups bob

Change a user’s home directory

With usermod -d command, you can also change the default home directory as follows.

$ usermod -d /home/new_directory user

In the example below, we have changed the home directory of user bob to the /home/test directory.

$ sudo usermod -d /home/test bob

Change a user’s login name

Using the -l flag, you can change the user’s login name as follows:

$ sudo usermod -l robert bob 

In this example, the command changes the user’s login name from bob to robert.

Change User ID ( UID ) for a user

The -u flag allows you to change the User ID of the user. In the example below, we have set the UID of user robert from the default 1005 to 4321.

$ sudo usermod -u 4321 robert

How to lock/unlock a user

In addition, you can also lock a user account using the -L option. In the example below, we are locking the user the user robert.

$ sudo usermod -L robert

When you try logging in, you get an authentication error because the user is locked.

To unlock the user, use the -U option as shown.

$ sudo usermod -U robert

Change a user’s expiry date

With the -e option, you can specify the expiry date of a user account. In this scenario, the expiry date of user robert is set to 2021-10-01.

$ sudo usermod -e 2021-10-01 robert

To verify, use the chage command as follows.

$ sudo chage -l robert

Modify a user’s primary group

The primary group of the user is the default group that the user belongs to upon creation.

Here, you can see that the primary group for the user bob is bob.

$ id bob

To change the primary group to another group, say games, run usermod command with the -g option as follows

$ sudo usermod -g games bob

Thereafter, use the id command to confirm that the group has changed.

Summary

That was a roundup of some of the useful nature of the usermod command. As observed, it comes quite in handy in changing various properties or attributes of regular users. It’s our hope that you are now competent in making the most of the usermod command.

Similar Posts