1.修改登录失败验证次数为3 vi /etc/ssh/sshd_config 将#MaxAuthTries 6 改为 MaxAuthTries 3

2.将#Port 22 改为 Port 22 Port 12345(你想改的端口号) 测试通过后将Port 22删掉

3.重启SSH服务 systemctl restart sshd.service

4.先把始终允许的IP填入 /etc/hosts.allow ,重点! 例如: vi /etc/hosts.allow sshd:123.123.123.123:allow sshd:8.8.8.8:allow

5.三次后直接封IP脚本 [root@localhost ~]# vi /usr/local/bin/secure_ssh.sh

#!/bin/bash
cat /var/log/secure | awk '/Failed/{print $(NF-3)}' | sort | uniq -c |awk '{print $2"="$1}' > /tmp/blacklist
 
MAXCOUNT="3"
 
for i in `cat /tmp/blacklist`
do
    IP=`echo $i | awk -F= '{print $1}'`
    NUM=`echo $i | awk -F= '{print $2}'`
 
if [ $NUM -gt $MAXCOUNT ];then
 
   grep $IP /etc/hosts.deny > /dev/null
   if [ $? -gt 0 ];then
	echo "sshd:$IP" >> /etc/hosts.deny
   fi
fi 
done

6.将secure_ssh.sh脚本放入cron计划任务,每天5:20执行一次。 [root@localhost ~]# crontab -e 20 05 * * * sh /usr/local/bin/secure_ssh.sh

查看IP黑名单 cat /etc/hosts.deny head -50 /etc/hosts.deny

直接执行脚本 sh /usr/local/bin/secure_ssh.sh