Linux usermod command is used to modify or change an existing user’s attributes. User's attributes are home directory, shell, password expiration date, groups, UID etc.
When the command runs, data in the /etc/passwd (user account information), /etc/shadow (secure account information) and /etc/group (group information) files are updated accordingly.
Only root (superuser) 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 tutorial, we will go through some examples to learn usermod command.
1) Changing home directory of a user
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
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 the 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)
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)
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
Users can be unlocked as follows which will remove the !
in front of the encrypted password.
# usermod –U test
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
6) Changing login and password
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
7) Change shell of a user
We can use usermod command to change the shell of a user. The following command will change the shell of the user 'newtest' to '/bin/bash'.
# usermod -s /bin/bash newtest
Hi,
Thanks for your valuable information,
But I need a script for useradd:
Suppose from root user, I create a user & I delete a user, then
I want a email alert at useradd & userdel, when root fire these commands,
So if you know then pls share
Thanks Jasvinder for the comments. You need a script that send email when root create a user using useradd command and also send email when deleting a user ?