fudyou 发表于 2022-7-6 14:46:46

linux centos7添加ip黑名单禁止某个ip访问

centos7用的是firewall 添加单个黑名单只需要把ip添加到 /etc/hosts.deny
格式sshd:$IP:deny
vim /etc/hosts.deny   添加你要禁止的ip就可以了
sshd:192.168.1.147:denyhttps://img-blog.csdn.net/20181012155056793?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3oxMzYxNTQ4MDczNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70
这是允许的 /etc/hosts.allow
sshd:19.16.18.1:allow
sshd:19.16.18.2:allow

找了个,多次失败登录即封掉IP,防止暴力破解的脚本 亲测有效 超过5次的就加到黑名单

1、编辑脚本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 5 ];then
      grep $IP /etc/hosts.deny > /dev/null
    if [ $? -gt 0 ];then
      echo "sshd:$IP:deny" >> /etc/hosts.deny
    fi
fi
done2、创建记录登录失败次数的文件touch /usr/local/bin/black.txt

3、添加定时 5分钟执行一次   

   */5 * * * *sh /usr/local/bin/secure_ssh.sh

4、测试 ssh登录147    ssh 192.168.1.147
https://img-blog.csdn.net/20181012181831441?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3oxMzYxNTQ4MDczNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70
5、查看黑名单列表是否记录   cat /usr/local/bin/black.txt
https://img-blog.csdn.net/20181012180900580?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3oxMzYxNTQ4MDczNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70
6、查看黑名单列表看是否添加进去了 cat /etc/hosts.deny
https://img-blog.csdn.net/20181012181923162?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3oxMzYxNTQ4MDczNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70



页: [1]
查看完整版本: linux centos7添加ip黑名单禁止某个ip访问