当前位置 博文首页 > weixin_30367169的博客:Linux课程笔记 用户和用户组管理

    weixin_30367169的博客:Linux课程笔记 用户和用户组管理

    作者:[db:作者] 时间:2021-09-12 18:15

    1.用户管理的文件

    1.1 ?/etc/skel目录

    ?? /etc/skel目录是用来存放新用户配置文件的目录,当我们添加用户时,这个目录下的所有文件会自动改被复制到新添加的用户的家目录下。

    ??

    ?? 示例:在/etc/skel目录下创建一个文件,看看新创建的用户家目录下是否有这个文件

    [root@test2 skel]# ll -a /etc/skel/

    总计 56

    drwxr-xr-x? 2 root root? 4096 06-02 20:02 .

    drwxr-xr-x 92 root root 12288 06-03 06:11 ..

    -rw-r--r--? 1 root root??? 33 2011-07-22 .bash_logout

    -rw-r--r--? 1 root root?? 176 2011-07-22 .bash_profile

    -rw-r--r--? 1 root root?? 124 2011-07-22 .bashrc

    -rw-r--r--? 1 root root??? 14 06-02 20:02 qinbf.txt

    [root@test2 skel]# cat qinbf.txt

    this is qinbf

    [root@test2 skel]# useradd student

    [root@test2 skel]# ll -a /home/student/

    总计 48

    drwx------ 2 student student 4096 06-03 06:13 .

    drwxr-xr-x 8 root??? root??? 4096 06-03 06:13 ..

    -rw-r--r-- 1 student student?? 33 06-03 06:13 .bash_logout

    -rw-r--r-- 1 student student? 176 06-03 06:13 .bash_profile

    -rw-r--r-- 1 student student? 124 06-03 06:13 .bashrc

    -rw-r--r-- 1 student student?? 14 06-03 06:13 qinbf.txt

    [root@test2 skel]# cat /home/student/qinbf.txt

    this is qinbf

    ?

    1.2 /etc/login.defs文件

    /etc/login.defs文件是用来定义创建用户时需要的一些用户的配置信息,如创建用户时,是否需要家目录,UID和GID的范围等。

    ?

    示例:修改UID的范围,看看创建的用户UID值得范围

    23 # Min/max values for automatic uid selection in useradd

    24 #

    25 UID_MIN???????????????? 20000??? #--à只修改了UID_MIN,没有修改GID_MIN

    26 UID_MAX???????????????? 60000

    [root@test2 etc]# useradd ?teache

    [root@test2 etc]# tail -1 /etc/passwd

    teache:x:20000:20000::/home/teache:/bin/bash

    ?

    1.3/etc/default/useradd文件

    /etc/default/useradd文件是在使用useradd添加用户时,的一个需要调用的一个默认的配置文件,可以使用useradd? -D参数。

    ?

    [root@test2 etc]# cat /etc/default/useradd

    #useradd? default? file

    GROUP=100

    HOME=/home? #-à把用户的家目录建在/home中

    INACTIVE=-1 ??#--à是否启用账号过期停权,-1表示不启用

    EXPIRE=?????? #-à账号终止日期,不设置表示不启用

    SHELL=/bin/bash? #-à新用户默认所用的shell类型

    SKEL=/etc/skel??? #-à配置新用户家目录的默认文件存放路径,用户家目录下文件都是从这里复制过去的

    CREATE_MAIL_SPOOL=yes?? #--à创建mail文件

    ?

    示例:修改/etc/default/useradd文件的内容,新建一个用户,看看新用户的配置

    [root@test2 etc]# mkdir /etc/qinbf/test

    [root@test2 etc]# ll -a /etc/qinbf/

    总计 40

    drwxr-xr-x? 3 root root? 4096 06-03 06:33 .

    drwxr-xr-x 93 root root 12288 06-03 06:31 ..

    -rw-r--r--? 1 root root??? 14 06-02 20:02 qinbf.txt

    drwxr-xr-x? 2 root root? 4096 06-03 06:33 test

    [root@test2 etc]# sed -i 's#/etc/skel#/etc/qinbf#g' /etc/default/useradd

    [root@test2 etc]# cat /etc/default/useradd

    # useradd defaults file

    GROUP=100

    HOME=/home

    INACTIVE=-1

    EXPIRE=

    SHELL=/bin/bash

    SKEL=/etc/qinbf

    CREATE_MAIL_SPOOL=yes

    [root@test2 etc]# useradd student2

    [root@test2 etc]# ll -a /home/student2/

    总计 32

    drwx------? 3 student2 student2 4096 06-03 06:34 .

    drwxr-xr-x 10 root???? root???? 4096 06-03 06:34 ..

    -rw-r--r--? 1 student2 student2?? 14 06-03 06:34 qinbf.txt

    drwxr-xr-x? 2 student2 student2 4096 06-03 06:34 test

    ?

    2用户管理的命令

    2.1 useradd

    当使用useradd命令不加参数选项,后面直接跟所添加的用户名时,系统会首先读取配置文件/etc/login.defs和/etc/default/useradd中所定义的参数或规则,根据设置的规则添加用户,同时会向/etc/passwd和/etc/group文件内添加新建用户和用户组的记录,并同步/etc/shadows和/etc/gshadows,同时系统还会根据/etc/default/useradd文件中所配置的信息建立用户家目录,并复制/etc/skel中的所有文件(包括隐藏的系统环境配置文件)到新用户的家目录中。

    useradd语法(来自man? useradd)

    名 称

    ?????? useradd - 帐 号 建 立 或 更 新 新 使 用 者 的 资 讯

    ?

    语 法

    ?????? useradd [-c comment] [-d home_dir]

    ?????????????? [-e expire_date] [-f inactive_time]

    ?????????????? [-g initial_group] [-G group[,...]]

    ?????????????? [-m [-k skeleton_dir] | -M] [-s shell]

    ?????????????? [-u uid [ -o]] [-n] [-r] login

    ?

    ?????? useradd -D [-g default_group] [-b default_home]

    ?????????????? ??[-f default_inactive] [-e default_expire_date]

    ?????????????? ??????[-s default_shell]

    ?

    参数说明

    说 明

    -c comment

    新 帐 号 password 档 的 说 明 栏

    -d home_dir

    新 帐 号 每 次 登 入 时 所 使 用 的 home_dir

    -e expire_date

    帐 号 终 止 日 期 。 日 期 的 指 定 格 式 为 MM/DD/YY

    -f inactive_time

    帐 号 过 期 几 日 后 永 久 停 权 。当 值 为 0 时 帐 号 则 立 刻 被 停 权 。而 当 值 为 -1 时 则 关 闭 此 功 能 , 预 设 值 为 -1

    -g initial_group

    group 名 称 或 以 数 字 来 做 为 使 用 者 登 入 起 始 群 组 (group) 。群 组 名 须 为 现 有 存 在 的 名 称 。群 组 数 字 也 须 为 现 有存 在 的 群 组 。预 设 的 群 组 数 字 为 1 。

    -G group

    定 义 此 使 用 者 为 此 一 堆 groups 的 成 员 。每 个 群 组 使 用 "," 区 格 开 来 , 不 可 以 夹 杂 空 白 字 元 。 群 组 名 同 -g 选 项 的 限 制 。定 义 值 为 使用 者 的 起 始 群 组 。

    -m

    使 用 者 目 录 如 不 存 在 则 自 动 建 立 。如 使 用 -k 选 项 skeleton_dir 内 的 档 案 将 复 制 至 使 用 者 目录 下 。然 而 在 /etc/skel目?? 录 下 的 档 案 也 会 复 制 过 去 取 代 。任 何 在 skeleton_dir or /etc/skel 的 目 录 也 相 同 会 在 使 用 者目 录 下 一 一 建 立 。The -k 同 -m 不 建 立 目 录 以 及 不 复 制 任 何 档 案 为 预 设 值

    -M

    不 建 立 使 用 者 目 录 , 即 使 /etc/login.defs 系 统 档 设 定 要 建立 使 用 者 目 录 。

    -s shell

    使 用 者 登 入 后 使 用 的 shell 名 称 。预 设 为 不 填 写 , 这 样 系 统 会 帮 你 指 定 预 设 的 登 入 shell

    -u uid [ -o]

    使 用 者 的 ID 值 。 必 须 为 唯 一 的 ID 值 , 除 非 用 -o 选 项 。数 字 不 可 为 负 值。预 设 为 最 小 不 得 小 于 999 而 逐 次 增? 加。 0~ 999 传 统 上 是 保 留 给 系 统 帐 号 使 用 。

    -n

    预 设 值 使 用 者 群 组 与 使 用 者 名 称 会 相 同 。 此 选 项 将 取 消 此 预设 值

    -r

    此 参 数 是 用 来 建 立 系 统 帐 号。系 统 帐 号 的 UID 会 比 定 义 在 系统 档 上 /etc/login.defs.? 的 UID_MIN 来 的 小。注 意 useadd 此 用 法 所 建 立 的 帐 号 不 会建 立 使 用 者 目 录, 也 不 会 在 乎 纪 录 在 /etc/login.defs. 的 定 义 值 。如 果 你 想 要 有 使用 者目录 须 额 外 指 定 -m 参 数 来 建 立 系统 帐 号 。这 是 RED HAT 额 外 增 设 的 选 项 。

    ?

    ?

    useradd? -D参数选项

    说明

    -b default_home

    定义使用者所属目录的前一个目录使用者名称会附加在 default_home后面用来建立新使用的目录。当然使用-d后则此选项无效 。

    -e default_expire_date

    使用者帐号停止日期

    -f default_inactive

    帐号过期 日后停权

    -g default_group

    新帐号起始群组名或ID。群组名须为现有存在的名称。群组I D也须为现有存在的群组

    -s default_shell

    使用者登入后使用的shell名称。往后新加入的帐号都将使用此 shell

    ?

    useradd实例:账号过期时间实例

    [root@test2 ~]# date +%F??? #----à当前时间为6月3号

    2013-06-03

    [root@test2 ~]# useradd -e 06/04/13? student03? #----à6月4号到期

    root@test2 ~]# date -s 06/05/13??? #----à将时间设置为6月5号

    2013年 06月 05日 星期三 00:00:00 CST

    ?

    ?

    ?

    ?

    Last login: Mon Jun? 3 00:02:45 2013 from 192.168.1.1

    [student03@test2 ~]$??? #------à可以正常登陆

    ?

    [root@test2 ~]# date -s 06/06/13?? #------à将系统时间设置为6月6号

    2013年 06月 06日 星期四 00:00:00 CST

    ?

    ?

    ?

    Your account has expired; please contact your system administrator

    #-----------------à提示账号已经到期

    [root@test2 ~]# su - student03

    [student03@test2 ~]$ pwd??? #-------à系统还是可以切换到student03账户下

    /home/student03

    [tmpuser1@test2 ~]$ chage -l student03

    最近一次密码修改时间??????????????????????????????????? : 6月 03, 2013

    密码过期时间??????????????????????????????????? :从不

    密码失效时间??????????????????????????????????? :从不

    帐户过期时间??????????????????????????????????????????? : 6月 04, 2013

    两次改变密码之间相距的最小天数????????? :0

    两次改变密码之间相距的最大天数????????? :99999

    在密码过期之前警告的天数??????? :7

    ?

    通过-e设置无法远程连接,但是可以su切换,账户并未被锁定

    账户过期时间和系统时间,需要相差2天

    ?

    useradd实例:useradd? –c? -u? -G? -s? -d 多个参数组合例子

    添加用户oldboy6,并设置其用户注释信息为HandsomeBoy,UID指定为806,归属用户为用户组root、oldboy、sa成员,其shell类型为/bin/sh,设置家目录为/oldboy6

    ?

    root@test2 ~]# useradd -c HandsomeBoy -u 806 -G root,sa -s /bin/sh -d /oldboy6 oldboy6

    [root@test2 ~]# grep oldboy6 /etc/passwd

    • oldboy6:x:806:806:HandsomeBoy:/oldboy6:/bin/sh

    [root@test2 ~]# id oldboy6

    uid=806(oldboy6) gid=806(oldboy6) groups=806(oldboy6),0(root),801(sa) context=user_u:system_r:unconfined_t:s0

    #------à在添加用户时,如果不使用-n参数,系统会自动创建一个与用户名同名的用户组

    ?

    useradd? -D的用法

    [root@test2 ~]# useradd -D

    GROUP=100

    HOME=/home

    INACTIVE=-1

    EXPIRE=

    SHELL=/bin/bash

    SKEL=/etc/skel

    CREATE_MAIL_SPOOL=yes

    [root@test2 ~]# useradd -D -e "06/12/13" -b "/home/test" -s /bin/sh

    [root@test2 ~]# useradd -D

    GROUP=100

    HOME=/home/test

    INACTIVE=-1

    EXPIRE=06/12/13

    SHELL=/bin/sh

    SKEL=/etc/skel

    CREATE_MAIL_SPOOL=yes

    #---à实验之后记得恢复之前的默认设置

    ?

    2.2 添加用户组命令groupadd

    与groupadd命令有关的文件有

    /etc/group-用户组相关文件

    /etc/gshadow-用户组加密相关文件

    ?

    groupadd命令语法

    语法

    ?????? groupadd [-g gid [-o]] [-r] [-f] group

    ?

    groupadd参数说明

    groupadd参数选项

    注释说明

    -g gid

    指定用户组GID。除非接-o参数,否则ID值必须是唯一的数字(不能为负数)。如果不指定-g参数,则预备值会从500开始。

    -r

    建立系统用户组。GID会比/etc/login.defs中定义的UID_MIN值小

    -f

    新增一个账户,强制覆盖一个已经存在的用户组账号

    ?

    groupadd实例

    [root@test2 ~]# groupadd -g 10086 2ma

    [root@test2 ~]# grep 2ma /etc/group

    2ma:x:10086:

    ?

    3 用户密码相关命令

    3.1 ?passwd命令介绍

    普通用户和超级用户都可以运行passwd命令,但普通用户只能更改自身的用户密码,超级用户root则可以设置或修改所有用户的密码,

    ?

    当直接执行passwd命令后面不接任何参数或用户名时,则表示修改当前登录用户的密码。

    [root@test2 ~]# passwd

    Changing password for user root.

    New UNIX password:

    [root@test2 ~]# passwd qinbf

    Changing password for user qinbf.

    New UNIX password:

    [root@test2 ~]# passwd root

    Changing password for user root.

    New UNIX password:

    ?

    passwd命令参数选项

    ?

    命令参数

    说明

    -k, --keep-tokens

    保留即将过期的用户在期满后仍能使用

    -d,--delete

    删除用户密码,仅能以root权限操作

    -l,--lock

    锁住用户无权更改其密码,仅能通过root权限操作

    -u,--unlock

    解除锁定

    -f,--force

    强制操作,仅root权限操作

    -x,--maximum=DAYS

    两次密码修改的最大天数,后面接数字;仅能root权限操作

    -n,--minimin=DAYS

    两次密码修改的最小天数,后面接数字,仅能root权限操作

    -w,--waring=DAYS

    在距多少天提醒用户修改密码;仅能root权限操作

    -I,--inactive=DAYS

    在密码过期多少天后,用户被禁掉,仅能以root操作

    -S,--status

    查询用户的密码状态,仅能root用户操作

    --stdin

    从stdin读入密码

    ?

    passwd命令实例

    实例1:用-l参数来锁定oldboy用户,使之不能修改密码,然后再用-u来解除锁定。

    [root@test2 ~]# passwd -l oldboy

    Locking password for user oldboy.

    passwd: Success

    [root@test2 ~]# passwd -S oldboy

    • oldboy LK 2013-06-15 0 99999 7 -1 (Password locked.)

    ?

    实例2:我们用-d参数清除test用户密码,然后通过-S参数查看

    [root@test2 ~]# passwd -d oldboy

    Removing password for user oldboy.

    passwd: Success

    [root@test2 ~]# passwd -S oldboy

    • oldboy NP 2013-06-15 0 99999 7 -1 (Empty password.)

    疑问:用户密码为空,那么是否通过远程ssh链接就不要密码了呢?

    解答:空密码登陆不了。

    ?

    实例3:举一个组合参数-x -w -i控制密码时效的例子

    要求odlboy用户7天内不能更改密码,60天以后必须修改密码。过期前10天通知odlboy用户,过期30天后禁止用户登陆。

    [root@test2 ~]# passwd -n 7 -x 60 -w 10 -i 30 test

    Adjusting aging data for user test.

    #--àchage? -m? 7? -M? 60? -W 10? -I? 30? odlboy命令也可以实现同样功能,只是参数的写法不同

    ?

    实例4:用--stdin参数实现非交互的批量设置或修改密码

    #-à记得使用history? -c清除历史记录

    [root@test2 ~]# vi us.sh

    ?

    1 #!/bin/bash

    2 userchars="oldstudent"? #用户字符串,就是用户的前面部分

    3 for num in `seq 3`? #seq 3 表示1-3

    4

    5 do?????

    6???????? useradd $userchars$num? #创建用户的命令

    7???????? passwd=`date|md5sum|cut -c3-20` #按照时间md5加密取3-20之间的字符作为用户的密码。此处要注意,密码部分应该要事先定义好变量,否则取值会变化,导致密码生成错误

    8???????? echo "$passwd" |passwd --stdin $userchars$num? #这行就是利用--stdin无交互设置密码的参数

    9???????? echo -e "user:$userchars$num\tpasswd:$passwd">>/tmp/user.log #输出创建的用户和密码到user.log文件

    10????????

    11???????? sleep 1

    12 done

    13 echo --------------------------

    14 cat /tmp/user.log

    ?

    [root@test2 ~]# sh us.sh

    Changing password for user oldstudent1.

    passwd: all authentication tokens updated successfully.

    Changing password for user oldstudent2.

    passwd: all authentication tokens updated successfully.

    Changing password for user oldstudent3.

    passwd: all authentication tokens updated successfully.

    --------------------------

    user:oldboy1??? passwd:7246382a2ba141f2db

    user:oldboy2??? passwd:7aeea9dd5537c975dd

    user:oldboy3??? passwd:4ee64c9a8e85bba809

    user:oldstudent1??????? passwd:9bc57124663721831d

    user:oldstudent2??????? passwd:57934e619ad637f218

    user:oldstudent3??????? passwd:5a54c4db74389a02dc

    ?

    总结:在实际工作中,最常用的用法就是直接使用passwd加用户名设置和修改密码,其次是加--stdin参数批量无交互设置密码,其他参数用的很少(包括-l,-u,-S等)。

    ?

    3.2 修改用户密码有效期相关命令chage

    chage说明

    chage和passwd等命令功能有不少是重复的,用来修改用户密码。

    ?

    chage语法

    用法:chage? [选项] 用户名

    chage参数选项

    注释说明

    -d,--lastday 最近日期

    将最近一次密码设置时间设为“最近日期”

    -E,--expiredate 过期日期

    将账户过期时间设为“过期时间”,日期格式为mm/dd/YY

    -h,--help

    显示帮助信息,并退出

    -I,--inactive 失效密码

    将因过期而失效的密码设为“失效密码”

    -l.--list

    显示账户年龄信息

    -m,--mindays 最小天数

    将两次改变密码之间相距最小天数设为“最小天数”

    -M,--maxdays 最大天数

    将两次改变密码之间相距最大天数设为“最大天数”

    -W,--warndays 警告天数

    将过期警告天数设为“警告天数”

    ?

    chage实例

    实例1:使用-l参数显示账户年龄信息

    [root@test2 ~]# chage -l qinbf

    最近一次密码修改时间??????????????????????????????????? : 5月 21, 2013

    密码过期时间??????????????????????????????????? :从不

    密码失效时间??????????????????????????????????? :从不

    帐户过期时间??????????????????????????????????????????? :从不

    两次改变密码之间相距的最小天数????????? :0

    两次改变密码之间相距的最大天数????????? :99999

    在密码过期之前警告的天数??????? :7

    ?

    实例2:举一个组合参数-m、-M、-W、-I控制密码时效的例子(同passwd命令的实例3)

    [root@test2 ~]# chage -l oldboy1

    最近一次密码修改时间??????????????????????????????????? : 6月 03, 2013

    密码过期时间?????????????????????????????????? ?:从不

    密码失效时间??????????????????????????????????? :从不

    帐户过期时间??????????????????????????????????????????? :从不

    两次改变密码之间相距的最小天数????????? :0

    两次改变密码之间相距的最大天数????????? :99999

    在密码过期之前警告的天数??????? :7

    [root@test2 ~]# chage -m 7 -M 60 -W 10 -I 30 oldboy1

    [root@test2 ~]# chage -l oldboy1

    最近一次密码修改时间??????????????????????????????????? : 6月 03, 2013

    密码过期时间??????????????????????????????????? : 8月 02, 2013

    密码失效时间??????????????????????????????????? : 9月 01, 2013

    帐户过期时间??????????????????????????????????????????? :从不

    两次改变密码之间相距的最小天数????????? :7

    两次改变密码之间相距的最大天数????????? :60

    在密码过期之前警告的天数??????? :10

    ?

    实例3:使用-E参数修改账户有效期

    #也可以使用usermod? -e 06/10/13来实现

    [root@test2 ~]# chage -E 06/13/13 oldboy1

    [root@test2 ~]# chage -l oldboy1

    ……

    帐户过期时间?????????????????????????????????????? : 6月 13, 2013

    ……

    [root@test2 ~]# usermod -e "" oldboy1

    [root@test2 ~]# chage -l oldboy1

    ……

    帐户过期时间??????????????????????????????????????????? :从不

    ……

    ?

    有关用户密码失效

    设置用户密码时效有利有弊,可以防止离职人员过期登录,也可以强制定期修改密码,提升系统安全性,缺点是有一些本应该保留的用户过期无法登录,再者服务器数量很大的情况下,修改密码是很大的工作量。在大规模的运维环境中,使用LDAP服务实行对linux账户统一认证,批量修改,是不错的方法。

    ?

    3.3 删除用户相关命令userdel

    userdel语法

    userdel? [-r] 用户名

    ?

    总结

    使用“userdel 用户名”删除用户时,仅会删除用户本身,其家目录及相关文件并不会被删除。

    ?

    我们可以直接使用vi编辑/etc/passwd,找到要处理的用户吗,先注释一段时间,确认真的没问题,然后再清理其家目录,注释的作用和userdel命令删除的效果是一样的。注释取消注释之后,还可以恢复。对/etc/passwd这样重要的文件进行修改,最好先备份。

    ?

    实例

    [root@test2 ~]# sed -i "s/tmpuser1/\#tmpuser1/g"? /etc/passwd

    [root@test2 ~]# grep \# /etc/passwd

    #tmpuser1:x:20003:20003::/home/#tmpuser1:/bin/bash

    [root@test2 ~]# ll -d /home/tmpuser1/

    drwx------ 2 20003 tmpuser1 4096 06-03 07:08 /home/tmpuser1/

    [root@test2 ~]# su - tmpuser1

    su: 用户 tmpuser1 不存在

    ?

    [root@test2 ~]# userdel tmpuser1?? #-à没有使用参数-r

    [root@test2 ~]# ll -d /home/tmpuser1?? #-->家目录还在

    drwx------ 2 20003 20003 4096 06-03 07:08 /home/tmpuser1

    [root@test2 ~]# cd /var/spool/mail/

    [root@test2 mail]# ll |grep tmpuser1 ?#--à邮件队列还存在

    -rw-rw---- 1?????? 20003 mail???? 0 06-03 07:07 tmpuser1

    ?

    [root@test2 ~]# tail -3 /etc/passwd

    • oldstudent1:x:20007:20007::/home/oldstudent1:/bin/bash
    • oldstudent2:x:20008:20008::/home/oldstudent2:/bin/bash
    • oldstudent3:x:20009:20009::/home/oldstudent3:/bin/bash

    [root@test2 ~]# userdel -r oldboy3?? #-à使用-r参数

    [root@test2 ~]# ll -d /home/oldboy3? #-à家目录不存在

    ls: /home/oldboy3: 没有那个文件或目录

    [root@test2 ~]# ll /var/spool/mail/ |grep oldboy3? #--à邮件队列不存在

    ?

    3.4 删除用户组相关命令groupdel

    语法格式

    groupdel? 用户组

    [root@test2 ~]# cat /etc/group

    …..

    • oldboy2:x:20002:

    ……

    [root@test2 ~]# userdel oldboy2

    [root@test2 ~]# ll -d /home/oldboy2

    ls: /home/oldboy2: 没有那个文件或目录

    [root@test2 ~]# grep oldboy2 /etc/group

    [root@test2 ~]# grep oldboy2 /etc/passwd

    ?

    3.5 用户信息修改相关命令usermod

    usermod语法

    ?

    ?

    usermod参数选项

    注释说明

    -c comment

    增加用户账户/etc/passwd中的注解说明

    -d home_dir

    更新家目录,如果给定-m选项,用户旧的家目录会搬到新的家目录中去

    -e expire_date

    加上用户账号停止日期,格式为mm/dd/yy

    -f inactive_days

    账户过期几日后永久停权,当值为-1时,则关闭此功能

    -g install_group

    更新用户新的起始登入用户组,用户组名须已存在

    -G group […]

    定义用户为一堆groups成员

    -l login_name

    变更用户login时的名称为login_name。其余信息不变

    -s shell

    指定新登入shell

    -u uid

    指定用户的UID,除非接-o参数,否则ID值必须唯一

    -L

    冻结用户的密码,使之无法登陆,实际就是间接修改/etc/passwd的密码栏,在密码栏的开头加上“!”

    -U

    取消冻结用户的密码,使之恢复登陆,取消/etc/passwd中的“!”。

    ?

    实例:

    [root@test2 ~]# grep oldstudent3 /etc/shadow

    • oldstudent3:$1$Nh65QzTp$IX.AFlh.F/9EAZ1KAnsw40:15859:0:99999:7:::

    [root@test2 ~]# usermod -L oldstudent3

    [root@test2 ~]# grep oldstudent3 /etc/shadow

    • oldstudent3:!$1$Nh65QzTp$IX.AFlh.F/9EAZ1KAnsw40:15859:0:99999:7:::

    [root@test2 ~]# usermod -U oldstudent3

    [root@test2 ~]# grep oldstudent3 /etc/shadow

    • oldstudent3:$1$Nh65QzTp$IX.AFlh.F/9EAZ1KAnsw40:15859:0:99999:7:::

    ?

    [root@test2 ~]# usermod -L oldstudent3

    [root@test2 ~]# passwd -S oldstudent3

    • oldstudent3 LK 2013-06-05 0 99999 7 -1 (Password locked.)

    [root@test2 ~]# grep oldstudent3 /etc/shadow????

    • oldstudent3:!$1$/pAJLrtB$2WBRM7PiWkJVxqipIgXPd0:15861:0:99999:7:::

    [root@test2 ~]# passwd -u oldstudent3

    Unlocking password for user oldstudent3.

    passwd: Success.

    [root@test2 ~]# grep oldstudent3 /etc/shadow

    • oldstudent3:$1$/pAJLrtB$2WBRM7PiWkJVxqipIgXPd0:15861:0:99999:7:::

    ?

    [root@test2 ~]# passwd -S oldstudent3??????

    • oldstudent3 PS 2013-06-05 0 99999 7 -1 (Password set, MD5 crypt.)

    [root@test2 ~]# passwd -l oldstudent3?? #使用passwd ?-l锁定

    Locking password for user oldstudent3.

    passwd: Success

    [root@test2 ~]# passwd -S oldstudent3

    • oldstudent3 LK 2013-06-05 0 99999 7 -1 (Password locked.)

    [root@test2 ~]# usermod -U oldstudent3? #使用usermod? -U解除锁定

    [root@test2 ~]# passwd -S oldstudent3

    • oldstudent3 LK 2013-06-05 0 99999 7 -1 (Password locked.) #还是锁定状态

    [root@test2 ~]# usermod -U oldstudent3? #再次使用usermod? -U解除锁定

    [root@test2 ~]# passwd -S oldstudent3 ???#成功解除

    • oldstudent3 PS 2013-06-05 0 99999 7 -1 (Password set, MD5 crypt.)

    从上面可以发现,使用usermod? -U参数锁定,用passwd -l来解除锁定,只需要输入一次命令,而使用passwd? -l参数锁定,用usermod? -U来解除锁定,需要输入两次命令。以下可以通过分别使用两个命令,/etc/shadow文件的变化来验证。

    下一篇:没有了