跳转到内容
搜索 AlmaLinux 文档

    浏览当前文档目录

    AlmaLinux 10 fail2ban 安装与最小配置 — EPEL 10 来源、fail2ban-firewalld 子包、jail.local 覆盖

    在 almalinux:10 容器(aarch64;10.2「Lavender Lion」,镜像 almalinux:10@sha256:838c2faf…f1b,2026-10-31)中实际执行:extras→epel-release→EPEL 启用、dnf install fail2ban fail2ban-firewalld 与 rpm -q 子包清点、rpm -ql fail2ban-firewalld 与 00-firewalld.conf 内容、/etc/fail2ban 目录初态、最小 jail.local 的 fail2ban-client -d 配置解析、fail2ban-client start 与 -h。原始日志:plans/content-expansion/evidence/logs/alma-fail2ban.log。

    本页没有执行:systemd 运行时(容器无 systemd)、真实封禁效果、sshd jail 长期运行——这些标注「未实测」,见文末「未验证事项」。

    fail2ban 不在 AlmaLinux 10 的基础仓库里(BaseOS/AppStream/CRB 均无),EL10 上的唯一官方渠道是 EPEL 10。启用只需两步——epel-release 由系统自带的 extras 仓库提供:

    Terminal window
    sudo dnf install -y epel-release
    dnf repolist
    # appstream baseos crb extras epel ← epel 已进入启用列表

    实测启用后的 epel 仓库名为 Extra Packages for Enterprise Linux 10 - aarch64。EPEL 启用的完整细节(repo 文件结构、per-minor 分支、exclude 风险)见本站 EPEL 10 启用指南。

    安装与 fail2ban-firewalld 集成子包(实测)

    Section titled “安装与 fail2ban-firewalld 集成子包(实测)”
    Terminal window
    sudo dnf install -y fail2ban fail2ban-firewalld
    rpm -q fail2ban fail2ban-firewalld fail2ban-selinux fail2ban-server

    实测结果(1.1.0-6.el10_0):

    包是否装上说明
    fail2ban✅ 1.1.0-6.el10_0meta 包,负责把子包拉齐
    fail2ban-server✅ 1.1.0-6.el10_0服务端与 fail2ban-client
    fail2ban-firewalld✅ 1.1.0-6.el10_0与 firewalld 的集成子包
    fail2ban-selinux❌ 未安装enforcing 主机建议另装

    连带安装了 python3-firewall、python3-nftables 等 firewalld 依赖。子包矩阵是 EL 上装 fail2ban 最容易漏的点:集成子包 fail2ban-firewalld 需显式安装(本页同时装了 fail2ban + fail2ban-firewalld,未做单独安装 fail2ban 的隔离对照),默认 banaction 也不是你想要的。

    最小 jail 配置与解析验证(实测)

    Section titled “最小 jail 配置与解析验证(实测)”

    先看包的初始状态:/etc/fail2ban/jail.conf 存在(25,847 字节),头注写得很直白——

    # YOU SHOULD NOT MODIFY THIS FILE.
    # Provide customizations in a jail.local file or a jail.d/customisation.local.

    即 jail 定制只写 jail.local 或 jail.d/*.local——.local 后缀不会被包升级覆盖。安装后 jail.d/ 里只有包提供的 00-firewalld.conf。

    写最小 sshd jail(EL8+ 关键点:日志后端必须显式指定 systemd,因为 sshd 日志走 journald 而非明文文件):

    /etc/fail2ban/jail.local
    [sshd]
    enabled = true
    backend = systemd

    配置解析用 fail2ban-client -d(dump 配置)验证,实测输出关键行:

    ['add', 'sshd', 'systemd'] ← systemd 后端被接受
    ['set', 'sshd', 'addjournalmatch', '_SYSTEMD_UNIT=sshd.service', '+', '_COMM=sshd', ...]
    ['set', 'sshd', 'bantime', '10m'] ← 继承 jail.conf 默认
    ['set', 'sshd', 'findtime', '10m']
    ['set', 'sshd', 'maxretry', 5]
    ['set', 'sshd', 'addaction', 'firewallcmd-rich-rules'] ← 继承 00-firewalld.conf

    这一行链路证明了三件事:jail.local 被读取、sshd jail 以 journal 为后端启用、默认 banaction 来自 fail2ban-firewalld 子包。改配置后可用 fail2ban-client -t(test)做无启动校验。

    systemd 环境的启用(未实测口径)

    Section titled “systemd 环境的启用(未实测口径)”

    容器无 systemd,以下命令为标准流程转述,未在本文验证:

    Terminal window
    sudo systemctl enable --now fail2ban
    sudo systemctl status fail2ban
    sudo fail2ban-client status sshd # 查 jail 状态与已封 IP
    sudo fail2ban-client set sshd unbanip <目标IP> # 手工解封

    判断 jail 是否真正工作要看 status sshd 的 Currently banned 计数与 journalctl -u fail2ban 的匹配日志,这部分在实机 VM 上验证后再补充。

    与 firewalld 的协作机制(集成文件实证)

    Section titled “与 firewalld 的协作机制(集成文件实证)”

    rpm -ql fail2ban-firewalld 实测该子包只装一个文件:

    /etc/fail2ban/jail.d/00-firewalld.conf

    内容(实测原文):

    [DEFAULT]
    banaction = firewallcmd-rich-rules
    banaction_allports = firewallcmd-rich-rules

    这就是协作的全部机制——fail2ban 不直接操作 nftables/iptables,而是通过 firewallcmd-rich-rules action 调 firewall-cmd --add-rich-rule="rule family='ipv4' source address='<目标IP>' port port='ssh' protocol='tcp' reject" 把封禁作为 rich rule 落进 firewalld,解封对应 --remove-rich-rule。-d 输出里能看到 action 全文(actionban/actionunban 两条 firewall-cmd 命令)。好处是封禁规则进入 firewalld 的统一管理面,firewall-cmd --list-rich-rules 直接可见;代价是依赖 firewalld 运行中。

    • 真实封禁效果:容器无 SSH 攻击流量与 journal 事件,banaction 触发、firewalld rich rule 落地、status sshd 计数变化均未实测。
    • systemd 运行时:systemctl enable/start、守护进程常驻、开机自启未实测(容器无 systemd)。
    • 长期运行:bantime/findtime 轮转、数据库(/var/lib/fail2ban/fail2ban.sqlite3)持久化、firewalld --reload 后封禁规则是否保留,均未实测。
    • banaction 推荐值:本文实测的是 00-firewalld.conf 的 firewallcmd-rich-rules(el10_0 包);实机默认值随包版本可能变化,以实机 jail.conf 原文为准。