阿良の小屋

安装并配置 Fail2ban

2026-10-16

安装 fail2ban

以 ubuntu 系统为例:

sudo apt update
sudo apt install -y fail2ban

创建自定义配置文件

不要直接修改默认的 /etc/fail2ban/jail.conf,请复制一份作为本地自定义配置:

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

修改配置文件

编辑 /etc/fail2ban/jail.local 文件:
针对 SSH 服务的典型配置样例(1 分钟错误 10 次,封禁 1 小时):

[sshd]
enabled = true
port = ssh
filter = sshd
logpath = %(sshd_log)s
maxretry = 10
findtime = 1m
bantime = 1h

启动服务并设置开机启动

sudo systemctl start fail2ban
sudo systemctl enable fail2ban

服务重启命令(修改配置后需要执行):

sudo systemctl restart fail2ban

常用管理命令

查看 SSH 封禁状态: sudo fail2ban-client status sshd
手动解封 IP: sudo fail2ban-client set sshd unbanip <IP_ADDRESS>


Fin.

Tags: Linux

扫描二维码,分享此文章