How to use usermod command to manage user account

To use Usermod command for managing user account

Usermod command is used to change the home directory of existing user, login name, login shell, comment of passwd file etc. The files that may be affected during this operation are /etc/passwd (user account information), /etc/shadow (secure account information) and /etc/group (group information). Only root/super user can use this command.

Adding the information to user

[root@linuxhelp Desktop]# usermod -c " Hello user"  linuxhelp

The option ‘ -c’ with usermod is used to set the information to the user. After adding the information to the user you view the status in /etc/passwd file.

[root@linuxhelp Desktop]# grep -E --color ' linuxhelp'  /etc/passwd
linuxhelp:x:501:501:Hello user:/home/linuxhelp:/bin/bash

Changing the User home directory

By default user details will be stored in home directory, if you want to verify run the following command.

[root@linuxhelp Desktop]# grep -E --color ' linuxhelp'  /etc/passwd
linuxhelp:x:501:501:Hello user:/home/linuxhelp:/bin/bash

Now change the home directory for the user. Here, we are changing home directory as /var/www/html.

[root@linuxhelp Desktop]# usermod -d /var/www/html/ linuxhelp

Verify the user status.

[root@linuxhelp Desktop]# grep -E --color ' linuxhelp'  /etc/passwd
linuxhelp:x:501:501:Hello user:/var/www/html/:/bin/bash

Setting the expiry date for user account

Use option &ndash e with usermod to set the expiry date for the user and verify the account information by using chage command.

[root@linuxhelp Desktop]# usermod -e 2016-06-20 linuxhelp
[root@linuxhelp Desktop]# chage -l linuxhelp
Last password change                    : Jun 14, 2016
Password expires                    : never
Password inactive                    : never
Account expires                    : Jun 20, 2016
Minimum number of days between password change    : 0
Maximum number of days between password change    : 99999
Number of days of warning before password expires    : 7

Changing the User primary group

Use option ‘ -g’ with usermod to change the user' s primary group

[root@linuxhelp Desktop]# usermod -g linux linuxhelp

Run the following command to verify the details.

[root@linuxhelp Desktop]# id linuxhelp
uid=501(linuxhelp) gid=502(linux) groups=502(linux)

Adding the group to existing user

Use option ‘ -G’ with usermod command, it will add new group to the existing user.

[root@linuxhelp Desktop]# usermod -G linux_grp linuxhelp

Verify the user account status.

[root@linuxhelp Desktop]# id linuxhelp
uid=501(linuxhelp) gid=502(linux) groups=502(linux),503(linux_grp)

Adding supplementary and primary groups to user

Add a user to any one of the supplementary group, you can use the options ‘ -a‘ and ‘ -G‘ . We are going to add a user account linuxhelp to wheel group. Now this user account can execute any root privileged commands.

[root@linuxhelp Desktop]# usermod -a -G wheel linuxhelp
[root@linuxhelp Desktop]# id linuxhelp
uid=501(linuxhelp) gid=502(linux) groups=502(linux),10(wheel),503(linux_grp)

Changing user login name

Use option ‘ -l’ with usermod command to change the existing login name.

[root@linuxhelp Desktop]# usermod -l linux_user linuxhelp

When you login with old login name it will show an error.

[root@linuxhelp Desktop]# id linuxhelp
id: linuxhelp: No such user

Try login with the newly created login name.

[root@linuxhelp Desktop]# id linux_user
uid=501(linux_user) gid=502(linux) groups=502(linux),10(wheel),503(linux_grp)

Lock the user account

Use option ' &ndash L ' with usermod to lock the user.

[root@linuxhelp Desktop]# usermod -L user_1

Verify by running the below command.

[root@linuxhelp Desktop]# grep -E --color ' user_1'  cat /etc/shadow
grep: cat: No such file or directory
/etc/shadow:user_1:!$6$VYCTPMF4$3sVkRBVsSJ9Ud5CiO2LEDqdZc/mRyOhFRvBjxwP917UBwc9U65hjVjmQPEG9aGQ/xy0p/Dp40Q8Lr9XCAf4sp.:16966:0:99999:7:::

Now you saw the ! Factorial symbol presented in begin of encrypted password which means the user account will be locked.

Unlock the user account

Use option ' &ndash U' with usermod to unlock the user.

[root@linuxhelp Desktop]# usermod -U user_1

Verify by using the following command.

[root@linuxhelp Desktop]# grep -E --color ' user_1'  cat /etc/shadow
grep: cat: No such file or directory
/etc/shadow:user_1:$6$VYCTPMF4$3sVkRBVsSJ9Ud5CiO2LEDqdZc/mRyOhFRvBjxwP917UBwc9U65hjVjmQPEG9aGQ/xy0p/Dp40Q8Lr9XCAf4sp.:16966:0:99999:7:::

Now the ! factorial symbol was removed and the user account will be unlocked.

Creating un-encrypted password for the user

Use ' &ndash p' with usermod command to create the encrypted password for the user.

[root@linuxhelp Desktop]# usermod -p linuxc user_1

Run the following command to verify the status.

[root@linuxhelp Desktop]# grep -E --color ' user_1'  cat /etc/shadow
grep: cat: No such file or directory
/etc/shadow:user_1:linuxc:16966:0:99999:7:::

Changing theUID and PID for the user account

Before setting the UID and GID for user account, verify the default setting of the user.

[root@linuxhelp Desktop]# id user_1
uid=123(user_1) gid=125(user_1) groups=125(user_1),505(mygrp)

Set the UID and GID for user account

[root@linuxhelp Desktop]# usermod -u 155 user_1
[root@linuxhelp Desktop]# groupmod -g 255 user_1

Now run the below command to verify the status.

[root@linuxhelp Desktop]# id user_1
uid=155(user_1) gid=255(user_1) groups=255(user_1),505(mygrp)

Changing the user shell location. The default user shell location is /bin/bash.

[root@linuxhelp Desktop]# grep -E --color ' user_1'  cat /etc/passwd
grep: cat: No such file or directory
/etc/passwd:user_1:x:155:255::/home/user_1:/bin/bash

Here, we are changing the user shell location from /bin/bash into /bin/sh

[root@linuxhelp Desktop]# usermod -s /bin/sh user_1
[root@linuxhelp Desktop]# grep -E --color ' user_1'  cat /etc/passwd
grep: cat: No such file or directory
/etc/passwd:user_1:x:155:255::/home/user_1:/bin/sh

Changing user home directory into new location

Default user' s home directory location is shown below.

[root@linuxhelp Desktop]# grep -E --color ' user_1'   /etc/passwd
user_1:x:155:255::/home/user_1:/bin/sh

To change the user home directory to new location, run the following command.

[root@linuxhelp Desktop]# usermod -d /etc/user/ -m user_1
[root@linuxhelp Desktop]# grep -E --color ' user_1'   /etc/passwd
user_1:x:155:255::/etc/user/:/bin/sh
FAQ
Q
How to add a user to a wheel group in Linux?
A
For adding a user to wheel group use "usermod -a -G wheel group name".
Q
What algorithm does the "/etc/passwd" uses yo store the passwords in Linux?
A
Linux uses hash algorithm for storing password.
Q
What is the command for changing user id?
A
Please use "usermod -u 777 username".
Q
How to Unlock the user account?
A
The command for unlocking user account is "usermod -U username".
Q
How to set userid and groupid?
A
You can set userid and groupid by "usermod -u 666 -g 777 username"