一、编写脚本/root/bin/checkip.sh,每5分钟检查一次,如果发现通过ssh登录失败 次数超过10次,自动将此远程IP放入Tcp Wrapper的黑名单中予以禁止防问。
1、编写脚本;
[root@localhost bin]# cat checkip.sh
#!/bin/bash
n=10
cat /var/log/secure |grep sshd|awk '/Failed/{print $(NF-3)}'|sort |uniq -c |sort >>ssherro.log
while read count ip;do
if [ ${count} > ${n} ];then
echo "sshd is from:${ip}" >>/etc/hosts.deny
fi
done < ssherro.log
附上获取ssh登录失败次数统计步骤(用其它机器模拟ssh失败登录):
[root@localhost bin]# cat /var/log/secure | grep sshd | awk '/Failed/ {print $(NF-3)}'
192.168.1.111
192.168.1.111
192.168.1.111
[root@localhost bin]# cat /var/log/secure | grep sshd | awk '/Failed/ {print $(NF-3)}'
192.168.1.111
192.168.1.111
192.168.1.111
192.168.1.112
192.168.1.112
192.168.1.112
192.168.1.111
192.168.1.111
192.168.1.111
[root@localhost bin]# cat /var/log/secure | grep sshd | awk '/Failed/ {print $(NF-3)}' | sort | uniq -c | sort -r
6 192.168.1.111
3 192.168.1.112
2、写入定时任务;
[root@localhost bin]# crontab -l
*/5 * * * * root sh /root/bin/checkip.sh &>/dev/null
二、配置magedu用户的sudo权限,允许magedu用户拥有root权限。
[root@localhost ~]# visudo
## Allow root to run any commands anywhere
root ALL=(ALL) ALL ##在此处下面添加
magedu ALL=(ALL) ALL