The linux command “usermod” is used to modify a user’s information. 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.
The basic syntax of this command is as follows.
usermod [-c comment] [-d home_dir [-m]] [-e expire_date] [-f inactive_time]
[-g initial_group] [-G group [,...]] [-l login_name] [-p passwd]
[-s shell] [-u uid [-o]] [-L|-U]login
In this article, we will go through some example usages of “usermod” command which will help you to learn these options in detail. First we can create a user “test” using useradd. In order to view user information, we can use the “id command.
# id test
uid=501(test) gid=501(test) groups=501(test)
Example 1: Changing the home directory of user “test”
Suppose the current home directory of the user “test” is /home/test and you want to change it to the existing directory /home/testnew without copying the contents of /home/test, you can use the following command.
#usermod –d /home/testnew test
If you want to move the contents of /home/test also (if the new directory doesn’t exist, it will create and move), you need to use the option “-m”.
#usermod –d /home/testnew –m test
Example 2: Adding groups to a user
When a user is added using “useradd” command without specifying group, then a group with the same name as that of user will be created. This is the primary group of the user. You can add as many groups to a user using the option “-G” as follows.
Suppose, you need to add a group “developer” to the user “test”, you can add it as follows.
#usermod –G developer test
Please note that, if you added the user to any other groups earlier (other than the primary group), that will get removed by the above command.
So, if you want to preserve the current groups of the user and add one more group you need to use the option –aG as follows.
#usermod –aG developer test
# id test
uid=501(test) gid=501(test) groups=501(test),506(pros),508(developer)
Example 3: Changing the primary group of a user
If you want to add a group as the primary group of the user, you can do it as follows.
# usermod –g developer test
# id test
uid=501(test) gid=508(developer) groups=508(developer), 506(pros)
Example 4: Locking and Unlocking users
In some cases, you may need to temporarily lock the account. This can be done with the “-L” option. This puts a '!' in front of the encrypted password, effectively disabling the password.
# usermod –L test
User can be unlocked as follows which will remove the '!' in front of the encrypted password.
# usermod –U test
Example 5: Changing the expiry data of an account
You can use the following command to disable the account “test” on 2012-12-01.
usermod -e 2012-12-01 test
Example 6: Changing login and password using usermod
You can change the login name itself using the –l switch.
# usermod –l newtest test
# id test
Id: test: No such user
# id newtest
uid=501(newtest) gid=508(developer) groups=508(developer), 506(pros)
You can change the password as follows.
# usermod –p newpass newtest
Example 7: Changing shell of a user
The “shell” provided to a user can be changed as follows. This will change the shell of “newtest” user to “/bin/bash”.
# usermod –s /bin/bash newtest