Change Linux user information - usermod
To change user information, use the usermod span> command.
It is possible to change the user's home directory, group, deadline, etc. We will explain here how to change groups, how to change home directories, commands, options, formats and usage examples.
The usermod command changes /etc/passwd, /etc/shadow, /etc/group file.
Last Update : December 12, 2018
Change Linux user information - usermod Contents
1. Usage and options of "usermod" command
Usage
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
login is the user account name.
[ ] Can be omitted. If you do not specify at least one option, nothing will be changed. When actually executing, [] is not required.
OPTION
-c comment | Change the comment of login. |
-d home_dir | Change the home directory of login to "home_dir". Also, if -m is appended to the option, the contents of the current home directory will be moved to the new home directory. Also, if the directory does not exist, it is newly created. |
-e expire_date | The date on which the user account will be disabled. The date is specified in the format YYYY-MM-DD. |
-f inactive_days | 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. |
-g initial_group | Change the default group to initial_group. |
-G group,[...] | Add subgroup to which the account belongs. Separate groups with commas. If you do not specify a group that the user originally belonged to here, it will be deleted from that group. |
-l login_name | Change the login name of the user from login to login_name. Passwords etc are intact. Let's change the name of the home directory. |
-s shell | Change the login shell to shell. |
-u uid | Change the ID number of the user to the specified one. |
-L | Lock the user's password. Locked accounts will not be able to log in with password authentication. |
-U | Unlock the user's password. It is the opposite of L. |
Notes
Using the -L option adds ! to the password entry in /etc/passwd or /etc/shadow.
You can lock even if you add ! Directly, and on the contrary if you delete! You can unlock it.
[root@localhost ~]# grep foo/etc/shadow foo:$1$AEgraoiuK+hOIwiOIhefaZT2cmtsb5KoS/:13616:0:99999:7::: [root@localhost ~]# vi /etc/shadow foo:!$1$AEgraoiuK+hOIwiOIhefaZT2cmtsb5KoS/:13616:0:99999:7::: ↑ Password authentication can not be performed.
Notes
About the case of setting a home directory by creating a new directory using the -d home_dir -m option.
For example, if you create a directory called /home/users/foo, you have to create it up to /home/users.
2. Usage example of "usermod" command
Change the user "foo" as follows.
- Change the default group to "bar_group"
- Make "foo" belong to subgroups "group1", "group2", "group3".
- Change the home directory to "/home/users/foo" (create a new directory).
Confirm current account information
[root@localhost ~]# id -a foo uid=514(foo) gid=514(foo) groups=514(foo) [root@localhost ~]# grep foo /etc/passwd foo :514:514::/home/foo:/bin/bash [root@localhost ~]# ls -d /home/users/foo ls: /home/users/foo: No such file or directoryExecute the command.
[root@localhost ~]# usermod -g bar_group -G group1,group2,group3 -d /home/users/foo -m foo [root@localhost ~]# id -a foo uid=514(foo) gid=518(bar_group) groups=518(bar_group),515(group1),516(group2),517(group3) [root@localhost ~]# grep foo /etc/passwd foo:x:514:518::/home/users/foo:/bin/bash