This command is used modify the user attributes like user id, group, name etc.
Syntax:
Usermod [options] username
The following are the list of options to modify already existing user attributes.
-d this option is used to change the working directory of the user
-g this option is used to change the primary group of the user
-G this option is used to change the secondary group of the user
-L this option is used to lock the user account.
-U this option is used to unlock the user account.
-s this option is used to assign shell to the user.
-e this option is used to assign specific expiry date to the particular user.
-u this option is used to modify the user id of the user.
-l this option is used to modify the login name of the user.
-c this option is used to change the comment or assign the comment to the particular user.
Let us see some examples to understand more on this topic.
Example-1:
Assigning comment to a user:
So we are going to assign a comment to the user john by using “-c” option.
[root@sys1 ~]# grep --color DB2admin /etc/passwd
john:x:501:501:DB2admin:/home/john:/bin/bash
john:x:501:501:DB2admin:/home/john:/bin/bash
Example -2:
Changing working directory of the user:
To change working directory of the user we use ‘-d’ option with usermod command.
[root@sys1 ~]# usermod -d /repo john
[root@sys1 ~]# grep --color /repo /etc/passwd
john:x:501:501:DB2admin:/repo:/bin/bash
[root@sys1 ~]# grep --color /repo /etc/passwd
john:x:501:501:DB2admin:/repo:/bin/bash
Example-3:
Changing primary group of the user:
To change primary group of the user we use ‘-g’ option with usermod command.
[root@sys1 ~]# usermod -g OracleDB steve
[root@sys1 ~]# id steve
uid=503(steve) gid=504(OracleDB) groups=504(OracleDB)
[root@sys1 ~]# id steve
uid=503(steve) gid=504(OracleDB) groups=504(OracleDB)
Example-4:
Changing secondary group of the user:
We use “-G” option with usermod command to change the secondary group of a particular user. To understand more observe the following scenario.
[root@sys1 ~]# id steve
uid=503(steve) gid=504(OracleDB) groups=504(OracleDB)
uid=503(steve) gid=504(OracleDB) groups=504(OracleDB)
Now I am going to add SQL as the secondary group of the user steve.
[root@sys2 ~]# usermod -G SQL steve
Now check whether the secondary group has assigned or not using id command.
[root@sys2 ~]# id steve
uid=502(steve) gid=504(OracleDB) groups=504(OracleDB),510(SQL)
uid=502(steve) gid=504(OracleDB) groups=504(OracleDB),510(SQL)
Example-5:
Locking and unlocking the user account:
By locking the user account the user was not able to login to the server. To lock the user account we use ‘-L’ option with user mod command. Have a look on the following scenario to understand more.
[root@sys2 ~]# usermod -L steve
From the above command, we locked the user steve. Now try to login with the credentials of steve.
login as: steve
steve@192.168.0.17's password:
Access denied
steve@192.168.0.17's password:
Access denied
Hence, the user account was successfully locked. Now my question is how to check locked user account?
Simply use passwd command with –s option as shown below.
[root@sys2 ~]# passwd -S steve
steve LK 2016-01-26 0 99999 7 -1 (Password locked.)
steve LK 2016-01-26 0 99999 7 -1 (Password locked.)
From the above output the user account was successfully locked .
Now, I am going to unlock the user steve by using ‘-U’ with usermod command. After unlocking the user can be able to login to the server.
[root@sys2 ~]# usermod -U steve
[root@sys2 ~]# passwd -S steve
steve PS 2016-07-31 0 99999 7 -1 (Password set, SHA512 crypt.)
From the above scenario we are successfully unlocked the user account steve. Now I am trying to login as steve as follows.
login as: steve
steve@192.168.0.4's password:
[steve@sys2 ~]$
[root@sys2 ~]# passwd -S steve
steve PS 2016-07-31 0 99999 7 -1 (Password set, SHA512 crypt.)
From the above scenario we are successfully unlocked the user account steve. Now I am trying to login as steve as follows.
login as: steve
steve@192.168.0.4's password:
[steve@sys2 ~]$
From the above view the steve account was successfully unlocked.
Example-6:
Assigning a shell to the user:
To change or assign ashell to the user we use “-s” option with usermod command. To know more about the Shell go through the topic shell in the introduction
[root@sys2 ~]# grep steve /etc/passwd
steve:x:502:509::/home/steve:/bin/bash
steve:x:502:509::/home/steve:/bin/bash
From the above command the shell assigned to the steve is bash. Now I am going ro change the shell to c-shell.
[root@sys2 ~]# grep steve /etc/passwd
steve:x:502:509::/home/steve:/bin/csh
steve:x:502:509::/home/steve:/bin/csh
From the above lines the shell is successfully updated to the user steve.
Example-7:
Modifying the username of the user account:
Using ‘-l’ option with usermod command we can change the user name of a particular account. To understand more observe the following.
[root@sys2 ~]# grep --color steve /etc/passwd
steve:x:502:509::/home/steve:/bin/csh
[root@sys2 ~]# usermod -l steveJobs steve
[root@sys2 ~]# grep --color steveJobs /etc/passwd
steveJobs:x:502:509::/home/steve:/bin/csh
steve:x:502:509::/home/steve:/bin/csh
[root@sys2 ~]# usermod -l steveJobs steve
[root@sys2 ~]# grep --color steveJobs /etc/passwd
steveJobs:x:502:509::/home/steve:/bin/csh
This is how we can change the user name of a particular account.
Example-8:
Assigning an expiry date to a user account:
By using ‘-e’ option with the usermod command we can assign the expiry date to a particular user account.
Let us see practically,
Check the expiry date of the user steve using chage –l command. We can change the expiry date of a user using chage command also click here to view that.
[root@sys2 ~]# chage -l steve
Last password change : Jul 31, 2016
Password expires : never
Password inactive : never
Account expires : never
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
Last password change : Jul 31, 2016
Password expires : never
Password inactive : never
Account expires : never
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
From the above output we understood that there is no account expiry date to the user steve.
Now assign an expiry date to the user steve as follows.
[root@sys2 ~]# usermod -e 2018-01-01 steve
Now check weather expiry date has assigned or not.
[root@sys2 ~]# chage -l steve
Last password change : Jul 31, 2016
Password expires : never
Password inactive : never
Account expires : Jan 01, 2018
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
Last password change : Jul 31, 2016
Password expires : never
Password inactive : never
Account expires : Jan 01, 2018
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
Hence we assigned an expiry date to the user account steve.
Example-9:
Modifying the user id of an user account:
To change the user id of an user we use ‘-u’ option with usermod command. Observe the following to understand more.
Now I am going to change the user id of steve.
Check the user id of steve.
[root@sys2 ~]# id steve
uid=502(steve) gid=509(OracleDB) groups=509(OracleDB),510(SQL)
uid=502(steve) gid=509(OracleDB) groups=509(OracleDB),510(SQL)
Change the user id of steve from 502 to 520.
[root@sys2 ~]# id steve
uid=520(steve) gid=509(OracleDB) groups=509(OracleDB),510(SQL)
uid=520(steve) gid=509(OracleDB) groups=509(OracleDB),510(SQL)
Hence the user id of steve has changed successfully.
You can change different attributes of the user account within single command also.
0 comments:
Post a Comment