南风大叔

PVE + ImmortalWrt 专用出口网关 + VM 默认翻墙 + 公网 SSH 回程修复(策略路由)


写在前面:这篇文章解决什么问题

这是一个真实 Homelab 场景

同时还有一个非常现实的需求:

我需要从公网通过端口转发 SSH 到这台 VM。

这正是本文的核心难点:

默认翻墙 + 公网 DNAT SSH = 非对称路由(SSH 会断)

本文给出的是工程级、可长期运行的最终解法。


网络拓扑总览

1️⃣ 家庭物理网络(LAN)

2️⃣ PVE 专用出口网络(虚拟)


第 1 步:在 PVE 中创建专用网桥 vmbr1

编辑 PVE 宿主机网络配置:

nano /etc/network/interfaces

添加:

auto vmbr1
iface vmbr1 inet manual
    bridge-ports none
    bridge-stp off
    bridge-fd 0

应用并检查:

ifreload -a
ip link show vmbr1

第 2 步:创建 ImmortalWrt VM(双网口)

网卡规划(非常重要)

网卡 PVE 网桥 角色 地址
eth0 vmbr1 LAN(内侧) 10.10.10.1/24
eth1 vmbr0 WAN(外侧) 192.168.31.x

ImmortalWrt 内配置要点:


第 3 步:业务 VM(cn-pek-home)双网卡配置

示例 VM 网卡:

3.1 切换默认出口为 ImmortalWrt(翻墙)

ip route replace 192.168.31.0/24 dev eth0
ip route replace default via 10.10.10.1 dev eth1

验证:

ip route
curl -4 https://ipinfo.io/ip

第 4 步:问题出现 —— 公网 SSH 突然失效

公网做了端口转发:

公网:522  →  192.168.31.5:22

现象:

原因(非常关键)

这是经典的:

非对称路由(Asymmetric Routing)


第 5 步:正确解法 —— 策略路由(mgmt 表)

目标拆解:

5.1 创建路由表

mkdir -p /etc/iproute2
touch /etc/iproute2/rt_tables

echo "100 mgmt" >> /etc/iproute2/rt_tables

5.2 配置 mgmt 路由表(必须包含直连网段)

ip route replace 192.168.31.0/24 dev eth0 scope link table mgmt
ip route replace default via 192.168.31.1 dev eth0 table mgmt

5.3 策略规则(核心)

ip rule add from 192.168.31.5/32 table mgmt priority 100
ip route flush cache

验证铁证:

ip rule
ip route get 1.1.1.1 from 192.168.31.5

应看到:

via 192.168.31.1 dev eth0 table mgmt

第 6 步:rp_filter(必做)

sysctl -w net.ipv4.conf.all.rp_filter=2
sysctl -w net.ipv4.conf.default.rp_filter=2
sysctl -w net.ipv4.conf.eth0.rp_filter=2
sysctl -w net.ipv4.conf.eth1.rp_filter=2

第 7 步:持久化(systemd)

脚本

nano /usr/local/sbin/mgmt-routing.sh
#!/usr/bin/env bash
sleep 2
ip route replace 192.168.31.0/24 dev eth0 scope link table mgmt
ip route replace default via 192.168.31.1 dev eth0 table mgmt
ip rule add from 192.168.31.5/32 table mgmt priority 100 2>/dev/null || true
sysctl -w net.ipv4.conf.all.rp_filter=2
sysctl -w net.ipv4.conf.default.rp_filter=2
sysctl -w net.ipv4.conf.eth0.rp_filter=2
sysctl -w net.ipv4.conf.eth1.rp_filter=2
ip route flush cache
chmod +x /usr/local/sbin/mgmt-routing.sh

systemd 服务

nano /etc/systemd/system/mgmt-routing.service
[Unit]
Description=Management policy routing
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/mgmt-routing.sh
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable mgmt-routing.service
systemctl start mgmt-routing.service

最终验收清单


总结

这套方案解决的是一个真实生产级问题

在同一台 VM 上,同时满足「默认翻墙」与「公网管理入口稳定」。

关键不是代理软件,而是:

至此,这个坑算是真正填平了。

#PVE #ImmortalWrt #OpenWrt #Passwall #Policy Routing #Ip Rule #DNAT #Homelab