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 Ibmi Media, 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.
What is Usermod Command Syntax ?
The usermod command takes the following syntax format:
$ usermod options username
How to Add a regular user to a Group ?
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
How to modify 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
Here, we have changed the home directory of user linuxapt to the /home/test directory:
$ sudo usermod -d /home/test linuxapt
How to Change a user's login name ?
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.
How to Change User ID ( UID ) for a user ?
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
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 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
How to Change a user's expiry date ?
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
How to change 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 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.
[Need help in fixing Linux Systems issues? We can help you. ]