Modify User Account in CentOS/RedHat/Fedora Linux

To modify an existing User Account in CentOS/RedHat/Fedora you use ‘ usermod ‘ command as a root.

The Syntax  to modify an existing user in Linux:

usermod [options] User_Name

When we run ‘usermod’ command on shell promp these files are used and affected.

     /etc/passwd     – User account information
/etc/shadow     – Secure account information
/etc/group       – Group account informatio.
/etc/gshadow  –Secure group account information
/etc/login.defs – Shadow password suite configuration .

All command line options  used with the ‘usermod’ command are shown below

______________________________________________________________________

   Options                                                                 Description

______________________________________________________________________

 -c, –comment COMMENT           new value of the GECOS ommand in field
-d, –home HOME_DIR               new home directory for the user account
-e, –expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
-f, –inactive INACTIVE              set password inactive after expiration
to INACTIVE
-g, –gid GROUP                        force use GROUP as new primary group
-G, –groups GROUPS               new list of supplementary GROUPS
-a, –append                              append the user to the supplemental GROUPS
mentioned by the -G option without removing
him/her from other groups
-h, –help                                     display this help message and exit
-l, –login NEW_LOGIN             new value of the login name
-L, –lock                                       lock the user account
-m, –move-home                     move contents of the home directory to the
new location (use only with -d)
-o, –non-unique                         allow using duplicate (non-unique) UID
-p, –password PASSWORD     use encrypted password for the new password
-R, –root CHROOT_DIR              directory to chroot into
-s, –shell SHELL                       new login shell for the user account
-u, –uid UID                                 new UID for the user account
-U, –unlock                                   unlock the user account
-Z, –selinux-user SEUSER        new SELinux user mapping for the user account
______________________________________________________________________

  

 Adding comments to an existing user account:

To add the brief  comment about the user Account you use ‘-c ‘ option with the ‘usermod’ command.

[root@localhost ~]# usermod -c “This is an dev Acc” noor
[root@localhost ~]# grep  ‘noor’ /etc/passwd
noor:x:1002:1008:This is an dev Acc:/home/noor:/bin/bash
[root@localhost ~]#

To change User default home directory : 

By default home directory in linux is ‘/home/username/ ‘. you can change the default home directory path and directory name with ‘-d ‘ option with the ‘ usermod ‘command.

[root@localhost ~]# grep -E  ‘noor’ /etc/passwd
noor:x:1002:1008:This is an dev Acc:/opt/my_home:/bin/bash
[root@localhost ~]#

To set user account Expiry date  :

To set expiry Date  of a user account you use ‘-e ‘ option with the ‘ YYYY-MM-DD ‘ date format.You check the status of user account expiry date you use ‘ chage -l ‘ command .

 [root@localhost ~]# chage -l noor
Last password change                                    : Mar 18, 2016
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : Mar 31, 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


[root@localhost ~]# usermod -e 2016-04-30 noor
[root@localhost ~]# chage -l noor
Last password change                                    : Mar 18, 2016
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : Apr 30, 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
[root@localhost ~]#

To Change User Primary Group:

To set or change a user primary group, we use  ‘-g ‘ option with ‘ usermod ‘ command.

[root@localhost ~]# id noor
uid=1002(noor) gid=1008(noor) groups=1008(noor)


[root@localhost ~]# usermod -g developer noor
[root@localhost ~]# id -Gn noor
developer
[root@localhost ~]#

To Change user Login name :

To change any existing user login name, You  use ‘-l ‘ (new login) option with ‘ usermod ‘ command

[root@localhost ~]# usermod -l noor_dev noor
[root@localhost ~]# id noor
id: noor: no such user
[root@localhost ~]# id noor_dev
uid=1002(noor_dev) gid=1005(developer) groups=1005(developer)
[root@localhost ~]#


To Lock an Existing user Account:

To Lock an existing user Account you can use ‘ -L ‘ option with the usermod command.

[root@localhost ~]# usermod -L noor_dev

[root@localhost ~]# cat /etc/shadow | grep noor_dev
noor_dev:!$1$MmDi0RKe$nsbt7YPqYds8Pcosq6sVL.:16878:0:99999:7::16921:

Note:- ! mark is the sign of Locked User


To Unlock an Existing user Account:

To unlock an existing locked user account ,you can use ‘ -L ‘ option with ‘usermod’ command .


[root@localhost ~]# cat /etc/shadow | grep noor_dev
noor_dev:!$1$MmDi0RKe$nsbt7YPqYds8Pcosq6sVL.:16878:0:99999:7::16921:[root@localhost ~]#
[root@localhost ~]# usermod -U noor_dev


[root@localhost ~]# cat /etc/shadow | grep noor_dev
noor_dev:$1$MmDi0RKe$nsbt7YPqYds8Pcosq6sVL.:16878:0:99999:7::16921:
[root@localhost ~]#

To create an Un-encrypted Password for User:

To create an un-encrypted password, you use  ‘-p’ option (password) with ‘usermod ‘command . After setting password, now check the shadow file to see password is set to un-encrypted format. 

[root@localhost ~]# usermod -p system@123 noor_dev

[root@localhost ~]# cat /etc/shadow | grep noor_dev
noor_dev:system@123:16878:0:99999:7::16921:
[root@localhost ~]#

 To change Shell an existing User:

By default login shell in Linux is  ‘/bin/bash ‘ .To check Installed Shell on your system  type the’ chsh -l ‘ command on the shell prompt.

[root@localhost ~]# chsh -l
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
[root@localhost ~]#

If you want to chande login shell  ‘ /bin/tcsh ‘ for user noor_dev , you can use ‘ -s ‘ option with usermod command.

[root@localhost ~]# usermod -s /bin/csh noor_dev

Now you check the login shell for user ‘ noor_dev ‘ 

 [root@localhost ~]# cat /etc/passwd |grep noor_dev
noor_dev:x:1002:1005:This is an dev Acc:/opt/my_home:/bin/csh
[root@localhost ~]#

You can also modify User account with multiple optin.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s