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.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Linux Systems commands queries.
In this context, we shall look into some of the usermod commands that you can use to change a regular user's properties.
The usermod command takes the following syntax format:
$ usermod options username
In most cases, usermod command is used for 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 implement this , use the -a -G or simply -aG in the syntax as seen below:
$ usermod -aG group user
Let us consider the following command where we are adding a regular user "linuxapt" to the sudo group:
$ usermod -aG sudo group linuxapt
Now, invoke the groups command to verify the groups that the user linuxapt belongs to. Initially, the user only belonged to the group linuxapt. After adding the user to the sudo group, the user now belongs to 2 groups: linuxapt and sudo groups:
$ groups linuxapt
With usermod -d command, you can also change the default home directory as follows:
$ usermod -d /home/new_directory user
Here, we have changed the home directory of user linuxapt to the /home/test directory:
$ sudo usermod -d /home/test linuxapt
Using the -l flag, you can change the user's login name as follows:
$ sudo usermod -l john linuxaptÂ
In this example, the command changes the user's login name from linuxapt to john.
The -u flag allows you to change the User ID of the user. Here, we have set the UID of user john from the default 1005 to 4321:
$ sudo usermod -u 4321 john
In addition, you can also lock a user account using the -L option. In the example below, we are locking the user the user john.
$ sudo usermod -L john
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 john
With the -e option, you can specify the expiry date of a user account. Here, the expiry date of user john is set to 2021-10-01.
$ sudo usermod -e 2021-10-01 john
To verify, use the chage command as follows:
$ sudo chage -l john
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 linuxapt is linuxapt.
$ id linuxapt
To change the primary group to another group, say games, run usermod command with the -g option as follows
$ sudo usermod -g games linuxapt
Thereafter, use the id command to confirm that the group has changed.
This article covers different use cases of using the usermod command. The usermod command in Linux is used to manage user properties at command line. The syntax of the usermod command is following:
$ usermod [option] [argument] username