Featured image of post 基于 OpenWrt 和 WireGuard 的远程组网与运维方案

基于 OpenWrt 和 WireGuard 的远程组网与运维方案

用 Rocky VPS 作为 WireGuard Hub,将宿舍 OpenWrt、工位 Linux/Windows 单机接入同一私有网,并通过 OpenWrt 远程唤醒宿舍 Windows PC。

基于 OpenWrt 和 WireGuard 的远程组网与运维方案

1. 目标

用一台有公网入口的 Rocky Linux VPS 做 WireGuard Hub,把宿舍 OpenWrt、工位 Linux、工位 Windows 等设备接入同一个私有网段,并通过宿舍 OpenWrt 远程唤醒宿舍 Windows PC。

本文不是 WireGuard 原理课,而是一份部署和维护手册。以后忘了命令、要新增 peer、要排查握手和路由问题时,优先按本文检查。

当前策略:

  • VPS 只做 WireGuard Hub 和转发中心。
  • 宿舍 OpenWrt 接入 WireGuard,并代表宿舍 LAN 192.168.1.0/24
  • 工位 Linux/Windows 作为单设备 peer 接入,只宣告自己的 /32 地址。
  • 不再尝试在 RT-AC88U 386.14_2 上强行部署 WireGuard 主链路。
  • 旧 OpenVPN 可暂时保留,继续管理旧网段,等 WireGuard 稳定后再逐步替换。

注意:本文是公开博客版本,所有私钥、公钥、MAC 地址、VPS 公网 IP 都用占位符表示。真实值只应保存在对应设备本机配置中,不要放进 GitHub、截图或聊天记录。


2. 当前拓扑

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
VPS / Rocky Linux
  wg0: 10.77.0.1/24
  UDP: 51820
  PublicKey: VPS_PUBLIC_KEY

宿舍 OpenWrt
  wg0: 10.77.0.2/32
  LAN: 192.168.1.0/24
  LAN gateway: 192.168.1.1
  LAN device: br-lan
  PublicKey: DORM_OPENWRT_PUBLIC_KEY

工位 Linux / ThinkStation
  wg0: 10.77.0.101/32
  PublicKey: OFFICE_LINUX_PUBLIC_KEY

工位 Windows
  建议地址: 10.77.0.102/32
  类型: 单设备 peer,不宣告整个 192.168.50.0/24

旧 OpenVPN
  10.88.0.0/24
  192.168.50.0/24 可暂时继续由 OpenVPN 管理

推荐访问方式:

1
2
3
4
5
6
7
8
访问 VPS:          10.77.0.1
访问宿舍 OpenWrt: 10.77.0.2 或 192.168.1.1
访问工位 Linux:   10.77.0.101
访问工位 Windows: 10.77.0.102
访问宿舍 PC:      192.168.1.x

远程唤醒宿舍 PC:
  ssh root@10.77.0.2 /root/wake-dorm-pc.sh

3. 地址规划与设计原则

3.1 网段规划

当前网段:

1
2
3
4
WireGuard 主网段: 10.77.0.0/24
旧 OpenVPN 网段:  10.88.0.0/24
宿舍 LAN:         192.168.1.0/24
工位 LAN:         192.168.50.0/24

后续如果有条件统一调整 LAN,建议改成不容易冲突的编号:

1
2
宿舍 LAN: 192.168.201.0/24
工位 LAN: 192.168.202.0/24

但当前阶段不强行修改 LAN。远程组网最怕“边调边失联”,先把 WireGuard 跑稳,再做地址治理。

3.2 Hub-and-Spoke,而不是全互联

本方案采用:

1
所有 peer -> VPS Hub -> 其他 peer / 宿舍 LAN

理由:

  • VPS 有公网入口,最适合做固定 Endpoint。
  • 宿舍 OpenWrt 通常在 NAT 后面,主动连 VPS 更稳定。
  • 工位 Linux/Windows 作为单机 peer 管理更清晰,不把工位 LAN 路由责任塞给普通终端。
  • 新增 peer 只需要改 VPS 和新设备,不需要每台设备互相加配置。

3.3 AllowedIPs 的核心规则

WireGuard 的 AllowedIPs 同时承担两件事:

1
2
1. 这个 peer 允许使用哪些源 IP
2. 哪些目标 IP 应该发给这个 peer

因此它既像访问控制列表,又像路由表。配置错误时,经常表现为“有 handshake,但 ping 不通”。

规则:

  • VPS 上:哪个 peer 真正负责某个网段,哪个 peer 才能宣告这个网段。
  • 宿舍 OpenWrt:可以宣告 10.77.0.2/32192.168.1.0/24
  • 工位 Linux/Windows:只宣告自己的 10.77.0.x/32
  • 工位 Linux/Windows 不宣告 192.168.50.0/24,除非它真的是工位网关。
  • OpenWrt 客户端侧的 AllowedIPs 写“远端目标”,不要把自己的本地 LAN 写进去。

错误示例:

1
2
3
4
5
6
7
[Peer]
# dorm-openwrt
AllowedIPs = 10.77.0.2/32, 192.168.1.0/24

[Peer]
# dorm-laptop
AllowedIPs = 10.77.0.30/32, 192.168.1.0/24

这里两个 peer 同时宣告 192.168.1.0/24,会造成路由冲突。

正确示例:

1
2
3
4
5
6
7
[Peer]
# dorm-openwrt
AllowedIPs = 10.77.0.2/32, 192.168.1.0/24

[Peer]
# dorm-laptop
AllowedIPs = 10.77.0.30/32

4. VPS / Rocky Linux 服务端

4.1 安装与检查

1
2
3
4
5
sudo dnf install -y wireguard-tools

sudo modprobe wireguard
lsmod | grep wireguard
wg --version

正常应看到:

1
2
wireguard ...
wireguard-tools v...

4.2 生成服务端密钥

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
sudo install -d -m 700 /etc/wireguard

sudo bash -c '
set -e
umask 077

if [ ! -f /etc/wireguard/server_private.key ]; then
  wg genkey > /etc/wireguard/server_private.key
fi

wg pubkey < /etc/wireguard/server_private.key > /etc/wireguard/server_public.key

chmod 600 /etc/wireguard/server_private.key
chmod 644 /etc/wireguard/server_public.key
'

查看公钥:

1
sudo cat /etc/wireguard/server_public.key

注意:

1
2
3
server_private.key 只留在 VPS。
server_public.key 复制给各客户端。
如果私钥贴到聊天、文档或 GitHub,直接重新生成该 peer 的密钥。

4.3 VPS 配置 /etc/wireguard/wg0.conf

1
sudo nano /etc/wireguard/wg0.conf

推荐模板:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
[Interface]
# Rocky VPS WireGuard Hub
Address = 10.77.0.1/24
ListenPort = 51820
PrivateKey = VPS_PRIVATE_KEY
SaveConfig = false

[Peer]
# dorm-openwrt
PublicKey = DORM_OPENWRT_PUBLIC_KEY
AllowedIPs = 10.77.0.2/32, 192.168.1.0/24

[Peer]
# office-linux-thinkstation
PublicKey = OFFICE_LINUX_PUBLIC_KEY
AllowedIPs = 10.77.0.101/32

# [Peer]
# # office-windows
# PublicKey = OFFICE_WINDOWS_PUBLIC_KEY
# AllowedIPs = 10.77.0.102/32

建议固定:

1
SaveConfig = false

原因是配置文件才是真源。否则运行时的临时变更可能被 wg-quick 写回文件,长期维护时容易混乱。

4.4 开启 IPv4 转发

1
2
3
4
echo 'net.ipv4.ip_forward = 1' | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl --system

sysctl net.ipv4.ip_forward

期望:

1
net.ipv4.ip_forward = 1

4.5 配置 firewalld

放行 WireGuard 端口:

1
2
3
4
sudo firewall-cmd --add-port=51820/udp --permanent
sudo firewall-cmd --reload

sudo firewall-cmd --list-ports

建议把 wg0 放入 trusted zone,减少 VPN 内部转发被 firewalld 拦截的概率:

1
2
3
4
sudo firewall-cmd --permanent --zone=trusted --add-interface=wg0
sudo firewall-cmd --reload

sudo firewall-cmd --get-active-zones

如果 VPS 还有云厂商安全组,也要放行:

1
入站 UDP 51820

4.6 启动与维护

启动:

1
sudo systemctl enable --now wg-quick@wg0

重启:

1
sudo systemctl restart wg-quick@wg0

停止:

1
sudo systemctl stop wg-quick@wg0

查看状态:

1
2
3
sudo systemctl status wg-quick@wg0 --no-pager
sudo wg show
ip addr show wg0

查看关键路由:

1
ip route | grep -E '10\.77|10\.88|192\.168\.1|192\.168\.50|default'

正常状态应至少包含:

1
2
3
default via ... dev ens...
10.77.0.0/24 dev wg0 proto kernel scope link src 10.77.0.1
192.168.1.0/24 dev wg0

如果旧 OpenVPN 仍在,可能还会看到:

1
2
10.88.0.0/24 via ... dev tun0
192.168.50.0/24 via ... dev tun0

5. 宿舍 OpenWrt 配置

5.1 当前接口确认

宿舍 OpenWrt 当前 LAN:

1
2
3
4
5
6
7
network.lan.device='br-lan'
network.lan.ipaddr='192.168.1.1'
network.lan.netmask='255.255.255.0'

br-lan: 192.168.1.1/24
wg0:    10.77.0.2/32
wan:    pppoe-wan

后续 WOL 必须从 br-lan 发,不要从 wg0pppoe-wan 发。

5.2 安装 WireGuard

1
2
3
4
opkg update
opkg install wireguard-tools kmod-wireguard luci-proto-wireguard

wg --version

5.3 生成 OpenWrt 密钥

1
2
3
4
5
6
umask 077
wg genkey > /etc/wireguard_openwrt_private.key
wg pubkey < /etc/wireguard_openwrt_private.key > /etc/wireguard_openwrt_public.key

chmod 600 /etc/wireguard_openwrt_private.key
cat /etc/wireguard_openwrt_public.key

将输出的 DORM_OPENWRT_PUBLIC_KEY 填到 VPS 的 dorm-openwrt peer 中。

5.4 用 UCI 配置 wg0

先设置变量:

1
2
3
4
OPENWRT_PRIV="$(cat /etc/wireguard_openwrt_private.key)"
VPS_PUB="VPS_PUBLIC_KEY"
VPS_HOST="VPS_PUBLIC_IP_OR_DOMAIN"
VPS_PORT="51820"

配置接口和 peer:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
uci -q delete network.wg0
uci -q delete network.wg0_peer_vps

uci set network.wg0='interface'
uci set network.wg0.proto='wireguard'
uci set network.wg0.private_key="$OPENWRT_PRIV"
uci add_list network.wg0.addresses='10.77.0.2/32'

uci set network.wg0_peer_vps='wireguard_wg0'
uci set network.wg0_peer_vps.description='rocky-vps'
uci set network.wg0_peer_vps.public_key="$VPS_PUB"
uci set network.wg0_peer_vps.endpoint_host="$VPS_HOST"
uci set network.wg0_peer_vps.endpoint_port="$VPS_PORT"
uci set network.wg0_peer_vps.persistent_keepalive='25'
uci set network.wg0_peer_vps.route_allowed_ips='1'
uci add_list network.wg0_peer_vps.allowed_ips='10.77.0.0/24'

uci commit network
/etc/init.d/network reload

关键点:

1
2
3
OpenWrt 客户端侧 AllowedIPs 写远端目标。
宿舍本地 LAN 192.168.1.0/24 不写在 OpenWrt 对 VPS 的 AllowedIPs 里。
VPS 端才写:AllowedIPs = 10.77.0.2/32, 192.168.1.0/24

5.5 修改 OpenWrt AllowedIPs

查看:

1
uci show network.wg0_peer_vps

只保留 WireGuard 网段:

1
2
3
4
5
6
7
uci delete network.wg0_peer_vps.allowed_ips 2>/dev/null

uci add_list network.wg0_peer_vps.allowed_ips='10.77.0.0/24'
uci set network.wg0_peer_vps.route_allowed_ips='1'

uci commit network
/etc/init.d/network reload

如果将来某远端子网也通过 VPS 可达,再按需添加,例如:

1
2
3
uci add_list network.wg0_peer_vps.allowed_ips='192.168.50.0/24'
uci commit network
/etc/init.d/network reload

当前不建议把工位 192.168.50.0/24 交给 WireGuard,除非工位网关也接入 WireGuard。

5.6 配置 OpenWrt 防火墙

创建 vpn zone,并允许 lan <-> vpn

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
uci -q delete firewall.vpn
uci -q delete firewall.lan_to_vpn
uci -q delete firewall.vpn_to_lan

uci set firewall.vpn='zone'
uci set firewall.vpn.name='vpn'
uci add_list firewall.vpn.network='wg0'
uci set firewall.vpn.input='ACCEPT'
uci set firewall.vpn.output='ACCEPT'
uci set firewall.vpn.forward='ACCEPT'
uci set firewall.vpn.masq='0'
uci set firewall.vpn.mtu_fix='1'

uci set firewall.lan_to_vpn='forwarding'
uci set firewall.lan_to_vpn.src='lan'
uci set firewall.lan_to_vpn.dest='vpn'

uci set firewall.vpn_to_lan='forwarding'
uci set firewall.vpn_to_lan.src='vpn'
uci set firewall.vpn_to_lan.dest='lan'

uci commit firewall
/etc/init.d/firewall restart

检查:

1
uci show firewall | grep -E 'vpn|wg0'

5.7 OpenWrt 测试

在 OpenWrt 上:

1
2
3
4
5
wg show
ip addr show wg0
ip route | grep 10.77

ping -c 4 10.77.0.1

从 VPS 测:

1
2
ping -c 4 10.77.0.2
ping -c 4 192.168.1.1

如果 10.77.0.2 通,但 192.168.1.1 不通,优先查:

1
2
3
VPS 上 dorm-openwrt 的 AllowedIPs 是否包含 192.168.1.0/24
OpenWrt 防火墙是否允许 vpn -> lan
VPS 是否开启 net.ipv4.ip_forward

6. 工位 Linux 单设备 Peer

6.1 安装

Ubuntu / Debian:

1
2
sudo apt update
sudo apt install -y wireguard

Rocky / RHEL / Fedora:

1
sudo dnf install -y wireguard-tools

6.2 生成密钥

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
sudo install -d -m 700 /etc/wireguard

sudo bash -c '
set -e
umask 077

if [ ! -f /etc/wireguard/client_private.key ]; then
  wg genkey > /etc/wireguard/client_private.key
fi

wg pubkey < /etc/wireguard/client_private.key > /etc/wireguard/client_public.key

chmod 600 /etc/wireguard/client_private.key
chmod 644 /etc/wireguard/client_public.key
'

查看公钥:

1
sudo cat /etc/wireguard/client_public.key

把输出的 OFFICE_LINUX_PUBLIC_KEY 加到 VPS。

6.3 Linux 配置 /etc/wireguard/wg0.conf

1
sudo nano /etc/wireguard/wg0.conf

配置:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[Interface]
# office-linux-thinkstation
Address = 10.77.0.101/32
PrivateKey = OFFICE_LINUX_PRIVATE_KEY
SaveConfig = false

[Peer]
# rocky-vps
PublicKey = VPS_PUBLIC_KEY
Endpoint = VPS_PUBLIC_IP_OR_DOMAIN:51820
AllowedIPs = 10.77.0.0/24, 192.168.1.0/24
PersistentKeepalive = 25

说明:

  • 10.77.0.0/24 用于访问 WireGuard 内所有 peer。
  • 192.168.1.0/24 用于访问宿舍 LAN。
  • 如果只需要访问 WireGuard peer,不需要访问宿舍 LAN,可以删掉 192.168.1.0/24
  • 如果短期只在同一校园网内测试,可以临时把 Endpoint 写成 VPS 当前内网可达 IP;长期使用应改成 VPS 公网 IP 或稳定域名。

权限:

1
sudo chmod 600 /etc/wireguard/wg0.conf

启动:

1
sudo systemctl enable --now wg-quick@wg0

重启:

1
sudo systemctl restart wg-quick@wg0

检查:

1
2
3
sudo wg show
ip addr show wg0
ip route | grep -E '10\.77|192\.168\.1'

测试:

1
2
3
ping -c 4 10.77.0.1
ping -c 4 10.77.0.2
ping -c 4 192.168.1.1

7. Windows 单设备 Peer

7.1 WireGuard 客户端配置

安装 Windows 官方 WireGuard 客户端后:

1
Add Tunnel -> Add empty tunnel

客户端会自动生成 PrivateKeyPublicKey

Windows 配置模板:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[Interface]
# office-windows
PrivateKey = OFFICE_WINDOWS_PRIVATE_KEY
Address = 10.77.0.102/32

[Peer]
# rocky-vps
PublicKey = VPS_PUBLIC_KEY
Endpoint = VPS_PUBLIC_IP_OR_DOMAIN:51820
AllowedIPs = 10.77.0.0/24, 192.168.1.0/24
PersistentKeepalive = 25

VPS 上添加:

1
2
3
4
[Peer]
# office-windows
PublicKey = OFFICE_WINDOWS_PUBLIC_KEY
AllowedIPs = 10.77.0.102/32

应用 VPS 配置:

1
sudo systemctl restart wg-quick@wg0

7.2 Windows 测试

PowerShell:

1
2
3
ping 10.77.0.1
ping 10.77.0.2
ping 192.168.1.1

注意:

1
2
3
Windows 防火墙经常默认拦 ICMP。
别人 ping Windows 的 10.77.0.102 不通,不一定代表 WireGuard 错。
RDP、SSH、HTTP 等实际服务端口测试更可靠。

8. Clash / v2ray TUN 与 WireGuard 共存

8.1 问题现象

TUN 模式代理可能截获 WireGuard 外层 UDP 流量:

1
本机 -> VPS_PUBLIC_IP:51820/udp

如果这条流量被代理接管,常见现象:

1
2
3
4
WireGuard latest handshake 不更新
transfer 只有 sent,没有 received
ping 10.77.0.1 不通
开 TUN 就断,关 TUN 就恢复

8.2 规则原则

必须 DIRECT

1
2
3
4
5
6
7
VPS_PUBLIC_IP/32
UDP 51820
10.77.0.0/24
10.88.0.0/24
192.168.1.0/24
192.168.50.0/24
RFC1918 私网

Clash 规则示例:

1
2
3
4
5
6
7
8
rules:
  - IP-CIDR,VPS_PUBLIC_IP/32,DIRECT,no-resolve
  - IP-CIDR,10.77.0.0/24,DIRECT
  - IP-CIDR,10.88.0.0/24,DIRECT
  - IP-CIDR,192.168.1.0/24,DIRECT
  - IP-CIDR,192.168.50.0/24,DIRECT
  - GEOIP,PRIVATE,DIRECT
  - MATCH,PROXY

建议 WireGuard Endpoint 用 IP,不用域名:

1
Endpoint = VPS_PUBLIC_IP:51820

这样可以避免 DNS 查询被 TUN 代理影响。若必须用域名,则 DNS 也要确认不被代理规则错误接管。


9. Wake-on-LAN 远程唤醒宿舍 PC

9.1 正确链路

推荐链路:

1
2
3
4
5
6
7
远端设备
  -> WireGuard
  -> VPS
  -> WireGuard
  -> 宿舍 OpenWrt 10.77.0.2
  -> OpenWrt 在 br-lan 发送 WOL 魔术包
  -> 唤醒宿舍 Windows PC

不要优先尝试远端直接向 192.168.1.255 广播。WireGuard 是三层隧道,广播不一定穿透。最稳的是让 OpenWrt 在宿舍 LAN 本地发 WOL。

9.2 Windows PC 设置

BIOS / UEFI:

1
2
3
Wake on LAN: Enabled
Power On By PCI-E / Resume By PCI-E Device: Enabled
ErP / EuP / Deep Sleep: Disabled

MSI 主板进 BIOS:

1
2
开机连续按 Delete
或 Windows 执行:shutdown /r /fw /t 0

Windows 网卡设置:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
设备管理器 -> 网络适配器 -> 有线网卡 -> 属性

电源管理:
  允许此设备唤醒计算机:开启
  只允许魔术封包唤醒计算机:开启

高级:
  Wake on Magic Packet:Enabled
  Shutdown Wake-On-Lan:Enabled
  Wake from S5:Enabled
  Wake on Pattern Match:建议关闭或不依赖

如果要从关机状态唤醒,建议关闭 Fast Startup:

1
控制面板 -> 电源选项 -> 选择电源按钮的功能 -> 更改当前不可用设置 -> 取消“启用快速启动”

如果只从睡眠唤醒,可以先不关 Fast Startup,先把最小链路测通。

9.3 OpenWrt 安装 WOL 工具

1
2
opkg update
opkg install etherwake luci-app-wol

只用命令行:

1
opkg install etherwake

9.4 查询 Windows PC MAC

Windows:

1
ipconfig /all

找有线网卡:

1
Physical Address / 物理地址

OpenWrt 上也可以查:

1
2
cat /tmp/dhcp.leases
ip neigh show dev br-lan

MAC 格式统一写成:

1
AA:BB:CC:DD:EE:FF

9.5 手动唤醒

OpenWrt 上执行:

1
etherwake -i br-lan AA:BB:CC:DD:EE:FF

宿舍 LAN 接口是:

1
br-lan

不要从 wg0pppoe-wan 发 WOL。

9.6 LuCI WOL 设置

如果使用 LuCI:

1
2
3
4
服务 -> Wake on LAN
Interface: br-lan
MAC: Windows 有线网卡 MAC
Send to broadcast address: 开启

建议开启 Send to broadcast address

理由:

1
2
PC 睡眠或关机后,OpenWrt 可能没有它的 ARP 记录。
广播发送更容易被网卡收到。

不要做:

1
2
不要从 WAN 端口转发 UDP 7/9 到 LAN
不要允许公网直接发 WOL 广播

9.7 固定唤醒脚本

1
2
3
4
5
6
cat > /root/wake-dorm-pc.sh <<'EOF'
#!/bin/sh
etherwake -i br-lan AA:BB:CC:DD:EE:FF
EOF

chmod +x /root/wake-dorm-pc.sh

远程唤醒:

1
ssh root@10.77.0.2 /root/wake-dorm-pc.sh

宿舍内网唤醒:

1
ssh root@192.168.1.1 /root/wake-dorm-pc.sh

9.8 WOL 排障顺序

先测试睡眠唤醒:

1
2
Windows PC 睡眠
OpenWrt 执行 etherwake

再测试关机唤醒。

如果睡眠能醒、关机不能醒,优先检查:

1
2
3
4
5
BIOS ErP 是否关闭
BIOS Resume By PCI-E 是否开启
Windows Fast Startup 是否关闭
网卡高级选项 Shutdown Wake-On-Lan 是否开启
PC 是否使用有线网卡

如果完全不能醒,优先检查:

1
2
3
4
MAC 是否是有线网卡 MAC
PC 是否接在 br-lan 下
网口关机后灯是否亮
网线、交换机、路由器 LAN 口是否正常

10. 常用排障命令

10.1 WireGuard 通用检查

1
2
3
sudo wg show
ip addr show wg0
ip route

重点看:

1
2
3
4
latest handshake: 是否更新
transfer: 是否双向增长
allowed ips: 是否正确
endpoint: 是否是正确 VPS 地址

典型问题:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
只有 sent,没有 received:
  对方没添加 peer
  公钥不匹配
  Endpoint 不通
  防火墙或云安全组没放行 UDP 51820

有 handshake,但 ping 不通:
  路由或 AllowedIPs 错
  IP 转发没开
  firewalld / OpenWrt 防火墙拦截
  Windows 防火墙拦 ICMP

10.2 VPS 检查

1
2
3
4
5
6
sudo wg show
ip route | grep -E '10\.77|10\.88|192\.168\.1|192\.168\.50|default'
sysctl net.ipv4.ip_forward
sudo firewall-cmd --list-ports
sudo firewall-cmd --get-active-zones
sudo systemctl status wg-quick@wg0 --no-pager

日志:

1
sudo journalctl -u wg-quick@wg0 -n 100 --no-pager

应用配置:

1
sudo systemctl restart wg-quick@wg0

10.3 OpenWrt 检查

1
2
3
4
5
6
wg show
ip addr show wg0
ip route | grep -E '10\.77|192\.168\.1'
uci show network.wg0
uci show network.wg0_peer_vps
uci show firewall | grep -E 'vpn|wg0'

重载网络:

1
/etc/init.d/network reload

重启防火墙:

1
/etc/init.d/firewall restart

10.4 Linux peer 检查

1
2
3
4
5
6
sudo wg show
ip addr show wg0
ip route | grep -E '10\.77|192\.168\.1'
ping -c 4 10.77.0.1
ping -c 4 10.77.0.2
ping -c 4 192.168.1.1

重启:

1
sudo systemctl restart wg-quick@wg0

10.5 连通性测试顺序

从工位 Linux 测:

1
2
3
ping -c 4 10.77.0.1    # VPS
ping -c 4 10.77.0.2    # 宿舍 OpenWrt WG
ping -c 4 192.168.1.1  # 宿舍 OpenWrt LAN

从 VPS 测:

1
2
3
ping -c 4 10.77.0.2
ping -c 4 192.168.1.1
ping -c 4 10.77.0.101

从宿舍 OpenWrt 测:

1
2
ping -c 4 10.77.0.1
ping -c 4 10.77.0.101

判断顺序:

1
2
3
1. 先确认 peer 到 VPS 的 10.77.0.1 通。
2. 再确认 peer 之间的 10.77.0.x 通。
3. 最后确认跨到宿舍 LAN 的 192.168.1.0/24 通。

不要一开始就测 RDP、SSH 或 WOL。先把三层连通性验证完。


11. 新增 peer 标准流程

11.1 选择 IP

建议:

1
2
3
4
手机:       10.77.0.10/32
笔记本:     10.77.0.11/32
工位 Linux: 10.77.0.101/32
工位 Win:   10.77.0.102/32

11.2 在新设备生成密钥

Linux:

1
2
3
umask 077
wg genkey > private.key
wg pubkey < private.key > public.key

Windows / macOS / 手机:

1
2
WireGuard App -> Add empty tunnel
自动生成 PrivateKey / PublicKey

11.3 VPS 添加 peer

1
2
3
4
[Peer]
# device-name
PublicKey = DEVICE_PUBLIC_KEY
AllowedIPs = 10.77.0.x/32

重启 VPS:

1
sudo systemctl restart wg-quick@wg0

11.4 客户端配置

单设备客户端模板:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[Interface]
Address = 10.77.0.x/32
PrivateKey = DEVICE_PRIVATE_KEY
SaveConfig = false

[Peer]
PublicKey = VPS_PUBLIC_KEY
Endpoint = VPS_PUBLIC_IP:51820
AllowedIPs = 10.77.0.0/24, 192.168.1.0/24
PersistentKeepalive = 25

如果只想访问 WireGuard peer,不访问宿舍 LAN:

1
AllowedIPs = 10.77.0.0/24

如果想全局代理,才写:

1
AllowedIPs = 0.0.0.0/0, ::/0

当前不建议默认全局代理。这个方案的目标是远程组网和运维,不是替代所有代理流量。


12. 安全注意事项

12.1 私钥管理

1
2
3
4
PrivateKey 永远只留在本机。
PublicKey 可以复制给 VPS。
不要把 PrivateKey 发到聊天、GitHub、博客、截图。
如果私钥泄露,重新生成该 peer 的密钥,并更新 VPS 上的 PublicKey。

12.2 公网暴露面

VPS 只需要暴露:

1
UDP 51820

不要为了 WOL 暴露:

1
2
3
UDP 7
UDP 9
192.168.1.255 广播转发

WOL 只从 OpenWrt 的 br-lan 本地发。公网只允许进入 WireGuard。

12.3 普通 LAN 设备是否需要安装 WireGuard

宿舍 192.168.1.0/24 下的普通设备不需要安装 WireGuard,也可以访问 10.77.0.0/24,前提是:

1
2
3
4
默认网关是 192.168.1.1
OpenWrt route_allowed_ips=1
OpenWrt 防火墙允许 lan -> vpn
VPS 允许转发

如果普通设备自己也安装 WireGuard,可以,但它只能宣告自己的 /32,不能宣告整个 LAN。

12.4 不要让普通终端假装网关

工位 Linux/Windows 如果只是单设备 peer,就只写:

1
2
AllowedIPs = 10.77.0.101/32
AllowedIPs = 10.77.0.102/32

不要写:

1
AllowedIPs = 192.168.50.0/24

除非这台设备真的是工位 LAN 的默认网关,并且已经配置好 IP 转发、防火墙和回程路由。


13. 当前推荐状态总结

应保持:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
VPS:
  10.77.0.1/24
  peer dorm-openwrt: 10.77.0.2/32, 192.168.1.0/24
  peer office-linux: 10.77.0.101/32
  后续 peer office-windows: 10.77.0.102/32

OpenWrt:
  wg0 10.77.0.2/32
  LAN 192.168.1.1/24 on br-lan
  AllowedIPs to VPS: 10.77.0.0/24
  firewall: lan <-> vpn

Office Linux:
  wg0 10.77.0.101/32
  AllowedIPs: 10.77.0.0/24, 192.168.1.0/24

Office Windows:
  wg0 10.77.0.102/32
  AllowedIPs: 10.77.0.0/24, 192.168.1.0/24

WOL:
  OpenWrt br-lan 发 etherwake
  远程命令: ssh root@10.77.0.2 /root/wake-dorm-pc.sh

不要做:

1
2
3
4
5
6
不要让 RT-AC88U 386.14_2 强行跑 WireGuard 主链路。
不要把 192.168.50.0/24 交给工位 Linux/Windows 单设备 peer。
不要让 Clash/v2ray TUN 代理 VPS:51820 的 WireGuard 外层流量。
不要从公网转发 UDP 7/9 做 WOL。
不要在多个 peer 上重复宣告同一个 LAN 网段。
不要把 PrivateKey、真实 MAC、VPS 公网 IP 随手贴到公开文档。

14. 一页速查

VPS:

1
2
3
4
5
6
sudo wg show
sudo systemctl restart wg-quick@wg0
ip route | grep -E '10\.77|192\.168\.1|default'
sysctl net.ipv4.ip_forward
sudo firewall-cmd --list-ports
sudo firewall-cmd --get-active-zones

OpenWrt:

1
2
3
4
5
6
wg show
ip route | grep -E '10\.77|192\.168\.1'
uci show network.wg0_peer_vps
uci show firewall | grep -E 'vpn|wg0'
/etc/init.d/network reload
/etc/init.d/firewall restart

Linux peer:

1
2
3
4
5
sudo wg show
ip route | grep -E '10\.77|192\.168\.1'
ping -c 4 10.77.0.1
ping -c 4 10.77.0.2
ping -c 4 192.168.1.1

WOL:

1
ssh root@10.77.0.2 /root/wake-dorm-pc.sh

如果只记一个排障原则:

1
2
3
4
先看 latest handshake 和 transfer 是否双向增长;
再看 VPS AllowedIPs;
再看 IP 转发;
最后看两端防火墙和本机代理 TUN 规则。
潇洒人间一键仙
使用 Hugo 构建
主题 StackJimmy 设计