vim /usr/local/bin/secure_ssh.sh
#! /bin/bash
cat /var/log/secure|awk ‘/Failed/{print $(NF-3)}’|sort|uniq -c|awk ‘{print $2"="$1;}’ > /usr/local/bin/black.txt
for i in cat /usr/local/bin/black.txt
do
IP=echo $i |awk -F= '{print $1}'
NUM=echo $i|awk -F= '{print $2}'
if [ $NUM -gt 2 ];then 失败3次会封IP
grep $IP /etc/hosts.deny > /dev/null
if [
?
−
g
t
0
]
;
t
h
e
n
e
c
h
o
"
s
s
h
d
:
? -gt 0 ];then echo "sshd:
?−gt0];thenecho"sshd:IP:deny" >> /etc/hosts.deny
fi
fi
done
touch /usr/local/bin/black.txt 创建记录登录失败次数的文件
vi /etc/crontab 添加定时任务:
*/1 * * * * root sh /usr/local/bin/secure_ssh.sh 1分钟执行一次
解锁
/var/log/secure (删除相关IP)
/etc/hosts.deny (删除相关IP)
ssh 密码错误4次锁此用户含root(普通用户锁86400/秒 root/10800秒)
[root@localhost ~]# cat /etc/pam.d/sshd
#%PAM-1.0
auth required pam_sepermit.so
auth substack password-auth
auth include postlogin
auth required pam_tally2.so deny=4 unlock_time=86400 even_deny_root root_unlock_time=10800
普通用户封86400秒 root用户108000秒
查看某一用户错误登陆次数
pam_tally2 --user superman
解锁
pam_tally2 --user superman --reset