Usermod command is used to modify users which already exist in your system.
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.
usermod -d /new_path/ username
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
chown test:test /home/developer
chmod 700 /home/developer
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
usermod -e [date in yyyy-mm-dd format] username
usermod -e 2020-12-23 test
Here we changed the expiry date of the user to 23rd Dec 2020.
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.
usermod -g [group_to_which_user_has_to_switch] username
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.
Add group to user
Adding a group to users can also be done using the usermod command.
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.
usermod -aG [group_name] username
usermod -aG testgroup test
Here user is added to one more group testgroup.
Change username login
User name can be changed for user using usermod command, here you will use -l switch
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.
usermod -L [user name]
usermod -L infojinx
Similarly to unlock a user by usermod command you will have to use -U switch.