How to use usermod command in Linux

Usermod command is used to modify users which already exist in your system. 

Syntax 

usemod [options] username

Example uses of usermod command

Change home directory of user

When a user is created a home directory is assigned to him. Now in case you need to change directory use switch -d . Note: you can assign home directory at path other than default path at time user addition. 

Syntax

usermod -d /new_path/ username

Example

usermod -d /home/developer test

Note: Before assigning a new home directory, you need to create a directory for it. Also you need to give proper permission to that directory

For above example you will need to create a new developer directory and give permissions for the same. Follow steps given below

mkdir /home/developer

chown test:test /home/developer

chmod 700 /home/developer

usermod -d

Change or Set expiry date of user

At time addition of user you may provide an expiry date for user account. usemod command can change or set expiry date for user account

Syntax

usermod -e [date in yyyy-mm-dd format] username

Example

usermod -e 2020-12-23 test

Here we changed the expiry date of the user to 23rd Dec 2020.

usermod -e

Change user’s group

Another use of usermod command is to change the primary group of users, for this you will have to use switch -g.

Syntax

usermod -g [group_to_which_user_has_to_switch] username

Example

usermod -g test_group test

Here the user’s primary group has been changed to test_group.

Note: group must exist before you can switch the user to that group.

usermod -g

Add group to user

Adding a group to users can also be done using the usermod command.  

Syntax

usermod -G [group_name] username

Note: When using this command the user will be added to groups mentioned in the command. The user will be removed from all other groups excluding the primary group to which the user is member.

To ensure previously used group information is not lost use -a switch along with G switch.

Syntax

usermod -aG [group_name] username

Example

usermod  -aG testgroup test

Here user is added to one more group testgroup. 

usermod -G and usermod -aG

Change username login

User name can be changed for user using usermod command, here you will use -l switch

Syntax 

usermod -l infojinx1 test

Locking and unlocking user

To lock a user by usermod command you will have to use -L switch. User will not be deleted but disabled.

Syntax

usermod -L [user name]

Example

usermod -L infojinx

Similarly to unlock a user by usermod command you will have to use -U switch.

usermod change login name (usermod -l), lock (usermod -L) and unlock (usermod -U)
Exit mobile version