This tutorial explains Linux “usermod” command, options and its usage with examples.
The usermod command modifies the system account files to reflect the changes that are specified on the command line.
usermod [options] username
Add the user to the supplementary group(s). Use only with the -G option.
-c COMMENT, –comment COMMENT
The new value of the user’s password file comment field. It is normally modified using the chfn utility.
-d, –home HOME_DIR
The user’s new login directory. If the -m option is given, the contents of the current home directory will be moved to the new home directory, which is created if it does not already exist.
-e EXPIRE_DATE, –expiredate EXPIRE_DATE
The date on which the user account will be disabled. The date is specified in the format YYYY-MM-DD. An empty EXPIRE_DATE argument will disable the expiration of the account. This option requires a /etc/shadow file. A /etc/shadow entry will be created if there were none.
-f INACTIVE, –inactive INACTIVE
The number of days after a password expires until the account is permanently disabled. A value of 0 disables the account as soon as the password has expired, and a value of -1 disables the feature. This option requires a /etc/shadow file. A /etc/shadow entry will be created if there were none.
-g GROUP, –gid GROUP
The group name or number of the user’s new initial login group. The group must exist. Any file from the user’s home directory owned by the previous primary group of the user will be owned by this new group.
-G, –groups GROUP1[,GROUP2,…[,GROUPN]]
A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no intervening whitespace. The groups are subject to the same restrictions as the group given with the -g option. If the user is currently a member of a group which is not listed, the user will be removed from the group.
-l NEW_LOGIN, –login NEW_LOGIN
The name of the user will be changed from LOGIN to NEW_LOGIN. Nothing else is changed. In particular, the user’s home directory or mail spool should probably be renamed manually to reflect the new login name.
Lock a user’s password. This puts a “!” in front of the encrypted password, effectively disabling the password. You can’t use this option with -p or -U. If you want to lock the account (not only access with a password), you should also set the EXPIRE_DATE to 1 using -e (see above).
-m, –move-home Move the content of the user’s home directory to the new location. This option is only valid in combination with the -d (or –home) option. usermod will try to adapt the ownership of the files and to copy the modes, ACL and extended attributes, but manual changes might be needed afterwards.
When used with the -u option, this option allows to change the user ID to a non-unique value.
-p PASSWORD, –password PASSWORD
The encrypted password, as returned by crypt. This option is not recommended because the password (or encrypted password) will be visible by users listing the processes (using the ps command, for instance). The password will be written in the local /etc/passwd or /etc/shadow file.
-R CHROOT_DIR, –root CHROOT_DIR
Apply changes in the CHROOT_DIR chroot directory and use the configuration files from the CHROOT_DIR directory.
-s SHELL, –shell SHELL
The name of the user’s new login shell. Setting this field to blank causes the system to select the default login shell.
-u, –uid UID
The new numerical value of the user’s ID. This value must be unique, unless the -o option is used. The value must be non-negative. The user’s mailbox, and any files which the user owns and which are located in the user’s home directory will have the file user ID changed automatically. The ownership of files outside of the user’s home directory must be fixed manually. No checks will be performed with regard to the UID_MIN, UID_MAX, SYS_UID_MIN, or SYS_UID_MAX values from /etc/login.defs.
Unlock a user’s password. This removes the “!” in front of the encrypted password. You can’t use this option with -p or -L. If you want to unlock the account (not only access with a password), you should also set the EXPIRE_DATE (for example to 99999, or to the EXPIRE value from /etc/default/useradd) using the -e option (see above).
1. Modify the home directory for the exampleuser account changing it to /home/exampleusernew.
# usermod -d /home/exampleusernew exampleuser
2. Change comment using -c command of abc account
# usermod -c "I'm usermod" abc
3. Change the primary group “abc” user belongs to using -g option and a groupid of 100
# usermod -g 100 abc
4. Provide supplementary groups to “abc” user using -G option
# usermod -G users,students abc
Note : 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.
5. For preserving the previously added groups and also add new use -a option
# usermod -aG users,students abc
6. Change name of “abc” user to newname using -l option
# usermod -l newname abc
7. 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
8. Changing the expiry date of an account
You can use the following command to disable the account “test” on 2014-12-01.
usermod -e 2014-12-01 test
9. Change password of user “abc” using -p option
# usermod –p newpass abc
10. 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
11. Changing uid of user “abc” to 9195
# usermod -u 9195 abc
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.