禁用用户帐户的最简单方法是修改/etc/shadow文件,该文件负责为列出/etc/passwd的用户保存加密密码。
以下是/etc/shadow文件中的记录示例:
tester:$6dKR$Yku3LWgJmomsynpcle9BCA:15711:0:99999:7:::
要禁用上述帐户,只需在加密密码前添加“*”或者“!”:
tester:!$6dKR$Yku3LWgJmomsynpcle9BCA:15711:0:99999:7:::
或者直接使用命令:
# usermod -L tester
现在不允许用户登录:
$su tester Password: su: Authentication failure
要启用用户帐户,只需从/etc/shadow文件删除“!”或者使用usermod命令:
# usermod -U tester
注意,这种方法只适用于通过/etc/shadow文件作为身份验证的情况下。
其他情况下用户仍然可以登录,比如用户使用ssh密钥登录。
将用户的shell设置为nologin
禁止用于登录的另一种方法是使用 /usr/sbin/nologin 伪shell。
nologin将显示一条礼貌信息:
This account is currently not available.
修改/etc/password文件,
将shell
tester:x:1001:1001:Tester,User,,:/home/tester:/bin/bash
改成
tester:x:1001:1001:Tester,User,,:/home/tester:/usr/sbin/nologin
完成后,即使用户有正确的密码,也无法登录:
$su tester Password: This account is currently not available.
日期:2020-07-07 20:56:16 来源:oir作者:oir