linux验证密码错误账号锁定时连续身份验证失败的时间间隔和自动解锁参数作用

- linux系统管理

配置登录失败处理时,看官方文档描述的 fail_interval 和 unlock_time 参数的作用,对于连续身份验证失败的时间间隔和自动解锁的效果不是很明确,这里做实验测试下。


实验思路

计划创建了两个普通账号, me 作为基础账号,要 su 登录到 super 账号下,验证 super 账号的锁定情况。




创建测试账户

[root@imzcy ~]# useradd me
[root@imzcy ~]# useradd super
[root@imzcy ~]# echo 123123 |passwd --stdin super
Changing password for user super.
passwd: all authentication tokens updated successfully.
[root@imzcy ~]#




配置登录失败处理

如下所示,为了能快速测出效果,所以我这里把fail_interval(连续身份验证失败的时间间隔)和 unlock_time(自动解锁时间)都设置为了 60 秒。

[root@imzcy ~]# authconfig --enablefaillock --faillockargs='deny=5 fail_interval=60 unlock_time=60 even_deny_root root_unlock_time=60' --update
[root@imzcy ~]#




验证 unlock_time 参数

故意连续输错5次密码

[me@imzcy ~]$ su - super
Password:
su: Authentication failure
[me@imzcy ~]$ su - super
Password:
su: Authentication failure
[me@imzcy ~]$ su - super
Password:
su: Authentication failure
[me@imzcy ~]$ su - super
Password:
su: Authentication failure
[me@imzcy ~]$ su - super
Password:
su: Authentication failure
[me@imzcy ~]$
[me@imzcy ~]$ date
Mon Dec 11 05:20:44 CST 2023
[me@imzcy ~]$

查看 super 用户的失败锁定情况,可以看到有 5 次标记为有效,账号应该已经被锁定了

[root@imzcy ~]# faillock --user super
super:
When                Type  Source                                           Valid
2023-12-11 05:20:14 TTY   pts/0                                                V
2023-12-11 05:20:19 TTY   pts/0                                                V
2023-12-11 05:20:24 TTY   pts/0                                                V
2023-12-11 05:20:29 TTY   pts/0                                                V
2023-12-11 05:20:35 TTY   pts/0                                                V
[root@imzcy ~]#

这时候我们哪怕输出正确的密码也不行了

[me@imzcy ~]$ su - super
Password:
su: Authentication failure
[me@imzcy ~]$ date
Mon Dec 11 05:21:17 CST 2023
[me@imzcy ~]$

等4分钟左右再次查看 super 用户锁定状态,好像还显示有效???

[root@imzcy ~]# date
Mon Dec 11 05:25:11 CST 2023
[root@imzcy ~]#
[root@imzcy ~]# faillock --user super
super:
When                Type  Source                                           Valid
2023-12-11 05:20:14 TTY   pts/0                                                V
2023-12-11 05:20:19 TTY   pts/0                                                V
2023-12-11 05:20:24 TTY   pts/0                                                V
2023-12-11 05:20:29 TTY   pts/0                                                V
2023-12-11 05:20:35 TTY   pts/0                                                V
[root@imzcy ~]#

再次尝试输错一次密码以后,再查看失败锁定状态,可以看到当前只有1次失败是有效的(V代表"Valid",表示该锁定事件是有效的),其他4个都失效状态(I代表"Invalid",表示该锁定事件是无效的),看来是上面锁定状态超时失效后不会自动更新,只有下次再失败更新值时才会同步更新掉。

[me@imzcy ~]$ su - super
Password:
su: Authentication failure
[me@imzcy ~]$
[root@imzcy ~]# faillock --user super
super:
When                Type  Source                                           Valid
2023-12-11 05:26:41 TTY   pts/0                                                V
2023-12-11 05:20:19 TTY   pts/0                                                I
2023-12-11 05:20:24 TTY   pts/0                                                I
2023-12-11 05:20:29 TTY   pts/0                                                I
2023-12-11 05:20:35 TTY   pts/0                                                I
[root@imzcy ~]#

因为只有一次登录失败记录,所以账号此时是没有被锁定的,我们输入正确密码就能切换到 super 用户下了。 说明确实锁定时间后确实会自动解锁用户。

[me@imzcy ~]$ su - super
Password:
Last failed login: Mon Dec 11 05:26:43 CST 2023 on pts/0
There were 7 failed login attempts since the last successful login.
[super@imzcy ~]$




验证 fail_interval 参数

连续输错4次密码(因为设置连续输错5次才会锁定,此时4次不会锁定账号)

[me@imzcy ~]$ date
Mon Dec 11 05:56:43 CST 2023
[me@imzcy ~]$ su - super
Password:
su: Authentication failure
[me@imzcy ~]$ su - super
Password:
su: Authentication failure
[me@imzcy ~]$ su - super
Password:
su: Authentication failure
[me@imzcy ~]$ su - super
Password:
su: Authentication failure
[me@imzcy ~]$
[me@imzcy ~]$ date
Mon Dec 11 05:57:06 CST 2023
[me@imzcy ~]$

查看 super 用户失败锁定状态,可以看到当前有4次失败登录被标记位有效。

[root@imzcy ~]# faillock --user super
super:
When                Type  Source                                           Valid
2023-12-11 05:56:46 TTY   pts/0                                                V
2023-12-11 05:56:51 TTY   pts/0                                                V
2023-12-11 05:56:57 TTY   pts/0                                                V
2023-12-11 05:57:02 TTY   pts/0                                                V
[root@imzcy ~]#
[root@imzcy ~]# date
Mon Dec 11 05:57:16 CST 2023
[root@imzcy ~]#

等2分钟,再查看 super 用户的失败锁定情况(还显示4次有效的失败登录),看样子估计还是状态没刷新。

[root@imzcy ~]# date
Mon Dec 11 05:59:19 CST 2023
[root@imzcy ~]#
[root@imzcy ~]# faillock --user super
super:
When                Type  Source                                           Valid
2023-12-11 05:56:46 TTY   pts/0                                                V
2023-12-11 05:56:51 TTY   pts/0                                                V
2023-12-11 05:56:57 TTY   pts/0                                                V
2023-12-11 05:57:02 TTY   pts/0                                                V
[root@imzcy ~]#

我们再尝试su切换用户输错一次密码(刷新下faillock状态),可以看到只有最近一次失败标记有效了,另外三次老的已经显示失效了。说明确实只有再时间间隔内连续错误才会锁定,超过时间间隔,老的失败登录会失效。

[me@imzcy ~]$ su - super
Password:
su: Authentication failure
[me@imzcy ~]$
[root@imzcy ~]# faillock --user super
super:
When                Type  Source                                           Valid
2023-12-11 06:00:32 TTY   pts/0                                                V
2023-12-11 05:56:51 TTY   pts/0                                                I
2023-12-11 05:56:57 TTY   pts/0                                                I
2023-12-11 05:57:02 TTY   pts/0                                                I
[root@imzcy ~]#