同一 LAN 上两个 WSL2 实例 (W10) 之间的 Wireguard 连接

同一 LAN 上两个 WSL2 实例 (W10) 之间的 Wireguard 连接

我希望能够在同一 LAN 上的两个 WSL2 实例之间设置 Wireguard,但无法实现它们之间的连接。

我有以下设置:

  • Windows 网络 192.168.0.0/24
  • Wireguard 网络 10.0.0.0/24
  • 装有 WSL2 Ubuntu 20.04 的机器 1(W10 主机)
    • WSL2 网络 172.19.208.0/28
    • WSL2 IP 接口 172.19.209.104
    • WSL2 wireguard ip 10.0.0.2/24
  • 装有 WSL2 Ubuntu 20.04 的机器 2(W10 主机)
    • WSL2 网络 172.27.224.0/28
    • WSL2 IP 接口 172.27.234.66
    • WSL2 ip Wireguard 10.0.0.1/24

在机器 1 WSL2 上执行:wg-quick up wg0

机器 1 Wireguard 配置 /etc/wireguard/wg0.conf

[Interface]
PrivateKey = blablablalbal
ListenPort = 60709
Address = 10.0.0.2/24

[Peer]
PublicKey = blablablabal
Endpoint = 192.168.0.3:42659
AllowedIPs = 10.0.0.1/32

在机器2 WSL2上执行:wg-quick up wg0

机器 2 Wireguard 配置 /etc/wireguard/wg0.conf

[Interface]
PrivateKey = blablablalbal
ListenPort = 42659
Address = 10.0.0.1/32

[Peer]
PublicKey = blablablabal
Endpoint = 192.168.0.3:60709
AllowedIPs = 10.0.0.2/32

测试:

  • 从机器 1 ping 10.0.0.1 或从机器 2 ping 10.0.0.2 没有响应。
  • traceroute 没有信息
  • 两台机器上的 wg0 接口均已启动
  • 不同的 AllowedIPs(完整 Wireguard 范围/24..)
  • 从 Windows 主机到 WSL2 Wireguard 端口的 portproxy:
Machine 1

netsh interface portproxy add v4tov4 listenport=60709 listenaddress=192.168.0.3 connectport=60709 connectaddress=10.0.0.2
Machine 2

netsh interface portproxy add v4tov4 listenport=42659 listenaddress=192.168.0.4 connectport=42659 connectaddress=10.0.0.1

机器 1 WSL2 Ubuntu 上的路由

default via 172.19.208.1 dev eth0
10.0.0.0/24 dev wg0 proto kernel scope link src 10.0.0.2
172.19.208.0/20 dev eth0 proto kernel scope link src 172.19.209.104

机器 2 WSL2 Ubuntu 上的路由

default via 172.27.224.1 dev eth0
10.0.0.0/24 dev wg0 proto kernel scope link src 10.0.0.1
172.27.224.0/20 dev eth0 proto kernel scope link src 172.27.234.66

我认为我在路由方面缺少了一些东西,但我不知道还能做什么。有人能给我提示或帮助吗?

谢谢。

答案1

不要将 WireGuard 接口的监听端口代理到其自己的接口地址。这样不行。

使用 WSL2,将该端口代理到 VM 的虚拟 NIC 接口,就像在机器 1 上一样:

netsh interface portproxy add v4tov4 listenport=60709 listenaddress=192.168.0.3 connectport=60709 connectaddress=172.19.209.104

在机器 2 上:

netsh interface portproxy add v4tov4 listenport=42659 listenaddress=192.168.0.4 connectport=42659 connectaddress=172.27.234.66

另请参阅通过本地网络连接到 WSL2 服务器有关代理 WSL2 连接的各种其他方法和技巧的问题。

另请注意,您Endpoint在 WireGuard 配置中为机器 1 发布的地址是错误的 - 它应该使用机器 2 的 IP 地址,而不是机器 1 的 IP 地址:

Endpoint = 192.168.0.4:42659

相关内容