Wednesday, April 17, 2013

Usermod command is used to
modify user account. This command is useful to change the parameters of only
existing users. It can change home directory of existing user, login name,
login shell, comment of passwd file etc.

We will understand usermod command with examples:

How to change home directory of existing user?

Sometimes we need to change
user’s home directory with new directory but all content of old directory
should have to move new directory. In this case, we need to use –m option with
–d, which moves all old directory content to new directory. If we don’t want to
move old directory content to new then no need to use –m option.

Test1 is already created user. We need to move old directory content to
new location and set it as new home directory.

Sometimes, we forget to mention shell while creating user. But we can change the default shell to desired as
below:

root@rhel:~/test2#
useradd test1

root@rhel:~/test2#
su test1

$

$

$ echo $USER

test1

$

$ echo $SHELL

/bin/sh

$

$ exit

root@rhel:~/test2#
usermod -s /bin/bash test1

root@rhel:~/test2#
su test1

test1@rhel:/root/test2$
echo $SHELL

/bin/bash

test1@rhel:/root/test2$

Rename the user

We can change the login name of
user with usermod command. But if
you change the username then it will not impact on other details like home
directory, expiry date etc. It will remain same.

root@rhel:~/test2#
cat /etc/passwd | grep test

test1:x:1008:1010::/home/test1:/bin/bash

root@rhel:~/test2#
usermod -l login1 test1

root@rhel:~/test2#
cat /etc/passwd | grep test

login1:x:1008:1010::/home/test1:/bin/bash

root@rhel:~/test2#
su login1

login1@rhel:/root/test2$

Lock user’s password

Root user can lock user’s
password as well as unlock it
using the usermod command. As below example, we locked user’s password by –L
option through root user and then tried to switch user from normal user but
couldn’t login. Again login by root user, unlock the user’s password by –U
option and the tried to login then it is allowing successfully.

root@rhel:~#
usermod -L login1

root@rhel:~#
exit

logout

machindra@rhel:~$

machindra@rhel:~$
su login1

Password:

su:
Authentication failure

machindra@rhel:~$
sudo -i

root@rhel:~#
usermod -U login1

root@rhel:~#
exit

logout

machindra@rhel:~$
su login1

Password:

login1@rhel:/home/test$

login1@rhel:/home/test$

Change UID of existing user

We can change the UID of
existing user by usermod command. For this purpose, we need to use –u option with
desired UID and it should not be used by any other user.

Old UID: 1008

New UID: 3045

root@rhel:~#

root@rhel:~#
cat /etc/passwd | grep test

login1:x:1008:1010::/home/test1:/bin/bash

root@rhel:~#
usermod -u 3045 login1

root@rhel:~#

root@rhel:~#
cat /etc/passwd | grep test

login1:x:3045:1010::/home/test1:/bin/bash

root@rhel:~#

Change the group of user

We can change the group of user
with –g option. Old group id of user login1 is 1010 and we will change it to
130 i.e. group will be rdeck.