一、基础查看

查看完整规则

1
nft list ruleset

更清晰(推荐):

1
nft list ruleset -a

显示 handle,删除规则时必须用


查看指定表

1
nft list table ip filter

查看指定链

1
nft list chain inet filter input

二、规则管理

清空所有规则

会立即断网(远程操作慎用)

1
nft flush ruleset

删除表

1
nft delete table ip filter

删除单条规则

1️⃣ 先查 handle

1
nft list chain inet filter input -a

2️⃣ 删除

1
nft delete rule inet filter input handle <ID>

三、配置与服务

重载配置

1
nft -f /etc/nftables.conf

或:

1
systemctl reload nftables

服务管理

1
2
3
4
systemctl restart nftables
systemctl stop nftables
systemctl disable nftables
systemctl enable nftables

配置备份与恢复

备份:

1
nft list ruleset > backup.nft

恢复:

1
nft -f backup.nft

四、日志与调试

添加日志规则

1
nft add rule inet filter input log prefix "NFT: "

查看日志:

1
journalctl -f

推荐过滤:

1
journalctl -f | grep -w NFT

五、CrowdSec 联动(重点增强)

1️⃣ CrowdSec 已封禁,但 nftables 看不到?

先确认 CrowdSec:

1
cscli decisions list

如果有 IP,例如:

1.2.3.4  ban

2️⃣ 查找 CrowdSec 写入的 nftables 表

CrowdSec firewall bouncer 默认会创建类似:

1
nft list tables

常见结果:

inet crowdsec
ip crowdsec

3️⃣ 查看 CrowdSec 封禁链

1
nft list table ip crowdsec

或:

1
nft list chain inet crowdsec crowdsec-chain

不同版本名字可能不同:

  • crowdsec-chain
  • input

4️⃣ 查找具体 IP 是否存在

1
nft list ruleset | grep -w 1.2.3.4

或更精准:

1
nft list table ip crowdsec | grep -w 1.2.3.4

5️⃣ 使用 set(关键点)

CrowdSec 通常不会一条条规则写 IP,而是用 set

1
nft list sets

查看:

1
nft list set ip crowdsec crowdsec-blacklists-crowdsec

你会看到:

elements = { 1.2.3.4, 5.6.7.8 }

✔️ 这才是真正的封禁来源


6️⃣ 手动验证封禁是否生效

1
nft list ruleset | grep -w drop

常见规则:

ip saddr @crowdsec-blacklists drop

说明:

命中 set 就会被 drop


7️⃣ 测试封禁(实战)

从服务器执行:

1
ping -c 1 <被封IP>

或查看连接:

1
ss -tnp | grep -w <IP>

8️⃣ 常见问题排查

看不到 crowdsec 表

可能原因:

  • bouncer 没运行
  • API key 错误

检查:

1
systemctl status crowdsec-firewall-bouncer

有 decision,但 nftables 没规则

重点检查:

1
cscli bouncers list

确认 bouncer 是否在线


nftables 有规则但不生效

检查链是否挂载到 input:

1
nft list ruleset | grep -w crowdsec

如果没有 hook:

说明规则没接入主链


六、高级技巧

实时监控规则变化

1
watch -n 1 nft list ruleset

临时封禁 IP(手动)

1
nft add element inet crowdsec crowdsec-blacklists { 1.2.3.4 }

手动解封

1
nft delete element inet crowdsec crowdsec-blacklists { 1.2.3.4 }

七、总结(核心认知)

  • nftables ≠ 单条规则系统,而是 table / chain / set 结构

  • CrowdSec:

    • decision → 写入 set
    • nftables → 通过 set 批量 drop
  • 查 IP 是否被封:

1
2
cscli decisions list
nft list set inet crowdsec crowdsec-blacklists

两边都要看,才完整