更新 防火墙.MD
This commit is contained in:
@@ -116,3 +116,99 @@ nft list ruleset
|
||||
```
|
||||
|
||||
3、填充中国大陆 IP 网段到 nftables 的`cn_ip`集合
|
||||
> nftables **不能直接用 ipset 命令**,ipset 是 iptables 的组件。需要脚本把 APNIC 网段导入 nftables set。
|
||||
|
||||
新建脚本 `/usr/local/bin/update-cn-set.sh`
|
||||
|
||||
```
|
||||
#!/bin/bash
|
||||
TMP=$(mktemp)
|
||||
curl -s https://ftp.apnic.net/stats/apnic/delegated-apnic-latest > $TMP
|
||||
|
||||
nft flush set ip filter cn_ip
|
||||
awk -F'|' '$2=="CN"&&$3=="ipv4"{len=32-log($5)/log(2);print "add @cn_ip " $4 "/" len}' $TMP | nft -f -
|
||||
rm -f $TMP
|
||||
```
|
||||
|
||||
赋予执行权限:
|
||||
|
||||
```
|
||||
chmod +x /usr/local/bin/update-cn-set.sh
|
||||
```
|
||||
|
||||
**手动执行一次,加载 CN 网段:**
|
||||
|
||||
```
|
||||
/usr/local/bin/update-cn-set.sh
|
||||
```
|
||||
|
||||
验证集合是否有网段:
|
||||
|
||||
```
|
||||
nft list set ip filter cn_ip
|
||||
```
|
||||
|
||||
### 设置开机自动加载网段
|
||||
|
||||
编辑 `/etc/network/if-pre-up.d/update-cn-set`
|
||||
|
||||
```
|
||||
#!/bin/sh
|
||||
/usr/local/bin/update-cn-set.sh
|
||||
```
|
||||
|
||||
```
|
||||
chmod +x /etc/network/if-pre-up.d/update-cn-set
|
||||
```
|
||||
|
||||
>
|
||||
> 开机网络就绪后,自动拉取 CN 网段填充 nftables 集合。
|
||||
|
||||
## ⚠️重要提醒
|
||||
|
||||
1. 你之前执行过`ipset create cn_ip`,**nftables 的 set 和 ipset 完全独立**,之前 ipset 命令创建的集合对 nftables 无效,可以删掉:
|
||||
|
||||
```
|
||||
ipset destroy cn_ip
|
||||
```
|
||||
|
||||
2. 不要同时跑 iptables/ipset 和 nftables 两套防火墙。
|
||||
3. **云服务器安全组!** 云服务商控制台安全组优先级高于主机 nftables,安全组如果放行 40000‑50000,主机防火墙拦截会失效。
|
||||
4. 测试:国内机器访问 40000 端口,应当`connection refused / timeout`。
|
||||
|
||||
## 如果你想回退,清空所有 nftables 规则
|
||||
|
||||
```
|
||||
nft flush ruleset
|
||||
systemctl stop nftables
|
||||
systemctl disable nftables
|
||||
```
|
||||
|
||||
## 排查点
|
||||
|
||||
- `nft -f /etc/nftables.conf --check` 一定要跑,配置写错直接开机会断连。
|
||||
- 如果执行`update‑cn‑set.sh`无 IP 写入集合:服务器无法访问 apnic 网站,可以把 delegated‑apnic‑latest 文件下载到本地,修改脚本读取本地文件。
|
||||
|
||||
### 补充:完整可用 `/etc/nftables.conf` 参考
|
||||
|
||||
```
|
||||
#!/usr/sbin/nft -f
|
||||
|
||||
flush ruleset
|
||||
|
||||
table ip filter {
|
||||
set cn_ip {
|
||||
type ipv4_addr
|
||||
flags interval
|
||||
auto-merge
|
||||
}
|
||||
|
||||
chain input {
|
||||
type filter hook input priority 0; policy accept;
|
||||
ip saddr @cn_ip tcp dport 40000-50000 drop
|
||||
ip saddr @cn_ip udp dport 40000-50000 drop
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
保存后再执行语法校验。
|
||||
Reference in New Issue
Block a user