BEST LINUX USERMOD COMMAND WITH EXAMPLES
Introduction
In this article we are going to discuss on How to use Linux usermod command. usermod command is used to Modify the already created user’s settings like change primary group of the user, add secondary groups to user, change UID and GID of the user and so on. usermod is a another tool like useradd to manager user accounts in Linux.
Follow the below Linux usermod commands :
Set Comment for a User Account
To set comment for a User Account in Linux we can use Linux usermod command with argument -c. The comment is useful for mention some important Information about user like Contact Information or some personal Information or something else. refer the command below.
[root@localhost ~]# usermod -c "my name is john" john # Set comment for a User Account # Confirm the Setting [root@localhost ~]# cat /etc/passwd | grep john john:x:502:502:my name is john:/home/john:/bin/bash
Lock a User Account
You can use Linux usermod command with argument -L to lock a User Account. Here I am locking the user account john. Refer the command below.
To confirm if the user account is locked or not you can check in /etc/shadow file. Locked Account will have a exclamation mark (!) at beginning of the encrypted password (Highlighted in Red color).
[root@localhost ~]# usermod -L john # Lock a User Account # Confirm the Setting [root@localhost ~]# cat /etc/shadow | grep john john:!$1$Rr8aOLUq$txkhPu.DqYCSG7FDEc4O71:17312:0:99999:7:::
Unlock a User Account
To Unlock a user account you can use usermod command with argument -U. Refer the command below.
[root@localhost ~]# usermod -U john # Unlock a User Account # Confirm the Setting [root@localhost ~]# cat /etc/shadow | grep john john:$1$Rr8aOLUq$txkhPu.DqYCSG7FDEc4O71:17312:0:99999:7:::
Change UID (User ID)
You can change the UID (User ID) of a user account using Linux usermod command with argument -u. Here I am changing UID of user john whose current UID is 502 and I am changing to 555. Refer the command below.
[root@localhost ~]# id john uid=502(john) gid=502(john) groups=502(john) # Confirm the Setting [root@localhost ~]# usermod -u 555 john # Change UID of a User Account [root@localhost ~]# id john uid=555(john) gid=502(john) groups=502(john)
Also Read :
- BEST ZIP COMMAND WITH EXAMPLES IN LINUX
- BEST RPM COMMAND ( REDHAT PACKAGE MANAGER ) WITH EXAMPLES IN LINUX
- COMPLETE UNIX COMMANDS AND BASIC LINUX COMMANDS WITH EXAMPLES FOR BEGINNERS
Rename a Login Name
To rename a login name we can use usermod command with argument -l. Here I am renaming the login name from john to ricky.
Syntax : usermod -l [New_Name] [Old Name]
[root@localhost ~]# usermod -l ricky john # Raname Login Name
After rename the Login Name when you check the User Information with old username you will get below error as the Login name is no more exist.
[root@localhost ~]# id john
id: john: No such user
Now check the user Information with New Login Name.
[root@localhost ~]# id ricky # Check the User Information with New Login Name
uid=555(ricky) gid=502(john) groups=502(john)
Set Expiry Date for a User Account using Linux Usermod Command
You can set expiry date for an user account by using Linux usermod command with argument -e.
[root@localhost ~]# usermod -e 2017-06-15 ricky # Set Expiry date for a User Account # Confirm the Setting [root@localhost ~]# chage -l ricky Last password change : May 26, 2017 Password expires : never Password inactive : never Account expires : Jun 15, 2017 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
Change Primary Group of a User Account
You can change the Primary group of a User Account by using usermod command with argument -g. Here I have a User Account named tony whose current Primary GID is 556 and I am changing the GID to 503. refer the command below.
[root@localhost ~]# id tony uid=556(tony) gid=556(tony) groups=556(tony) # Confirm the Setting [root@localhost ~]# usermod -g admins tony # Change the Primary Group [root@localhost ~]# id tony uid=556(tony) gid=503(admins) groups=503(admins)
Add Multiple Supplementary/Secondary Groups using Linux usermod command
You can add multiple Supplimentry group OR Secondary groups to a user account. You can do so by using usermod command with argument -G. Here we are using a extra argument i.e. -a which is also referred as append.
Note : If you don’t use the argument -a with -G then Linux usermod will remove all previously added Secondary groups and only add the current one which is you are adding.
[root@localhost ~]# usermod -a -G developers,workers ricky # Add Supplementary/ Secondary to a User Account # Confirm the Setting [root@localhost ~]# id ricky uid=555(ricky) gid=502(john) groups=502(john),557(developers),558(workers)
Change Home Directory
To change the Home directory of a User Account use Linux usermod command with argument -d. Refer the command below.
[root@localhost ~]# usermod -d /users/elinuxbook elinuxbook # Change the Home Directory # Confirm the Setting [root@localhost ~]# cat /etc/passwd | grep elinuxbook elinuxbook:x:557:559::/users/elinuxbook:/bin/bash
Set Password Inactive for a User Account
Password Inactive is a password aging policy by which user can use the same password even after expire the password till allowed days. Here I am allowing 10 days of password inactive for user elinuxbook. To set Password Inactive you can use Linux usermod command with argument -f. Refer the command below.
[root@localhost ~]# usermod -f 10 elinuxbook # Set Password Inactive for a User Account # Confirm the Setting [root@localhost ~]# cat /etc/shadow | grep elinuxbook elinuxbook:$1$HQEiPMJa$A1IXvuj8Ys08eLolPz6fP1:17312:0:99999:7:10::
Move Home Directory of a User Account
Linux usermod command also allowing us to move the home directory of a user account with it’s data. We can do so by using Linux usermod command with arguments -m (for moving the home directory) and -d (to change the directory).
Let’s do the task more briefly :
Here I am creating a user account named shree and set password for that.
[root@localhost ~]# useradd shree # Creating a New User Account [root@localhost ~]# passwd shree # Set Password for User Account Changing password for user shree. New password: Retype new password: passwd: all authentication tokens updated successfully.
Now create some files in home directory of the user account.
[root@localhost ~]# cd /home/shree/ # Changing to User's Home Directory [root@localhost shree]# ls [root@localhost shree]# touch file{1,2,3,4,5}.txt # Creating some files [root@localhost shree]# ls file1.txt file2.txt file3.txt file4.txt file5.txt
So let’s go ahead and move the home directory of the user account shree. Refer the below command.
[root@localhost ~]# usermod -m -d /users/shree shree # Move the Home Directory of a User Account # Confirm the Setting [root@localhost ~]# cat /etc/passwd | grep shree shree:x:558:560::/users/shree:/bin/bash
Confirm if data moved or not.
[root@localhost ~]# cd /users/shree/ [root@localhost shree]# ls file1.txt file2.txt file3.txt file4.txt file5.txt
Also you can notice that the Home Directory is not available on it’s previous location.
[root@localhost shree]# cd /home/ [root@localhost home]# ls elinuxbook john tony
Change Shell Script of User Account
To change the Shell Script of a User Account you can use usermod command with argument -s. Refer the command below.
[root@localhost ~]# usermod -s /etc/test elinuxbook # Change Shell Script of a User Account [root@localhost ~]# cat /etc/passwd | grep elinuxbook elinuxbook:x:557:503::/users/elinuxbook:/etc/test
For more Help on usermod Command and Arguments
For help on Linux usermod command and it’s arguments you can refer the below.
[root@localhost ~]# usermod --help # For more Help on usermod command
Usage: usermod [options] LOGIN
Options:
-c, --comment COMMENT new value of the GECOS 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
-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 new SELinux user mapping for the user account
Manual Page of usermod command
You can refer the manual page of Linux usermod command
[root@localhost ~]# man usermod # Refer the Manual Page of Linux usermod command
If you found this article useful then Like Us, Share Us, Subscribe our Newsletter OR if you have something to say then feel free to comment on the comment box below.