🔧 ​​一、安装 Fail2ban​

  1. ​启用 EPEL 仓库​
    yum install epel-release -y
  2. ​安装 Fail2ban​
    yum install fail2ban -y
  3. ​启动并设置开机自启​
    systemctl start fail2ban
    systemctl enable fail2ban

⚠️ 注意:CentOS 7.9 默认 Python 版本为 2.7,Fail2ban 0.11+ 兼容此版本

5


⚙️ ​​二、配置核心防护规则​

​1. 创建自定义配置文件​

避免覆盖默认配置,创建 jail.local

cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
vim /etc/fail2ban/jail.local
​2. 配置 SSH 防护(重点)​

在 jail.local 中添加以下内容

[sshd] 

enabled = true
port = ssh
filter = sshd
logpath = /var/log/secure # CentOS 认证日志路径
maxretry = 5 # 5 次失败尝试后封禁 
findtime = 600 # 10 分钟内触发规则 
bantime = 86400 # 封禁 24 小时(单位:秒) 
ignoreip = 127.0.0.1/8 192.168.1.0/24 # 信任 IP 白名单 
​3. 可选:Nginx 防护​

针对 Web 暴力破解或扫描

[nginx-http-auth]
enabled = true 
filter = nginx-http-auth
logpath = /var/log/nginx/error.log 
maxretry = 3

🔒 ​​三、服务管理与监控​

​命令​ ​作用​
systemctl restart fail2ban 重启服务使配置生效
fail2ban-client status sshd 查看 SSH 防护状态及被封禁 IP
fail2ban-client set sshd unbanip <IP> 手动解封指定 IP

1

3

tail -f /var/log/fail2ban.log 实时监控封禁日志

9

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐