centos服务器fail2ban部署指南标准流程

安装

sudo 复制代码
sudo yum install fail2ban -y

Fail2Ban的默认配置文件在/etc/fail2ban/jail.conf,但不建议直接编辑这个文件。/etc/fail2ban/jail.local的优先级默认更高,无需指定路径。

sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo vim /etc/fail2ban/jail.local

[api-worktool]
enabled  = true
port     = http,https
filter   = nginx-errors
logpath  = /www/wwwlogs/api.worktool.ymdyes.cn.log
maxretry = 10

[site2]
enabled  = true
port     = http,https
filter   = nginx-errors
logpath  = /www/wwwlogs/site2.example.com.log
maxretry = 3

创建或编辑过滤器文件 /etc/fail2ban/filter.d/nginx-errors.conf,添4xx、5xx错误码

sudo vim /etc/fail2ban/filter.d/nginx-errors.conf

[Definition]
failregex = ^<HOST> -.* "(GET|POST|PUT|DELETE|HEAD).*HTTP/.*" (4\d{2}|5\d{2}) 

重启fail2ban生效

sudo systemctl enable fail2ban
sudo systemctl restart fail2ban

检查和测试

sudo fail2ban-client status
sudo fail2ban-client status api-worktool
sudo fail2ban-client status site2
sudo fail2ban-client -d

Fail2Ban会使用iptables来禁止IP地址。确保你的防火墙允许Fail2Ban添加规则。(缺省)

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
相关推荐
likfishdn6 分钟前
Linux的文件与目录管理
linux·运维·服务器
chian-ocean25 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=27 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
吃汤圆的抹香鲸1 小时前
GoLand 安装包 绿色版 Win,Mac,Linux 包含IntelliJ全家桶 专为Go语言设计的集成开发环境(IDE)
linux·windows·macos·go·intellij-idea·go1.19
追寻光1 小时前
Cannot find a valid baseurl for repo: base/7/x86_64
linux
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
陆沙1 小时前
deepseek-r1-centos-本地服务器配置方法
linux·服务器·centos
吃汤圆的抹香鲸2 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net
sz66cm2 小时前
Linux基础 -- ARM 32 位架构动态注入代码技术文档
linux·arm开发·架构
Mr.pyZhang4 小时前
Android构建系统 - 04 编译产物
android·linux