usermod命令
usermod:修改用户的属性信息。
语法格式:
usermod [options] LOGIN
选项:
-a:添加用户到辅助组中。和-G选项一起使用,将用户添加到组中。
示例1:
[[email protected] ~]# usermod -aG user2 user1 [[email protected] ~]# grep ‘user1‘ /etc/group user1:x:503: user2:x:504:user1 [[email protected] ~]# 作用:将user1添加到user2组中,将user2作为user1的附属组。
-c:修改用户的comment信息。
示例2:
[[email protected] ~]# grep user1 /etc/passwd user1:x:503:503::/home/user1:/bin/bash [[email protected] ~]# usermod -c "I am user1" user1 [[email protected] ~]# grep user1 /etc/passwd user1:x:503:503:I am user1:/home/user1:/bin/bash [[email protected] ~]#
-d:修改用户的家目录。通常和-m选项一起使用,作用是将用户家目录下的内容移动到新的目录下,如果新的家目录不存在的话,则创建。
示例3:
[[email protected] ~]# grep user1 /etc/passwd user1:x:503:503:I am user1:/home/user1:/bin/bash [[email protected] ~]# mkdir /user1 [[email protected] ~]# usermod -d /user1 user1 [[email protected] ~]# grep user1 /etc/passwd user1:x:503:503:I am user1:/user1:/bin/bash [[email protected] ~]#
示例4:
[[email protected] ~]# grep user1 /etc/passwd user1:x:503:503:I am user1:/home/user1:/bin/bash [[email protected] ~]# usermod -d /user1 -m user1 [[email protected] ~]# grep user1 /etc/passwd user1:x:503:503:I am user1:/user1:/bin/bash [[email protected] ~]# su - user1 [[email protected] ~]$
-e:修改账户过期时间,格式为YYYY-MM-DD。
示例5:
[[email protected] ~]# chage -l user1 Last password change : Jul 01, 2014 Password expires : never Password inactive : never Account expires : Jul 02, 2014 Minimum number of days between password change : 0 Maximum number of days between password change : 99999 Number of days of warning before password expires : 7 [[email protected] ~]# usermod -e 2014-8-8 user1 [[email protected] ~]# chage -l user1 Last password change : Jul 01, 2014 Password expires : never Password inactive : never Account expires : Aug 08, 2014 Minimum number of days between password change : 0 Maximum number of days between password change : 99999 Number of days of warning before password expires : 7 [[email protected] ~]#
-f:密码过期多少天后账户将停用。0将立即停用,-1将关闭该特性。
示例6:
[[email protected] ~]# grep user1 /etc/shadow user1:!!:16252:0:99999:7:5:16290: [[email protected] ~]# 说明:user1为密码过期5天后,账户将停用。 将其修改为10: [[email protected] ~]# usermod -f 10 user1 [[email protected] ~]# grep user1 /etc/shadow user1:!!:16252:0:99999:7:10:16290: [[email protected] ~]#
-g:修改用户的基本组或初始登录组。
示例7:
[[email protected] ~]# grep user1 /etc/passwd user1:x:503:503:I am user1:/home/user1:/bin/bash [[email protected] ~]# id -gn user2 user2 [[email protected] ~]# id -g user1 503 [[email protected] ~]# id -g user2 504 [[email protected] ~]# usermod -g user2 user1 [[email protected] ~]# grep user1 /etc/passwd user1:x:503:504:I am user1:/home/user1:/bin/bash [[email protected] ~]#
-G:修改用户的附属组。
-l:修改用户的login name。
示例8:
[[email protected] ~]# grep user1 /etc/passwd user1:x:503:504:I am user1:/home/user1:/bin/bash [[email protected] ~]# usermod -l user11 user1 [[email protected] ~]# grep user11 /etc/passwd user11:x:503:504:I am user1:/home/user1:/bin/bash [[email protected] ~]#
-L:锁定用户,在密码前加上!
示例9:
[[email protected] ~]# usermod -L user1 [[email protected] ~]# su - user2 [[email protected] ~]$ su - user1 Password: su: incorrect password [[email protected] ~]$ 说明:当锁定一个用户时,即使输入正确的密码,也会提示密码错误。
-U:解锁锁定的用户。
-u:修改用户的UID
示例10:
[[email protected] ~]# grep user1 /etc/passwd user1:x:503:504:I am user1:/home/user1:/bin/bash [[email protected] ~]# usermod -u 1000 user1 [[email protected] ~]# grep user1 /etc/passwd user1:x:1000:504:I am user1:/home/user1:/bin/bash [[email protected] ~]#
-s:修改用户的shell
示例11:
[[email protected] ~]# grep user1 /etc/passwd user1:x:1000:504:I am user1:/home/user1:/bin/bash [[email protected] ~]# usermod -s /sbin/nologin user1 [[email protected] ~]# grep user1 /etc/passwd user1:x:1000:504:I am user1:/home/user1:/sbin/nologin [[email protected] ~]#
还有一个专门修改用户shell的指令是chsh。
chsh的选项:
-l:列出在/etc/shells中的shell。
-s:改变用户的login shell
示例1:
[[email protected] ~]# chsh -l /bin/sh /bin/bash /sbin/nologin /bin/tcsh /bin/csh [[email protected] ~]#
示例2:
[[email protected] ~]# grep user1 /etc/passwd user1:x:1000:504:I am user1:/home/user1:/bin/bash [[email protected] ~]# chsh -s /sbin/nologin user1 Changing shell for user1. Shell changed. [[email protected] ~]# grep user1 /etc/passwd user1:x:1000:504:I am user1:/home/user1:/sbin/nologin [[email protected] ~]#
时间: 2024-10-02 21:21:21