修改用户信息
- 直接使用编辑器修改用户相关文件
- 使用 usermod 命令进行修改
1. 命令格式
usermod [选项] 用户名
[root@VM-0-7-centos ~]# usermod --help
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
the user 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
-R, --root CHROOT_DIR directory to chroot into
-P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files
-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
-v, --add-subuids FIRST-LAST add range of subordinate uids
-V, --del-subuids FIRST-LAST remove range of subordinate uids
-w, --add-subgids FIRST-LAST add range of subordinate gids
-W, --del-subgids FIRST-LAST remove range of subordinate gids
-Z, --selinux-user SEUSER new SELinux user mapping for the user account
[root@localhost ~]# usermod [选项] 用户名
选项:
-u UID: 修改用户的UID
-d 家目录: 修改用户的家目录。家目录必须写绝对路径
-c 用户说明: 修改用户的说明信息,就是/etc/passwd文件的第五个字段
-g 组名: 修改用户的初始组,就是/etc/passwd文件的第四个字段
-G 组名: 修改用户的附加组,其实就是把用户加入其他用户组
-s shell: 修改用户的登录Shell。默认是/bin/bash
-e 日期: 修改用户的失效日期,格式为“YYYY-MM-DD”。也就是/etc/shadow
文件的第八个字段
-L: 临时锁定用户(Lock)
-U: 解锁用户(Unlock)
usermod命令多出了几个选项,其中,-L可以临时锁定用户,不让这个用户登录。其实锁定的方法就是在/etc/shadow文件的密码字段前加入“!”。因为密码项是加密换算的,加入任何字符都会导致密码失效,所以这个用户就会被禁止登录。
而解锁(-U)就是把密码字段前的“!”取消。
[root@VM-0-7-centos ~]# usermod -L handsome
[root@VM-0-7-centos ~]# grep "handsome" /etc/shadow
handsome:!$1$3QkFgKy5$9PDoRTPrJjIyJviyIm2f6.:18858:0:99999:7:::
[root@VM-0-7-centos ~]# usermod -U handsome
[root@VM-0-7-centos ~]# grep "handsome" /etc/shadow
handsome:$1$3QkFgKy5$9PDoRTPrJjIyJviyIm2f6.:18858:0:99999:7:::
别的选项和 useradd 差不多,只不过一个加 、一个改而已
[root@VM-0-7-centos ~]# grep "beauty1" /etc/passwd
beauty1:x:550:1003:beautiful girl:/home/beauty1:/bin/bash
[root@VM-0-7-centos ~]# usermod -c "I love beautiful girl" beauty1
[root@VM-0-7-centos ~]# grep "beauty1" /etc/passwd
beauty1:x:550:1003:I love beautiful girl:/home/beauty1:/bin/bash