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

centos7用的是firewall 添加单个黑名单只需要把ip添加到 /etc/hosts.deny

格式  sshd:$IP:deny

vim /etc/hosts.deny   添加你要禁止的ip就可以了

sshd:192.168.1.147:deny


 

这是允许的 /etc/hosts.allow 
sshd:19.16.18.1:allow
sshd:19.16.18.2:allow

=========================

多次失败登录即封掉IP,防止暴力破解的脚本,超过20次的就加到黑名单

 

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 20 ];then
      grep $IP /etc/hosts.deny > /dev/null
    if [ $? -gt 0 ];then
      echo "sshd:$IP:deny" >> /etc/hosts.deny
    fi
  fi
done

 

2、创建记录登录失败次数的文件 

touch /usr/local/bin/black.txt

 

3、添加定时 10分钟执行一次   ( 定时 10分钟执行详见: https://www.cnblogs.com/emanlee/p/10293762.html )

 */10 * * * * root  sh /usr/local/bin/secure_ssh.sh

 

 REF

https://blog.csdn.net/z13615480737/article/details/83028304?utm_source=blogxgwz4

 

 


posted @ 2019-01-31 19:37  emanlee  阅读(13387)  评论(0编辑  收藏  举报