Files
2026-06-02 23:10:58 +08:00

121 lines
3.3 KiB
Markdown

# fnOS EasyTier iptables 转发规则持久化
适用于飞牛 fnOS / Debian 系 systemd 环境:开机后通过 `systemd timer` 延迟加载 iptables 转发规则,避免重启后规则丢失或网络/tun 设备未就绪导致规则加载失败。
默认规则:
- 开启 IPv4 转发:`/proc/sys/net/ipv4/ip_forward = 1`
- 允许 `192.168.2.0/24``192.168.3.0/24` 双向转发
- 允许 `192.168.2.0/24``192.168.3.0/24` 主动访问虚拟组网 `192.168.4.0/24`
- 允许 `192.168.4.0/24``192.168.2.0/24``192.168.3.0/24` 的 RELATED/ESTABLISHED 回包
- 禁止 `192.168.4.0/24` 主动访问 `192.168.2.0/24``192.168.3.0/24`,实现安全隔离
- 允许 `tun0` 入站/出站 FORWARD
- 开机延迟 `20s` 执行
## 一键安装
```bash
curl -fsSL https://gitea.chickliu.fun/Hermes/fnos-iptables-forward/raw/branch/main/install.sh -o install.sh
sudo bash install.sh install
```
或直接:
```bash
curl -fsSL https://gitea.chickliu.fun/Hermes/fnos-iptables-forward/raw/branch/main/install.sh | sudo bash
```
## 自定义网段 / 网卡 / 延迟
```bash
sudo LAN_A=192.168.2.0/24 LAN_B=192.168.3.0/24 VIRTUAL_LAN=192.168.4.0/24 TUN_IF=tun0 DELAY=20s bash install.sh install
```
## 查看状态
```bash
sudo bash install.sh status
```
会显示:
- timer 状态
- service 最近执行结果
- `ip_forward`
- `iptables -S FORWARD`
## 卸载
```bash
sudo bash install.sh uninstall
```
卸载会移除:
- `/home/scripts/iptables-forward.sh`
- `/etc/systemd/system/iptables-forward.service`
- `/etc/systemd/system/iptables-forward.timer`
注意:卸载不会主动清空当前 iptables FORWARD 规则,避免误断网。如需清空请手动管理 iptables。
## 安装后生成的文件
### `/home/scripts/iptables-forward.sh`
```bash
#!/usr/bin/env bash
set -Eeuo pipefail
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -F FORWARD
iptables -P FORWARD ACCEPT
# 放行 192.168.3.0/24 <-> 192.168.2.0/24
iptables -A FORWARD -s 192.168.2.0/24 -d 192.168.3.0/24 -j ACCEPT
iptables -A FORWARD -s 192.168.3.0/24 -d 192.168.2.0/24 -j ACCEPT
# 放行 192.168.3.0/24、192.168.2.0/24 -> 192.168.4.0/24(虚拟组网)
iptables -A FORWARD -s 192.168.3.0/24 -d 192.168.4.0/24 -j ACCEPT
iptables -A FORWARD -s 192.168.2.0/24 -d 192.168.4.0/24 -j ACCEPT
# 放行 192.168.4.0/24 -> 192.168.3.0/24、192.168.2.0/24 的回包(必须)
iptables -A FORWARD -s 192.168.4.0/24 -d 192.168.3.0/24 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -s 192.168.4.0/24 -d 192.168.2.0/24 -m state --state RELATED,ESTABLISHED -j ACCEPT
# 禁止 192.168.4.0/24 主动访问 192.168.3.0/24、192.168.2.0/24(安全隔离)
iptables -A FORWARD -s 192.168.4.0/24 -d 192.168.3.0/24 -j DROP
iptables -A FORWARD -s 192.168.4.0/24 -d 192.168.2.0/24 -j DROP
# 放行 EasyTier 虚拟网卡 tun0
iptables -A FORWARD -i tun0 -j ACCEPT
iptables -A FORWARD -o tun0 -j ACCEPT
```
### `/etc/systemd/system/iptables-forward.service`
```ini
[Unit]
Description=EasyTier iptables forward rules loader
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=/home/scripts/iptables-forward.sh
User=root
```
### `/etc/systemd/system/iptables-forward.timer`
```ini
[Unit]
Description=Delay load EasyTier iptables forward rules after boot
[Timer]
OnBootSec=20s
Unit=iptables-forward.service
[Install]
WantedBy=timers.target
```