家庭异地组网技术方案

背景与需求

设备清单

  • 移动设备:手机×2(Android/iOS)、笔记本(Windows/macOS)
  • 家庭设备:家庭电脑(Windows)、NAS(群晖/威联通)— 内网 192.168.1.x
  • 办公设备:办公电脑(Windows)— 公司网络
  • 家庭网关:RT-AX86U(梅林 388+)、GL.iNet MT3000(OpenWrt)
  • 云端:云主机(Linux,公网)— 科学上网出口

核心诉求

  • 家庭电脑和 NAS 同内网
  • 手机和笔记本为移动网络
  • 云主机可科学上网,每年可能换服务器厂商(追求价格)
  • 家庭宽带目前没有公网IP

已测试设备

设备 状态
RT-AX56U V2 刷梅林后安装失败(没有U盘插口),弃用
极路由3(HC5861) 刷机成砖(Flash只有16M),废弃
GL.iNet MT3000 采购测试 OpenWrt
RT-AX86U(二手) 采购测试梅林 388+

方案决策

1
2
3
4
5
6
7
8
9
家庭宽带是否有公网IP?

┌───┴───┐
是 否
│ │
▼ ▼
场景A 场景B
RT-AX86U 云主机
作服务端 作服务端

网段规划

1
2
3
10.6.0.0/24   WireGuard 虚拟网段(服务端 .1,客户端 .2-.8)
192.168.1.0/24 家庭内网(家庭电脑 .3、NAS .8)
192.168.2.0/24 公司内网(办公电脑 .7)

注:如公司网段与家庭内网冲突,请自行调整。

场景A:有公网IP — 家庭网关作服务端

检查是否有公网IP

方法一:对比 WAN 口 IP 与公网 IP

  1. 登录路由器 Web 管理界面(192.168.1.1
  2. 进入 外部网络 → 连接状态
  3. 记录 WAN IP 地址(如 100.x.x.x
  4. 打开手机/电脑浏览器访问 https://ip.sbhttps://ifconfig.me
  5. 对比两个 IP:
    • 相同 → 有公网IP
    • 不同(如 10.x.x.x、172.16-31.x.x、192.168.x.x) → 无公网IP(NAT)

方法二:SSH 命令检查

1
2
3
4
5
# 在路由器 SSH 执行
curl -s ifconfig.me

# 或查看 WAN 口 IP
nvram get wan0_ipaddr

方法三:查看路由器 WAN 口 IP 段

常见私网 IP 段(无公网IP):

  • 10.0.0.0 - 10.255.255.255
  • 172.16.0.0 - 172.31.255.255
  • 192.168.0.0 - 192.168.255.255

如果 WAN 口 IP 在上述范围内,说明无公网IP。

方法四:致电运营商

如果以上方法都无法确认,直接拨打运营商客服电话询问:

  • “请问我的宽带是否有公网IP?”
  • 若有,询问能否提供固定 IP(对 DDNS 更友好)

常见情况

WAN 口 IP 公网IP 说明
100.x.x.x 直接公网IP
58.x.x.x 直接公网IP
10.x.x.x 运营商大内网
172.16-31.x.x 运营商大内网
192.168.x.x 路由器 NAT

架构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
                    ┌─────────────────────────┐
│ 公网 │
│ <你的公网IP>:51820 │
└───────────┬─────────────┘

┌───────────┴─────────────┐
│ 家庭网关 RT-AX86U │
│ WireGuard (10.6.0.1) │
│ LAN: 192.168.1.0/24 │
└───────────┬─────────────┘

┌───────────┴─────────────┐
│ 家庭电脑 (.3) NAS (.8)│
└───────────────────────────┘

移动网络:手机A (.4) 手机B (.5) 笔记本 (.6)
公司网络:办公电脑 (.7)
云主机(可选):科学上网出口,位于公网

梅林 388+ 配置步骤

前置条件:梅林固件 ≥ 388,已开启 SSH

Web 界面配置

  1. 登录 192.168.1.1
  2. 进入 VPN → WireGuard → 选择 作为服务器
  3. 勾选 启用 WireGuard VPN
  4. 监听端口:51820
  5. 地址:10.6.0.1/24

生成密钥(SSH)

1
2
3
4
ssh admin@192.168.1.1
cd /jffs/configs/wireguard
wg genkey | tee server_private.key | wg pubkey > server_public.key
cat server_public.key

添加客户端 Peer:Web 界面点击 添加 Peer,为每个客户端配置公钥和允许的IP(如 10.6.0.4/32),持久_keepalive 设为 25

命令行完整配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
cat > /jffs/configs/wireguard/wg0.conf << 'EOF'
[Interface]
PrivateKey = <server_private.key内容>
Address = 10.6.0.1/24
ListenPort = 51820
PostUp = sysctl -w net.ipv4.ip_forward=1
PostUp = iptables -A FORWARD -i %i -j ACCEPT
PostUp = iptables -A FORWARD -o %i -j ACCEPT
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT
PostDown = iptables -D FORWARD -o %i -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

[Peer]
PublicKey = <手机A公钥>
AllowedIPs = 10.6.0.4/32

[Peer]
PublicKey = <手机B公钥>
AllowedIPs = 10.6.0.5/32
# 更多 Peer...
EOF

chmod 600 /jffs/configs/wireguard/wg0.conf
wg-quick up /jffs/configs/wireguard/wg0.conf

开机自启

1
2
3
4
5
cat > /jffs/scripts/services-start << 'EOF'
#!/bin/sh
wg-quick up /jffs/configs/wireguard/wg0.conf
EOF
chmod +x /jffs/scripts/services-start

防火墙放行

1
iptables -I INPUT -p udp --dport 51820 -j ACCEPT

场景B:无公网IP — 云服务器作服务端

核心优势

现有网络完全不做改动,路由器保持原样。

  • 不需要改家庭路由器的任何配置
  • 不需要动光猫
  • 不需要申请端口映射
  • 当路由器不支持 WireGuard 时,在内网新增一个节点(工控机、旁路由、树莓派等)来跑 WireGuard

手头有什么用什么,灵活组合。

注意事项

WireGuard 默认端口 51820 容易被封,建议使用非标准端口:

  • 云主机:443、8443、53 等
  • 内网节点:任意可用的 UDP 端口

架构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
                        ┌─────────────────────────┐
│ 公网 │
│ <云服务器公网IP>:51820 │
│ 云主机 │
│ WireGuard (10.6.0.1) │
│ 科学上网出口 │
└───────────┬─────────────┘

┌───────────┴─────────────┐
│ 家庭网络(无公网IP) │
│ RT-AX86U 客户端 (.2) │
│ LAN: 192.168.1.0/24 │
│ 家庭电脑 (.3) NAS (.8)│
└───────────────────────────┘

移动网络:手机A (.4) 手机B (.5) 笔记本 (.6)
公司网络:办公电脑 (.7)

云主机配置

安装 WireGuard

1
2
3
4
apt update && apt install -y wireguard
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
ufw allow 51820/udp

生成密钥

1
2
3
4
5
6
cd /etc/wireguard
wg genkey | tee server_private.key | wg pubkey > server_public.key

for i in {1..6}; do
wg genkey | tee client${i}_private.key | wg pubkey > client${i}_public.key
done

服务端配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
cat > /etc/wireguard/wg0.conf << 'EOF'
[Interface]
PrivateKey = <server_private.key内容>
Address = 10.6.0.1/24
ListenPort = 51820
PostUp = sysctl -w net.ipv4.ip_forward=1
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostUp = iptables -A FORWARD -o wg0 -j ACCEPT
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostUp = ip route add 192.168.1.0/24 via 10.6.0.2

PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
PostDown = iptables -D FORWARD -o wg0 -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
PostDown = ip route del 192.168.1.0/24 via 10.6.0.2

[Peer]
PublicKey = <RT-AX86U公钥>
AllowedIPs = 10.6.0.2/32, 192.168.1.0/24
PersistentKeepalive = 25

[Peer]
PublicKey = <办公电脑公钥>
AllowedIPs = 10.6.0.3/32
PersistentKeepalive = 25

# 手机A/B/笔记本...
EOF

systemctl enable wg-quick@wg0
systemctl start wg-quick@wg0

梅林作为客户端

Web 界面

  1. VPN → WireGuard → 选择 作为客户端
  2. 服务器地址:<云主机IP>
  3. 服务器端口:51820
  4. 接口私钥:RT-AX86U 的客户端私钥
  5. 服务器公钥:云主机服务端公钥
  6. 地址:10.6.0.2/24
  7. AllowedIPs:0.0.0.0/0(全流量)或 10.6.0.0/24, 192.168.1.0/24(仅家庭流量)

SSH 命令行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
mkdir -p /jffs/configs/wireguard
cd /jffs/configs/wireguard

wg genkey | tee client_private.key | wg pubkey > client_public.key
cat client_public.key

cat > wg0.conf << 'EOF'
[Interface]
PrivateKey = <client_private.key内容>
Address = 10.6.0.2/24
DNS = 223.5.5.5

[Peer]
PublicKey = <云主机服务端公钥>
Endpoint = <云主机IP>:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
EOF

chmod 600 wg0.conf
wg-quick up wg0.conf

cat > /jffs/scripts/services-start << 'EOF'
#!/bin/sh
wg-quick up /jffs/configs/wireguard/wg0.conf
EOF
chmod +x /jffs/scripts/services-start

OpenWrt 配置

MT3000 作为客户端

LuCI Web 界面VPN → WireGuard → 添加新接口 wg0,配置私钥、IP 10.6.0.3/24,Peer 配置服务端公钥、端点、允许的IP 0.0.0.0/0

命令行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
ssh root@192.168.8.1

cd /etc/wireguard
wg genkey | tee private.key | wg pubkey > public.key

cat > /etc/config/network << 'EOF'
config interface 'wg0'
option proto 'wireguard'
option private_key '<MT3000私钥>'
list addresses '10.6.0.3/24'

config wireguard_wg0 'wgclient'
option public_key '<服务端公钥>'
option endpoint_host '<服务端IP>'
option endpoint_port '51820'
option route_allowed_ips '1'
option persistent_keepalive '25'
list allowed_ips '0.0.0.0/0'
EOF

uci add firewall rule
uci set firewall.@rule[-1].name='Allow-WireGuard'
uci set firewall.@rule[-1].src='wan'
uci set firewall.@rule[-1].dest_port='51820'
uci set firewall.@rule[-1].proto='udp'
uci set firewall.@rule[-1].target='ACCEPT'
uci commit firewall

/etc/init.d/network restart

Windows 客户端配置

下载:https://www.wireguard.com/install/

配置文件

1
2
3
4
5
6
7
8
9
10
[Interface]
PrivateKey = <客户端私钥>
Address = 10.6.0.4/24
DNS = 223.5.5.5

[Peer]
PublicKey = <服务端公钥>
Endpoint = <服务端地址>:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25

分流配置(仅访问家庭内网)

1
AllowedIPs = 10.6.0.0/24, 192.168.1.0/24

Tailscale 备用方案

梅林安装

Web 界面:体质管理 → 应用中心 → 安装 Tailscale

命令行:

1
2
3
4
ssh admin@192.168.1.1
tailscaled &
tailscale up --accept-dns=false
tailscale up --advertise-routes=192.168.1.0/24 --accept-dns=false

控制台放行路由:登录 https://login.tailscale.com/admin/machines ,找到 RT-AX86U → Edit route settings → 启用 192.168.1.0/24Approve

OpenWrt 安装

1
2
3
4
opkg update
opkg install tailscale
tailscaled &
tailscale up --advertise-routes=192.168.1.0/24 --accept-dns=false

常用命令

1
2
3
4
5
tailscale status    # 查看所有设备
tailscale ip # 查看本机虚拟IP
tailscale netcheck # 检测网络质量
tailscale up # 重新入网
tailscale down # 断开

方案对比

场景A(家庭网关) 场景B(云服务器)
公网IP 需要 不需要
延迟 中(经过云主机)
端口映射 需要 不需要
成本 无额外费用 需要云服务器费
科学上网 需额外配置 云主机作为出口

故障排查

连接问题

  • 服务端:wg show 检查是否运行
  • 客户端:检查防火墙 UDP 51820 是否开放
  • 内网访问:检查服务端 AllowedIPs 是否包含内网段

梅林排查

1
2
3
4
5
wg show
ip addr show
ip route show
iptables -L -n -v | grep wg0
logread | grep -i wireguard

OpenWrt 排查

1
2
3
/etc/init.d/network restart
wg show
iptables -t nat -L -n -v

云主机更换流程

每年换厂商时,客户端只需更新 [Peer] 中的 Endpoint

1
2
3
4
5
[Peer]
PublicKey = <服务端公钥>
Endpoint = <新服务器IP>:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25

Peer 公钥保持不变。

实施路径

场景A(有公网IP)

  1. 检查公网IP:curl -s ifconfig.me
  2. 配置梅林 DDNS(如需要)
  3. 启用 WireGuard 服务端
  4. 添加所有客户端 Peer
  5. 各设备安装 WireGuard 客户端连接

场景B(无公网IP)

  1. 购买云服务器,部署 WireGuard 服务端
  2. RT-AX86U 部署 WireGuard 客户端
  3. 各设备安装 WireGuard 客户端
  4. (可选)配置云服务器科学上网

云服务器推荐

按需选择,注意线路质量(CN2/BGP 对 WireGuard 性能影响较大)