Files
deploy.stack/etc/fail2ban/jail.d/00-common.local
T
cnphpbb 8f80c7dbba docs(etc): 补充 Fail2ban 配置覆盖规则
- 说明 jail.d/*.local 的加载顺序及覆盖范围
- 补充生效值核对命令和白名单合并要求
2026-11-09 00:15:46 +08:00

58 lines
3.5 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# fail2ban —— 所有 jail 共用的策略(公用版本)
#
# ── 加载顺序(fail2ban 1.1.0 源码 client/configreader.py:213-221)────────
# jail.conf -> jail.d/*.conf -> jail.local -> jail.d/*.local(按名排序)
# 后读的覆盖先读的。四个来源里 jail.d/*.local 排在**最后,优先级最高**。
#
# ── 覆盖关系:本目录的四个 .local 文件会**盖掉**机器上的 /etc/fail2ban/jail.local ──
# 也就是说:jail.local 里写的 [DEFAULT] bantime / findtime / banaction / ignoreip,
# 以及 [sshd] 这类同名 jail 段的同名选项,**在本目录铺开后不生效**。
# 实测(fail2ban 1.1.0):
# jail.local 写 bantime=99h / findtime=20m / banaction=multiport / [sshd] maxretry=9
# 只有 jail.local 时 -> 生效 99h / 20m / multiport / 9
# 铺开本目录后 -> 生效 1d / 10m / allports / 3
#
# 不冲突的情况:jail.local 里**只在其他 jail 段**配置的(如 [nginx-http-auth]、
# [apache-auth])属于独立 section,与本目录互不影响,照常启用。
#
# ⚠ 最容易踩的一条:jail.local 里的 [DEFAULT] ignoreip 会被本文件整段覆盖。
# 若本机 jail.local 有额外白名单(VPN 出口、监控 IP 等),必须把那些网段
# 合并进本文件的 ignoreip,否则那些 IP 会被误封。
#
# ── 为什么用 00- 前缀 ─────────────────────────────────────────
# 同一次读取中 jail.d/*.local 按文件名排序,00- 保证本文件的 [DEFAULT]
# 先于 sshd.local / gitea-ssh.local / recidive.local 加载,
# 让后面那几个单 jail 文件能按需覆盖 [DEFAULT] 里的公共策略。
[DEFAULT]
# ── 端口无关封禁(本方案的核心)────────────────────────────────
# sshd 端口在各机器上落在 49152-65535 内且每台不同,写死 port 必然出错。
# nftables 的 type=allports 生成的规则是:
# meta l4proto { tcp } <family> saddr @<set> reject
# 完全不引用 <port>,因此端口是多少都不影响封禁是否生效。
# 对比 type=multiport 会生成 tcp dport { <port> },端口写错 = 检测到但封不住。
banaction = nftables[type=allports]
banaction_allports = nftables[type=allports]
# ── 安全默认 ──────────────────────────────────────────────
# usedns=no:不信任反向解析(默认 warn 会解析主机名,可被 DNS 欺骗/拖慢)
usedns = no
# ignoreself:永不封本机
ignoreself = true
# ── 白名单 ────────────────────────────────────────────────
# 必须包含管理出口 IP,否则管理员自己会被封(fail2ban-client set banip 还会绕过本项)。
# 保留 RFC1918:内网探测/监控/tailscale 等不应触发封禁。
# 注意:本项会覆盖 jail.local 的 [DEFAULT] ignoreip(见文件头说明),
# 本机额外的白名单网段要合并写在这里。
ignoreip = 127.0.0.1/8 ::1 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 183.238.148.253/32
# ── 递增封禁:1d -> 2d -> 4d -> ... 上限 4w ─────────────────
bantime = 1d
findtime = 10m
maxretry = 3
bantime.increment = true
bantime.factor = 2
bantime.maxtime = 4w