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
相关推荐
轻松Ai享生活18 小时前
5 节课深入学习Linux Cgroups
linux
christine-rr18 小时前
linux常用命令(4)——压缩命令
linux·服务器·redis
三坛海会大神55519 小时前
LVS与Keepalived详解(二)LVS负载均衡实现实操
linux·负载均衡·lvs
東雪蓮☆19 小时前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
乌萨奇也要立志学C++19 小时前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器
獭.獭.21 小时前
Linux -- 信号【上】
linux·运维·服务器
hashiqimiya21 小时前
centos配置环境变量jdk
linux·运维·centos
百锦再21 小时前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame
hashiqimiya21 小时前
权限更改centos中系统文件无法创建文件夹,使用命令让普通用户具备操作文件夹
linux
逆小舟1 天前
【Linux】人事档案——用户及组管理
linux·c++