Ubuntu 22.04 安装 fail2ban 1.1.1 指南
Ubuntu Server 22.04 从零安装 fail2ban 实战:apt 仓库 0.11.2 与官方源码 1.1.1 双路径安装、systemd 托管、版本选型、升级降级、卸载清理与常见排障,全程实测。
Ubuntu 22.04 安装 fail2ban 1.1.1 指南
目标:在 Ubuntu Server 22.04(jammy)上从零完成 fail2ban 入侵防御封禁框架的安装、systemd 托管与运行验证,覆盖「apt 官方仓库(0.11.2-6)」与「官方发布件源码安装(1.1.1 最新稳定版)」两条完整路径,并给出升级、降级、卸载与常见排障方案。已在 Ubuntu 22.04 环境对两条安装路径逐条实测通过。
一、环境信息
- 操作系统:Ubuntu Server 22.04 LTS(jammy),x86_64;
- 内核:6.8.0-138-generic;
- fail2ban 版本:apt 仓库版 0.11.2-6(jammy/universe),源码版 1.1.1(上游最新稳定版,2026-08-15 发布);
- Python:3.10.12(系统自带);
- 服务托管:systemd 249(
fail2ban.service); - 相关软件:openssh-server 1:8.9p1(被保护对象)、rsyslog(auth.log 落盘)、sqlite3 3.37.2(可选,用于检查 fail2ban 数据库);
- 实测数据目录:
/etc/fail2ban/(配置)、/var/lib/fail2ban/(数据库)、/var/log/fail2ban.log(日志)。
二、前置条件
安装前确认以下四点,每点给出验证命令与实测回显。
1. 系统版本与架构(本文命令面向 Ubuntu 22.04):
cat /etc/os-release | head -2
实测回显:
PRETTY_NAME="Ubuntu 22.04.x LTS"
NAME="Ubuntu"
2. systemd 可用(fail2ban 以 systemd 服务托管):
systemctl is-system-running
实测回显(running 或 degraded 均可正常承载 fail2ban):
degraded
3. apt 源可达且 universe 仓库启用(fail2ban 位于 universe):
apt-get update
实测回显(输出节选):
Hit:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
Reading package lists...
4. (被保护对象)openssh-server 已安装并监听 22 端口:
apt-get install -y openssh-server rsyslog
systemctl enable --now ssh
ss -ltn | grep ':22'
实测回显:
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=4321,fd=3))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=4321,fd=4))
⚠️ 注意:最小化安装的 Ubuntu(如部分云镜像/定制镜像)可能未预装 rsyslog,此时
/var/log/auth.log不存在;rsyslog 装好后 sshd 的认证日志会同时落盘到/var/log/auth.log并进入 systemd journal。fail2ban 在 Debian/Ubuntu 系对 sshd jail 默认读 journal(paths-debian.conf中sshd_backend = systemd),两种日志形态均可被识别。
三、版本选型说明
fail2ban 官方发布节奏较慢,Ubuntu 22.04 仓库内置版本与上游最新稳定版存在代差,选型前先对照:
| 来源 | 版本 | 发布时间 | 获取方式 | 适用场景 |
|---|---|---|---|---|
| Ubuntu jammy/universe | 0.11.2-6 | 2020-11(上游 0.11.2) | apt install fail2ban | 追求包管理一致、可自动随系统升级、对版本不敏感 |
| GitHub 官方发布件 | 1.1.1 | 2026-08-15 | wget 源码 tar.gz + python3 setup.py install | 需要 1.1.x 新特性(递增禁时默认策略、nftables 默认动作、systemd 后端增强等) |
关键差异解读:0.11.2 与 1.1.1 在默认封禁动作上有实质区别——0.11.2 的 Debian 打包默认动作是 iptables-multiport,而 1.1.1 在 Debian/Ubuntu 的 paths-debian.conf 中已改为 banaction = nftables;且 1.1.x 对 systemd journal 后端、ignoreself(自动忽略自身 IP)等行为更完善。生产新部署建议优先源码安装 1.1.1。
版本溯源(查询日期 2026-10-24,来源 GitHub Releases):
| 版本 | 发布日期 |
|---|---|
| 0.11.2 | 2020-11-23 |
| 1.0.1 | 2022-09-27 |
| 1.0.2 | 2022-11-09 |
| 1.1.0 | 2024-04-25 |
| 1.1.1 | 2026-08-15 |
四、方案 A:apt 官方仓库在线安装(0.11.2-6)
4.1 方案说明
Ubuntu 官方仓库一键安装,自动注册 systemd 服务与 logrotate 配置,是与系统集成度最高的方式。代价是版本停留在 0.11.2-6。
4.2 安装步骤
apt-get install -y fail2ban
实测回显(全新安装,输出节选,略去读库进度行与 debconf 提示):
Reading package lists...
Building dependency tree...
Reading state information...
Suggested packages:
mailx system-log-daemon monit sqlite3
The following NEW packages will be installed:
fail2ban
0 upgraded, 1 newly installed, 0 to remove and 3 not upgraded.
Need to get 394 kB of archives.
After this operation, 2098 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy/universe amd64 fail2ban all 0.11.2-6 [394 kB]
Fetched 394 kB in 2s (211 kB/s)
Selecting previously unselected package fail2ban.
Preparing to unpack .../fail2ban_0.11.2-6_all.deb ...
Unpacking fail2ban (0.11.2-6) ...
Setting up fail2ban (0.11.2-6) ...
⚠️ 注意:部分环境(定制镜像、自动化部署模板)配置了
policy-rc.d策略,安装日志末尾会出现invoke-rc.d: policy-rc.d denied execution of start.,表示包管理器未自动拉起服务。无论哪种情况,统一用下节的systemctl enable --now fail2ban显式启用即可。
4.3 systemd 托管与验证
启用开机自启并立即启动:
systemctl enable --now fail2ban
验证服务状态:
systemctl status fail2ban --no-pager | head -6
实测回显:
● fail2ban.service - Fail2Ban Service
Loaded: loaded (/lib/systemd/system/fail2ban.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2026-10-23 20:49:00 UTC; 9min ago
Docs: man:fail2ban(1)
Main PID: 4987 (fail2ban-server)
Tasks: 5 (limit: 94371)
Memory: 15.1M
验证客户端与服务端连通及版本:
fail2ban-client -V
fail2ban-client status
dpkg -l fail2ban | tail -1
实测回显:
1.1.1
Status
|- Number of jail: 1
`- Jail list: sshd
ii fail2ban 0.11.2-6 all ban hosts that cause multiple authentication errors
💡 说明:apt 版安装后
jail.d/defaults-debian.conf默认就启用了 sshd jail,因此status已显示 1 个 jail。fail2ban-client -V显示的 1.1.1 为后文源码安装覆盖 PATH 后的版本号;若仅执行方案 A,此处显示 0.11.2,属正常差异。
4.4 安装落点
- 可执行文件:
/usr/bin/fail2ban-client、/usr/bin/fail2ban-server、/usr/bin/fail2ban-regex; - 配置目录:
/etc/fail2ban/(jail.conf、jail.d/、filter.d/ 103 个过滤器、action.d/ 69 个动作); - systemd 单元:
/usr/lib/systemd/system/fail2ban.service; - 数据库:
/var/lib/fail2ban/fail2ban.sqlite3(服务首次启动后生成); - 日志轮转:
/etc/logrotate.d/fail2ban。
五、方案 B:官方发布件源码安装 1.1.1(最新稳定版)
5.1 方案说明
从 GitHub 官方发布件(源码 tar.gz)安装上游最新稳定版 1.1.1,二进制落在 /usr/local/bin(PATH 优先于 /usr/bin),可与方案 A 的 apt 版共存,是获取最新版本的标准路径。依赖 Python >= 3.5 与 python3-setuptools(来源:官方仓库 README Requirements 节)。
5.2 安装依赖
apt-get install -y python3-setuptools wget
实测回显(已装时):
python3-setuptools is already the newest version (59.6.0-1.2ubuntu0.3).
5.3 下载官方发布件并校验
cd /root
wget https://github.com/fail2ban/fail2ban/archive/refs/tags/1.1.1.tar.gz -O f2b.tar.gz
sha256sum f2b.tar.gz
ls -la f2b.tar.gz
实测回显(下载完成,实测尾部):
2026-10-23 21:00:51 (1.12 MB/s) - 'f2b.tar.gz' saved [627126/627126]
sha256sum f2b.tar.gz
ls -la f2b.tar.gz
实测回显:
4be0ea0488e32de260058462a44a040f0542cd26a9fb6fa6d2514f9dd8ec1609 f2b.tar.gz
-rw-r--r-- 1 root root 627126 Oct 23 20:36 f2b.tar.gz
⚠️ 注意:GitHub 下载慢或超时时,可为 wget 追加代理参数
wget -e use_proxy=yes -e https_proxy=http://<代理地址:端口> <URL>重试。
5.4 解压与安装
tar -xzf f2b.tar.gz
cd fail2ban-1.1.1
python3 setup.py install
实测回显(输出节选,实测首尾):
running install
/usr/lib/python3/dist-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
warnings.warn(
running build
running build_py
creating build/lib
copying fail2ban/version.py -> build/lib/fail2ban
...
creating build/scripts-3.10
copying and adjusting bin/fail2ban-client -> build/scripts-3.10
copying and adjusting bin/fail2ban-server -> build/scripts-3.10
copying and adjusting bin/fail2ban-regex -> build/scripts-3.10
...
copying build/scripts-3.10/fail2ban-testcases -> /usr/local/bin
copying build/scripts-3.10/fail2ban-server -> /usr/local/bin
copying build/scripts-3.10/fail2ban-regex -> /usr/local/bin
...
You can also install systemd service-unit file from "build/fail2ban.service"
(optionally paired with "build/fail2ban.socket" to enable socket activation)
resp. corresponding init script from "files/*-initd".
安装落点验证:
which fail2ban-client fail2ban-server fail2ban-regex
fail2ban-client -V
实测回显:
/usr/local/bin/fail2ban-client
/usr/local/bin/fail2ban-server
/usr/local/bin/fail2ban-regex
1.1.1
5.5 ⚠️ 真实排障:systemd 单元内路径与安装前缀不一致
源码安装的 systemd 单元落在 /usr/lib/systemd/system/fail2ban.service,但单元内 ExecStart 仍指向 /usr/bin/fail2ban-server,而源码二进制实际装在 /usr/local/bin。若环境里没有 apt 版(或已卸载),直接启动会报「status=203/EXEC」类执行失败。先查看单元确认:
grep ExecStart /usr/lib/systemd/system/fail2ban.service
实测回显(修复前):
ExecStart=/usr/bin/fail2ban-server -xf start
# ExecStart=/usr/bin/fail2ban-server -xf --logtarget=sysout start
将单元内的三个路径(ExecStart/ExecStop/ExecReload)改为实际安装前缀 /usr/local/bin,并重载 systemd:
sed -i 's|/usr/bin/fail2ban-server|/usr/local/bin/fail2ban-server|; s|/usr/bin/fail2ban-client|/usr/local/bin/fail2ban-client|' /usr/lib/systemd/system/fail2ban.service
systemctl daemon-reload
grep ExecStart /usr/lib/systemd/system/fail2ban.service
实测回显(修复后):
ExecStart=/usr/local/bin/fail2ban-server -xf start
# ExecStart=/usr/local/bin/fail2ban-server -xf --logtarget=sysout start
5.6 启动与验证
systemctl enable --now fail2ban
systemctl is-active fail2ban
fail2ban-client status
实测回显:
active
Status
|- Number of jail: 1
`- Jail list: sshd
再用配置自检命令做一次完整校验:
fail2ban-client -t
实测回显:
OK: configuration test is successful
六、方案对比与选择建议
| 维度 | 方案 A:apt 仓库 | 方案 B:源码 1.1.1 |
|---|---|---|
| 版本 | 0.11.2-6(2020 上游版) | 1.1.1(2026-08 上游最新稳定版) |
| 联网要求 | 需要(Ubuntu 源) | 需要(GitHub 下载,可离线拷入 tar 包) |
| 默认封禁动作 | iptables-multiport(0.11 打包默认) | nftables(1.1.1 Debian 系 paths-debian.conf 默认) |
| systemd 单元 | 包内置,路径正确 | 需按 5.5 修正单元内路径 |
| 升级维护 | 随 apt upgrade 自动 | 重复 5.3~5.6 手动升级 |
| 与系统集成 | 自动 logrotate、补丁跟随发行版 | 前缀 /usr/local,需自行管理清理 |
| 适用 | 求稳、求省心的生产机 | 要新特性(nftables 默认、journal 增强、递增禁时默认可用)的新部署 |
选择建议:可接受 0.11.2 旧版选 A;需要 1.1.x 行为与特性选 B。两者共存时 /usr/local/bin 优先,实际执行的永远是源码版。
七、配置说明
安装后最小可用配置:创建 /etc/fail2ban/jail.local(不要直接改 jail.conf,官方约定 .local 覆盖文件在升级时不被覆盖):
cat > /etc/fail2ban/jail.local <<'EOF'
[sshd]
enabled = true
EOF
应用配置(无需重启服务):
fail2ban-client reload
实测回显:
OK
验证生效(bantime/findtime/maxretry 默认值):
fail2ban-client get sshd bantime
fail2ban-client get sshd findtime
fail2ban-client get sshd maxretry
实测回显(jail.local 仅显式启用 sshd、未覆盖 [DEFAULT] 时,即全默认值):
600
600
5
💡 说明:以上为
jail.conf[DEFAULT] 段默认值(bantime = 10m、findtime = 10m、maxretry = 5,实测自 1.1.1 jail.conf 第 103/107/110 行)。逐参数详解与 filter.d/action.d 结构见《fail2ban 1.1 配置参数详解》。
八、升级、降级与多版本管理
查看当前实际生效版本与来源:
which fail2ban-client
fail2ban-client -V
dpkg -l fail2ban | tail -1
实测回显(源码版生效时):
/usr/local/bin/fail2ban-client
1.1.1
ii fail2ban 0.11.2-6 all ban hosts that cause multiple authentication errors
先弄清「版本号从哪来」:fail2ban-client 只是个入口脚本,它 import 的 Python 库决定版本号。源码安装把库放进 /usr/local/lib/python3.10/dist-packages/fail2ban(实测含 fail2ban-1.1.1.egg-info),而 Python 的模块搜索顺序中 /usr/local/.../dist-packages 优先于 /usr/lib/python3/dist-packages——因此即便显式调用 /usr/bin/fail2ban-client(apt 版脚本,实测文件 mtime 仍为打包日期、未被源码覆盖),-V 输出的仍是 1.1.1(实测)。校验库版本:
python3 -c "import fail2ban.version as v; print(v.version)"
ls -d /usr/local/lib/python3.10/dist-packages/fail2ban*
实测回显:
1.1.1
/usr/local/lib/python3.10/dist-packages/fail2ban
/usr/local/lib/python3.10/dist-packages/fail2ban-1.1.1.egg-info
升级源码版到新版本:重复「下载新版 tar.gz → 解压 → python3 setup.py install → systemctl restart fail2ban」即可,配置目录 /etc/fail2ban 与数据库不会被动。
降级回 apt 版:必须同时移除源码版脚本与库,否则 apt 版脚本仍会 import 到 /usr/local 下的 1.1.1 库:
systemctl stop fail2ban
rm -f /usr/local/bin/fail2ban-client /usr/local/bin/fail2ban-server /usr/local/bin/fail2ban-regex /usr/local/bin/fail2ban-testcases
rm -rf /usr/local/lib/python3.10/dist-packages/fail2ban /usr/local/lib/python3.10/dist-packages/fail2ban-1.1.1.egg-info
sed -i 's|/usr/local/bin/fail2ban-server|/usr/bin/fail2ban-server|; s|/usr/local/bin/fail2ban-client|/usr/bin/fail2ban-client|' /lib/systemd/system/fail2ban.service
systemctl daemon-reload
systemctl start fail2ban
python3 -c "import fail2ban.version as v; print(v.version)"
⚠️ 注意:上述降级流程的最后一步预期输出
0.11.2(回归 apt 库),本文实测环境保留 1.1.1 主线运行、未执行完整降级,该输出为按库搜索路径推导的预期值,执行后请以fail2ban-client -V与dpkg -l fail2ban双重核对。
⚠️ 注意:0.11.2 与 1.1.1 共用
/etc/fail2ban配置目录,降级后若使用过 1.1.1 特有参数(个别 1.1 新参数 0.11 不识别),fail2ban-client -t会报错,按报错注释掉对应行即可。
九、常见问题(FAQ)
Q1:安装后 systemctl status fail2ban 显示 inactive 或找不到服务?
先显式启用:systemctl enable --now fail2ban。若包安装日志出现过 invoke-rc.d: policy-rc.d denied execution of start.,即为安装策略未自动拉起,显式启用即可。实测启用后 systemctl is-active fail2ban 返回 active。
Q2:源码安装后启动失败,journal 显示 status=203/EXEC?
单元内路径与实际安装前缀不一致(ExecStart=/usr/bin/... 而二进制在 /usr/local/bin)。按 5.5 节 sed 修正三个路径并 daemon-reload,实测修复后 systemctl is-active fail2ban 返回 active。
Q3:sshd jail 启动报「Have not found any log file for sshd jail」?
0.11.2 在无 rsyslog 的最小化环境会找不到 /var/log/auth.log。两条解法:安装并启动 rsyslog(apt-get install -y rsyslog),或显式指定 journal 后端——在 jail.local 的 [sshd] 段写 backend = systemd(需安装 python3-systemd)。1.1.1 的 Debian 系配置已默认 sshd_backend = systemd,实测 fail2ban-client status sshd 的 Filter 段显示 Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd + _COMM=sshd-session。
Q4:怎么确认 fail2ban 用的是文件日志还是 journal?
fail2ban-client get sshd logpath
实测回显(journal 后端时无文件监控):
No file is currently monitored
配合 fail2ban-client -d | grep "'sshd'" 可看到 ['add', 'sshd', 'systemd'],确认后端为 systemd。
Q5:升级 Ubuntu 后 fail2ban 服务频繁重启?
查看 journalctl -u fail2ban -n 50,常见原因是 /var/run/fail2ban 运行时目录权限或残留 socket。单元已声明 RuntimeDirectory=fail2ban,若手工删过 socket,systemctl restart fail2ban 重建即可。
十、卸载与清理
卸载 apt 版(连同配置文件):
apt-get purge -y fail2ban
实测回显(输出节选):
Removing fail2ban (0.11.2-6) ...
Purging configuration files for fail2ban (0.11.2-6) ...
卸载源码版(apt 版不存在时):
systemctl stop fail2ban
rm -f /usr/local/bin/fail2ban-client /usr/local/bin/fail2ban-server /usr/local/bin/fail2ban-regex /usr/local/bin/fail2ban-testcases
rm -rf /usr/local/lib/python3.10/dist-packages/fail2ban /usr/local/lib/python3.10/dist-packages/fail2ban-1.1.1.egg-info
rm -f /lib/systemd/system/fail2ban.service
systemctl daemon-reload
rm -rf /etc/fail2ban
rm -rf /var/lib/fail2ban
验证卸载干净(/etc/fail2ban 不存在时的实测回显,取自 apt 版卸载场景):
ls /etc/fail2ban
ls: cannot access '/etc/fail2ban/': No such file or directory
再确认 which fail2ban-client 无输出、python3 -c "import fail2ban" 报 ModuleNotFoundError 即全部清理完成。
十一、参考链接
- GitHub 官方仓库(含 README 安装说明):https://github.com/fail2ban/fail2ban
- 官方 Releases(1.1.1 及历史版本):https://github.com/fail2ban/fail2ban/releases
- 1.1.1 源码包直链:https://github.com/fail2ban/fail2ban/archive/refs/tags/1.1.1.tar.gz
- 官方 Wiki:https://github.com/fail2ban/fail2ban/wiki
- 官方 man 手册(fail2ban-client):https://manpages.ubuntu.com/manpages/jammy/en/man1/fail2ban-client.1.html
站内相关:《fail2ban 介绍与选型指南》、《Ubuntu 22.04 安装 fail2ban 1.1.1 指南》、《fail2ban 1.1 常用 CLI 命令与工具详解》