# nftables 维护

## 一、基础查看

### 查看完整规则

```bash
nft list ruleset
```

更清晰（推荐）：

```bash
nft list ruleset -a
```

> 显示 `handle`，删除规则时必须用

---

### 查看指定表

```bash
nft list table ip filter
```

### 查看指定链

```bash
nft list chain inet filter input
```

---

## 二、规则管理

### 清空所有规则 

> 会立即断网（远程操作慎用）

```bash
nft flush ruleset
```

---

### 删除表

```bash
nft delete table ip filter
```

---

### 删除单条规则

1️⃣ 先查 handle

```bash
nft list chain inet filter input -a
```

2️⃣ 删除

```bash
nft delete rule inet filter input handle <ID>
```

---

## 三、配置与服务

### 重载配置

```bash
nft -f /etc/nftables.conf
```

或：

```bash
systemctl reload nftables
```

---

### 服务管理

```bash
systemctl restart nftables
systemctl stop nftables
systemctl disable nftables
systemctl enable nftables
```

---

### 配置备份与恢复

备份：

```bash
nft list ruleset > backup.nft
```

恢复：

```bash
nft -f backup.nft
```

---

## 四、日志与调试

### 添加日志规则

```bash
nft add rule inet filter input log prefix "NFT: "
```

查看日志：

```bash
journalctl -f
```

> 推荐过滤：

```bash
journalctl -f | grep -w NFT
```

---

## 五、CrowdSec 联动（重点增强）

### 1️⃣ CrowdSec 已封禁，但 nftables 看不到？

先确认 CrowdSec：

```bash
cscli decisions list
```

如果有 IP，例如：

```
1.2.3.4  ban
```

---

### 2️⃣ 查找 CrowdSec 写入的 nftables 表

CrowdSec firewall bouncer 默认会创建类似：

```bash
nft list tables
```

常见结果：

```
inet crowdsec
ip crowdsec
```

---

### 3️⃣ 查看 CrowdSec 封禁链

```bash
nft list table ip crowdsec
```

或：

```bash
nft list chain inet crowdsec crowdsec-chain
```

>  不同版本名字可能不同：

* `crowdsec-chain`
* `input`

---

### 4️⃣ 查找具体 IP 是否存在

```bash
nft list ruleset | grep -w 1.2.3.4
```

或更精准：

```bash
nft list table ip crowdsec | grep -w 1.2.3.4
```

---

### 5️⃣ 使用 set（关键点）

CrowdSec 通常不会一条条规则写 IP，而是用 **set**：

```bash
nft list sets
```

查看：

```bash
nft list set ip crowdsec crowdsec-blacklists-crowdsec
```

> 你会看到：

```
elements = { 1.2.3.4, 5.6.7.8 }
```

✔️ 这才是真正的封禁来源

---

### 6️⃣ 手动验证封禁是否生效

```bash
nft list ruleset | grep -w drop
```

常见规则：

```
ip saddr @crowdsec-blacklists drop
```

说明：
> 命中 set 就会被 drop

---

### 7️⃣ 测试封禁（实战）

从服务器执行：

```bash
ping -c 1 <被封IP>
```

或查看连接：

```bash
ss -tnp | grep -w <IP>
```

---

### 8️⃣ 常见问题排查

#### 看不到 crowdsec 表

可能原因：

* bouncer 没运行
* API key 错误

检查：

```bash
systemctl status crowdsec-firewall-bouncer
```

---

#### 有 decision，但 nftables 没规则

> 重点检查：

```bash
cscli bouncers list
```

确认 bouncer 是否在线

---

#### nftables 有规则但不生效

检查链是否挂载到 input：

```bash
nft list ruleset | grep -w crowdsec
```

如果没有 hook：

说明规则没接入主链

---

## 六、高级技巧

### 实时监控规则变化

```bash
watch -n 1 nft list ruleset
```

---

### 临时封禁 IP（手动）

```bash
nft add element inet crowdsec crowdsec-blacklists { 1.2.3.4 }
```

---

### 手动解封

```bash
nft delete element inet crowdsec crowdsec-blacklists { 1.2.3.4 }
```

---

## 七、总结（核心认知）

* nftables ≠ 单条规则系统，而是 **table / chain / set 结构**
* CrowdSec：

  * decision → 写入 set
  * nftables → 通过 set 批量 drop
* 查 IP 是否被封：

```bash
cscli decisions list
nft list set inet crowdsec crowdsec-blacklists
```

> 两边都要看，才完整

---