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
相关推荐
Nerd Nirvana16 分钟前
软考—系统架构设计(案例 | 论文)
linux·系统架构·软件工程·软考·计算机基础
勤奋的凯尔森同学1 小时前
webmin配置终端显示样式,模仿UbuntuDesktop终端
linux·运维·服务器·ubuntu·webmin
希忘auto4 小时前
详解Redis在Centos上的安装
redis·centos
打不了嗝 ᥬ᭄6 小时前
Linux的权限
linux
落幕6 小时前
C语言-进程
linux·运维·服务器
深度Linux6 小时前
C++程序员内功修炼——Linux C/C++编程技术汇总
linux·项目实战·c/c++
风静如云7 小时前
OpenBMC:BmcWeb定义service
linux
leoufung8 小时前
VIM FZF 安裝和使用
linux·编辑器·vim
bugtraq20219 小时前
XiaoMi Mi5(gemini) 刷入Ubuntu Touch 16.04——安卓手机刷入Linux
linux·运维·ubuntu
CodeWithMe9 小时前
[ Vim ] 常用命令 and 配置
linux·编辑器·vim