VPS 一键脚本
返回目录 / 教程 / fail2ban

Fail2ban 防 SSH 爆破教程

本教程说明如何用 xyz.sh/fail2ban 为 VPS 的 SSH 开启 Fail2ban 自动封禁。

命令
$ curl -fsSL xyz.sh/fail2ban | bash

说明 · xyz.sh/fail2ban · 最后更新:

适用条件

root 权限;CentOS / Rocky / AlmaLinux 需要能访问 EPEL 源。

需要 systemd 管理的 sshd(常见 VPS 默认如此)。

步骤

可选:先阅读源码 curl -fsSL xyz.sh/fail2ban.sh,并核对 xyz.sh/fail2ban.sh.sha256。

先预览:curl -fsSL xyz.sh/fail2ban | bash -s -- --dry-run

安装并启用:curl -fsSL xyz.sh/fail2ban | bash -s -- -y

自定义:--maxretry 3 --bantime 24h --findtime 10m --ignoreip 203.0.113.5

查看封禁:curl -fsSL xyz.sh/fail2ban | bash -s -- --status

如何验证

运行 fail2ban-client status sshd,可以看到当前失败次数和被封禁的 IP;或 curl -fsSL xyz.sh/fail2ban | bash -s -- --status。

如何撤销或卸载

curl -fsSL xyz.sh/fail2ban | bash -s -- --uninstall -y 会删除 xyz-sshd.local 并重启 fail2ban;如果连软件包也要删,再执行 apt-get remove fail2ban 或 dnf remove fail2ban。