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
相关推荐
专注VB编程开发20年3 分钟前
VB.NET Asp.Net Core模板WebAPI应用-宝塔面板Linux系统通过Docker部署
linux·docker·.net
bob_young9 分钟前
Gerrit的安装与使用说明(Ubuntu)
linux·gerrit
大大大大肉包1 小时前
私有化部署DeepSeek
linux·运维·服务器
xyd陈宇阳1 小时前
Linux 入门五:Makefile—— 从手动编译到工程自动化的蜕变
linux·运维·服务器·makefile
脑斧猴3 小时前
Linux中进程
linux·服务器·c++
Brandon汐3 小时前
Linux文件传输:让数据飞起来!
linux·运维·网络
tan180°4 小时前
Linux自行实现的一个Shell(15)
linux·服务器·c++·后端·vim
开发小能手-roy4 小时前
linux Ubuntu 用户权限设置
linux·运维·ubuntu
小白探索世界欧耶!~5 小时前
【踩坑】GitHub Actions 运行的 Linux 环境中,文件名是大小写敏感的
linux·运维·服务器·前端·vue.js·笔记·github
lyingcloud5 小时前
debian系统中文输入法失效解决
linux·运维·debian