WSL_Windows10_x16_network_setup

Windows 10 + WSL2 访问 RT-Thread x16 开发板网络配置与排查记录

适用场景:Windows 10 + WSL2 + Hyper-V + RT-Thread x16 开发板。

本文记录一次完整的 WSL2 网络排查过程,以及最终验证成功的方案,便于以后重装、迁移或再次遇到类似问题时快速恢复。


1. 问题背景

目标是在 Windows 10 主机上使用 WSL2 进行 x16 开发,并让 WSL2 可以直接访问 RT-Thread x16 开发板。

开发板信息:

1
x16 IP:202.197.27.110/24

Windows 物理网卡为 Realtek,原来在开发网络中的地址为:

1
Windows:202.197.27.116/24

希望最终达到:

1
WSL → x16:202.197.27.117 → 202.197.27.110

同时又不影响 WSL 原来的 Internet 能力。


2. 最初的问题现象

最开始 WSL2 使用默认 NAT 网络。

典型状态:

1
2
WSL eth0:172.30.216.169/20
默认网关:172.30.208.1

后来因为 WSL NAT DHCP 动态变化,也出现过:

1
WSL eth0:172.30.221.198/20

对应路由类似:

1
2
default via 172.30.208.1 dev eth0
172.30.208.0/20 dev eth0

WSL 可以正常:

  • 访问 WSL NAT 网关
  • 访问 Windows 主机
  • 访问 Internet,例如 8.8.8.8
  • 使用 curl https://www.baidu.com

但是:

1
ping 202.197.27.110

无法访问 x16。

典型现象

1
WSL → 202.197.27.110:失败

而 Windows 主机本身:

1
ping -S 202.197.27.116 202.197.27.110

可以成功。

因此问题不是 x16 本身、也不是 Windows 到 x16 的物理链路问题,而是:

WSL2 默认 NAT 网络无法在当前 Windows 多网络环境下直接到达 202.197.27.0/24 开发网段。


3. Windows 当前网络环境

Windows 上当时存在多个网络接口,例如:

  • Realtek 物理网卡 以太网
  • VMware VMnet1 / VMnet8
  • 以太网 2
  • Meta Tunnel
  • Sangfor VNIC
  • Hyper-V 虚拟网卡

Windows 路由中还存在多个默认路由,部分接口 metric 很低,因此不能简单假设“所有目标流量都从 Realtek 走”。

这一点是本次排查比较重要的背景。


4. 如何查看 WSL 网络配置

4.1 查看 WSL 网卡地址

在 WSL 内:

1
ip addr

只看 eth0:

1
ip addr show eth0

例如默认 NAT:

1
2
eth0:
    inet 172.30.221.198/20

4.2 查看 WSL 路由

1
ip route

正常 NAT 模式类似:

1
2
default via 172.30.208.1 dev eth0
172.30.208.0/20 dev eth0 proto kernel scope link src 172.30.221.198

4.3 查看指定目标的实际出口

这是判断路由问题非常有用的命令:

1
ip route get 202.197.27.110

可以看到:

  • 下一跳
  • 出口接口
  • 源地址

本次排查中它显示 x16 流量先走 WSL NAT 网关:

1
via 172.30.208.1

说明没有直接进入 202.197.27.0/24 这个物理开发网络。

4.4 查看链路

可以使用:

1
tracepath 202.197.27.110

本次曾观察到 WSL 第一跳是 Windows 主机,然后后续没有得到 x16 的响应。


5. 如何查看 Windows 网络配置

5.1 查看网络适配器

PowerShell:

1
2
Get-NetAdapter |
    Format-Table Name, InterfaceDescription, Status, ifIndex, MacAddress

5.2 查看 IPv4 地址

1
2
Get-NetIPAddress -AddressFamily IPv4 |
    Format-Table InterfaceAlias, IPAddress, PrefixLength, SkipAsSource

查看指定接口:

1
Get-NetIPAddress -InterfaceAlias "以太网" -AddressFamily IPv4

5.3 查看 Windows 路由

1
route print

或:

1
Get-NetRoute -AddressFamily IPv4

5.4 查看 Windows IP 配置

1
Get-NetIPConfiguration

5.5 查看 Hyper-V 虚拟交换机

1
2
Get-VMSwitch |
    Format-Table Name, SwitchType, NetAdapterName

最终环境中为:

1
2
3
Default Switch   Internal
WSL-External     External
WSL              Internal

6. 初期尝试:SkipAsSource

Windows Realtek 网卡当时拥有两个 IPv4:

1
2
202.197.27.116/24
192.168.23.88/24

曾经尝试把:

1
192.168.23.88

设置为:

1
SkipAsSource=True

目的是减少多地址网卡导致的源地址/路由选择干扰。

但这个修改没有解决:

1
WSL → 202.197.27.110

仍然无法访问。

因此可以得出:

SkipAsSource 不是本问题的核心解决方案。

后来 Hyper-V External Switch 建立后,Windows 的 IP 拓扑发生了变化,原来的 192.168.23.88 已不再存在于当前 IPv4 配置中,因此没有再强行恢复。


7. 初期尝试:WSL mirrored 模式

曾经尝试使用:

1
2
3
4
[wsl2]
networkingMode=mirrored
dnsTunneling=true
autoProxy=true

但是当前环境为:

1
Windows 10 22H2 / build 19045.6466

在该环境下,WSL mirrored networking 不可用。

因此 mirrored 模式放弃。


8. 初期尝试:WSL bridged 模式

曾经进一步尝试:

1
2
3
4
[wsl2]
networkingMode=bridged
vmSwitch=WSL-External
dhcp=true

然后:

1
2
wsl --shutdown
wsl

进入 WSL 后发现:

1
ip addr show eth0

只有类似:

1
eth0: UP

但是:

  • 没有 IPv4 地址
  • ip route 为空

执行:

1
ping 202.197.27.110

直接得到:

1
Network is unreachable

因此该方法在本次 Windows 10 + WSL 版本环境中没有正常工作。

结论

本次没有继续使用:

1
networkingMode=bridged

最终仍然保持 WSL 的默认 NAT 模式。


9. 启用 Hyper-V

为了实现稳定的外部网络接入,启用了 Hyper-V。

执行:

1
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All -All

然后重启 Windows。

启用后确认:

1
Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All

可以看到 Hyper-V 相关组件为 Enabled。

注意

这不是临时调试配置,因为最终方案确实依赖 Hyper-V External Switch,因此应该保留。


10. 创建 Hyper-V External Switch

使用 Realtek 物理网卡创建 External vSwitch:

1
2
3
4
New-VMSwitch `
    -Name "WSL-External" `
    -NetAdapterName "以太网" `
    -AllowManagementOS $true

然后确认:

1
Get-VMSwitch

应看到:

1
WSL-External   External   Realtek PCIe GbE Family Controller

重要现象

建立 External Switch 后,Windows 的开发网络 IPv4:

1
202.197.27.116/24

最终位于:

1
vEthernet (WSL-External)

而不是物理网卡 以太网 本身。

这是 Hyper-V External Switch 的正常结构,不能误认为配置丢失。


11. 为什么 Get-VM 看不到 WSL

执行:

1
Get-VM

没有看到 WSL 虚拟机。

这是正常的。

WSL2 使用动态创建的虚拟机,不能按普通 Hyper-V VM 的方式直接通过 Get-VM 管理。

可以使用:

1
hcsdiag list

查看当前 WSL VM。

本次得到的 WSL VM ID 类似:

1
425FC408-42AF-412D-9EBD-689B51882B65

12. 查看 WSL 自动创建的虚拟网络

Windows 中最终可以看到:

1
2
3
4
5
6
7
8
vEthernet (WSL)
    Hyper-V Virtual Ethernet Adapter

vEthernet (WSL-External)
    Hyper-V Virtual Ethernet Adapter

vEthernet (Default Switch)
    Hyper-V Virtual Ethernet Adapter

其中:

1
2
3
WSL                    → Internal
WSL-External            → External
Default Switch          → Internal

不要做的事情

不要试图直接把:

1
WSL

这个 Internal Switch 删除或改成 External。

它是 WSL 默认 NAT 网络的一部分。


13. 最终解决方案:WSL 保持 NAT + 增加第二块外部网卡

最终采用的是双网卡结构:

1
2
3
4
5
WSL eth0
    ↓
WSL NAT
    ↓
Internet

同时:

1
2
3
4
5
6
7
8
9
WSL eth1
    ↓
WSL-External
    ↓
Realtek
    ↓
202.197.27.0/24
    ↓
x16

也就是:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
            Windows Realtek
              202.197.27.116
                    │
           Hyper-V WSL-External
                    │
             ┌──────┴──────┐
             │             │
      Windows Host      WSL eth1
      .27.116            .27.117
                             │
                           x16
                         .27.110

WSL eth0
172.30.x.x
    │
    └── WSL NAT → Internet

这样既保留 WSL Internet,又能直接访问 x16。


14. 使用 WSLAttachSwitch 把 WSL 接入 External Switch

由于 WSL2 的 VM 是动态创建的,采用专门工具:

1
WSLAttachSwitch.exe

本次文件位置:

1
G:\tools\WSLAttachSwitch.exe

第一次执行:

1
G:\tools\WSLAttachSwitch.exe "WSL-External"

然后保存参数:

1
G:\tools\WSLAttachSwitch.exe "WSL-External" --save-params

保存后,后续可直接:

1
G:\tools\WSLAttachSwitch.exe

工具输出过:

1
2
Endpoint already attached to current WSL2 VM.
Parameters saved, will be reused when the tool is invoked without a network provided.

说明当前 WSL VM 已经附加到了指定网络,并且参数已经保存。


15. 附加成功后的 WSL 网卡

附加后,WSL 中出现:

1
ip link

典型结果:

1
2
eth0
eth1

其中:

1
2
eth0 → 原来的 WSL NAT
eth1 → WSL-External

刚附加时 eth1 可能是:

1
state DOWN

这是正常的,后面需要手工或脚本配置。


16. 给 eth1 配置固定开发板网络

最终固定使用:

1
2
WSL eth1 = 202.197.27.117/24
x16      = 202.197.27.110/24

执行:

1
2
sudo ip link set eth1 up
sudo ip addr add 202.197.27.117/24 dev eth1

注意:

不需要给 eth1 添加默认网关。

因为:

1
2
eth0 → 默认路由 → Internet
eth1 → 202.197.27.0/24 → x16

Linux 会自动生成:

1
202.197.27.0/24 dev eth1

因此不需要额外添加:

1
202.197.27.110/32

本次曾经测试过:

1
sudo ip route add 202.197.27.110/32 dev eth1

之后删除:

1
sudo ip route del 202.197.27.110/32 dev eth1

删除后仍然可以正常 ping x16,证明 /24 路由已经足够。


17. 最终 WSL 路由

最终类似:

1
2
3
default via 172.30.208.1 dev eth0
172.30.208.0/20 dev eth0 proto kernel scope link src 172.30.221.198
202.197.27.0/24 dev eth1 proto kernel scope link src 202.197.27.117

这个路由结构非常清晰:

1
2
Internet      → eth0
x16 网段      → eth1

18. 验证结果

18.1 WSL → x16

1
ping -c 4 202.197.27.110

成功,实测延迟约:

1
0.2 ~ 0.4 ms

18.2 WSL → Internet

1
ping -c 4 8.8.8.8

成功。

18.3 删除多余 /32 路由后

1
2
sudo ip route del 202.197.27.110/32 dev eth1
ping -c 4 202.197.27.110

仍然成功。

说明最终网络配置是正确的。


19. 最终自动化方案

为了以后使用方便,最终没有选择 Windows 开机自动任务,而是采用:

需要 x16 开发时,双击一个 BAT 文件。

原因:

  • 简单
  • 可控
  • WSL / Hyper-V 启动时序更容易管理
  • 不需要 Windows 一启动就常驻 WSL 网络环境
  • 不需要任务计划程序

20. WSL 内网络配置脚本

文件:

1
/usr/local/sbin/x16-network.sh

内容:

1
2
3
4
5
6
#!/bin/bash

ip link set eth1 up

ip addr flush dev eth1
ip addr add 202.197.27.117/24 dev eth1

设置可执行:

1
sudo chmod +x /usr/local/sbin/x16-network.sh

执行:

1
sudo /usr/local/sbin/x16-network.sh

为什么使用 ip addr flush dev eth1

因为 WSLAttachSwitch 后 eth1 的状态可能在不同启动过程中略有差异。

先清理该接口上的旧 IPv4,再配置固定地址,可以避免重复执行时报:

1
RTNETLINK answers: File exists

不会影响 eth0。


21. Windows 侧一键启动 BAT

文件:

1
G:\tools\start_x16_wsl.bat

最终内容:

 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@echo off
chcp 65001 >nul
echo ========================================
echo   WSL x16 Network Startup
echo ========================================

echo.
echo [1/3] Starting WSL...
wsl.exe -d Ubuntu-22.04 --exec /bin/true

if errorlevel 1 (
    echo [ERROR] Failed to start WSL.
    pause
    exit /b 1
)

timeout /t 3 /nobreak >nul

echo.
echo [2/3] Attaching WSL to WSL-External...
"G:\tools\WSLAttachSwitch.exe"

if errorlevel 1 (
    echo [ERROR] WSLAttachSwitch failed.
    pause
    exit /b 1
)

timeout /t 1 /nobreak >nul

echo.
echo [3/3] Configuring WSL eth1...
wsl.exe -d Ubuntu-22.04 -- sudo /usr/local/sbin/x16-network.sh

if errorlevel 1 (
    echo [ERROR] Failed to configure eth1.
    pause
    exit /b 1
)

echo.
echo ========================================
echo   WSL x16 network started successfully.
echo ========================================
echo.
echo WSL eth1: 202.197.27.117/24
echo x16:      202.197.27.110
echo.
pause

执行流程

双击:

1
G:\tools\start_x16_wsl.bat

依次执行:

1
2
3
4
5
6
7
8
9
1. 启动 Ubuntu-22.04
        ↓
2. WSLAttachSwitch → WSL-External
        ↓
3. WSL 出现 eth1
        ↓
4. sudo x16-network.sh
        ↓
5. eth1 = 202.197.27.117/24

22. sudo 密码问题

BAT 中调用:

1
wsl.exe -d Ubuntu-22.04 -- sudo /usr/local/sbin/x16-network.sh

没有把 Linux 密码写入 BAT。

如果 sudo 凭据缓存已经失效,执行时会在窗口中要求输入:

1
[sudo] password for xunyingya:

输入密码即可继续。

目前没有配置:

1
NOPASSWD

也没有把密码明文保存到脚本中。

这是有意保留的简单方案。


23. .wslconfig 最终配置

最终不要再使用:

1
networkingMode=bridged

也不要依赖:

1
vmSwitch=WSL-External

最终采用 WSL 默认 NAT + WSLAttachSwitch 增加第二网卡,因此建议:

1
2
3
4
[wsl2]
networkingMode=nat
dnsTunneling=true
autoProxy=true

其中:

1
networkingMode=nat

是当前方案的核心。


24. 为什么不删除 vEthernet (WSL)

Windows 中还会看到:

1
2
3
vEthernet (WSL)
vEthernet (WSL-External)
vEthernet (Default Switch)

这些不应该为了“清理”而删除。

最终用途分别是:

1
2
3
4
5
6
7
8
vEthernet (WSL)
    → WSL eth0 → NAT → Internet

vEthernet (WSL-External)
    → WSL eth1 → x16 开发网络

vEthernet (Default Switch)
    → Hyper-V / WSL 默认虚拟网络组件

25. Windows 上的最终 IP 结构

Hyper-V External Switch 建立后,Windows 的开发地址最终位于:

1
2
vEthernet (WSL-External)
    202.197.27.116/24

而不是:

1
以太网

物理 Realtek 网卡本身因此可能不再直接显示 IPv4,这是 Hyper-V External Switch 的正常表现。


26. 最终完整网络拓扑

 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
                           Windows 10
                               │
                  Realtek PCIe GbE Family Controller
                               │
                    Hyper-V External Switch
                         WSL-External
                               │
               ┌───────────────┴───────────────┐
               │                               │
      Windows vEthernet                   WSL eth1
      202.197.27.116/24                  202.197.27.117/24
               │                               │
               │                               │
               └───────────────┬───────────────┘
                               │
                         202.197.27.0/24
                               │
                          x16 = .110


WSL eth0
172.30.221.x/20
    │
    └── vEthernet (WSL)
             │
             └── WSL NAT
                    │
                 Internet

27. 如何快速判断以后是否正常

第一步:Windows 看交换机

1
2
Get-VMSwitch |
    Format-Table Name, SwitchType, NetAdapterName

应该存在:

1
WSL-External    External

第二步:WSL 看网卡

1
2
ip addr show eth0
ip addr show eth1

应该存在:

1
2
eth0 → 172.30.x.x
eth1 → 202.197.27.117/24

第三步:看路由

1
ip route

应该至少有:

1
2
default via 172.30.x.x dev eth0
202.197.27.0/24 dev eth1

第四步:测试 x16

1
ping -c 4 202.197.27.110

第五步:测试 Internet

1
ping -c 4 8.8.8.8

如果两者都成功,网络就是正常的。


28. 如果以后 WSL 重启后 eth1 消失

这是该方案的正常特性。

因为 WSL2 VM 是动态创建的,所以再次启动 WSL 后,之前附加的 External Endpoint 可能需要重新挂载。

直接双击:

1
G:\tools\start_x16_wsl.bat

即可重新完成:

1
2
3
4
启动 WSL
→ WSLAttachSwitch
→ 创建 eth1
→ 配置 202.197.27.117

29. 本次不需要再恢复的设置

当前已经验证并应保留:

1
2
3
4
5
6
7
Hyper-V                    ✅ 保留
WSL-External               ✅ 保留
WSLAttachSwitch.exe        ✅ 保留
WSLAttachSwitch 参数保存   ✅ 保留
x16-network.sh             ✅ 保留
start_x16_wsl.bat          ✅ 保留
networkingMode=nat         ✅ 保留

不再使用:

1
2
3
networkingMode=bridged     ❌
networkingMode=mirrored    ❌
vmSwitch=WSL-External      ❌

关于 SkipAsSource

曾经为了排查多 IP 路由问题调整过 SkipAsSource,但最终方案并不依赖它。

当前 192.168.23.88 已不存在于当前 Windows IPv4 配置,因此不要凭旧记录强行重新添加。

如果以后确实需要恢复 192.168.23.88 这张网络,应先确认它原本的用途、网关和应该绑定的接口,再单独配置。


30. Meta Tunnel 说明

Windows 网络连接中还存在:

1
Meta Tunnel

它属于第三方代理 / TUN 类虚拟网络接口,不是本次 WSL-x16 方案的一部分。

本次没有对它做任何修改,也不应该为了整理 WSL 网络而随意禁用或删除。

如果它正在承担系统代理或 TUN 流量,修改它可能影响 Windows 正常上网。


31. 最终日常使用方法

平时需要进行 x16 开发时:

1
2
双击:
G:\tools\start_x16_wsl.bat

然后在 WSL 中直接访问:

1
x16 = 202.197.27.110

例如:

1
2
3
ping 202.197.27.110
ssh <user>@202.197.27.110
scp <file> <user>@202.197.27.110:<path>

具体是否使用 SSH / SCP 取决于 x16 上实际运行的服务。


32. 本次问题的核心结论

这次问题不是:

  • x16 网口不通
  • Windows 到 x16 不通
  • WSL 网卡损坏
  • 单纯缺少默认路由

真正的问题可以概括为:

WSL2 默认 NAT 网络虽然可以访问 Internet,但在当前 Windows 多网络环境下无法直接把 x16 的 202.197.27.0/24 开发网络暴露给 WSL。

最终采用:

WSL 保持 NAT,另外通过 Hyper-V External Switch + WSLAttachSwitch 给 WSL 增加第二块网卡,并给该网卡配置固定 202.197.27.117/24。

这是本次实际验证成功的方案。

最终状态:

1
2
3
4
WSL eth0 → Internet          ✅
WSL eth1 → x16               ✅
Windows → x16                ✅
重启后重新双击 BAT → 正常      ✅

33. 快速恢复清单

以后重装或迁移环境,可以按以下顺序:

Windows

  1. 安装 / 启用 Hyper-V。
  2. 创建:
1
New-VMSwitch -Name "WSL-External" -NetAdapterName "以太网" -AllowManagementOS $true
  1. 准备:
1
G:\tools\WSLAttachSwitch.exe
  1. 保存参数:
1
G:\tools\WSLAttachSwitch.exe "WSL-External" --save-params

WSL

  1. 保持:
1
2
3
4
[wsl2]
networkingMode=nat
dnsTunneling=true
autoProxy=true
  1. 创建:
1
/usr/local/sbin/x16-network.sh

内容:

1
2
3
4
#!/bin/bash
ip link set eth1 up
ip addr flush dev eth1
ip addr add 202.197.27.117/24 dev eth1
  1. 设置:
1
sudo chmod +x /usr/local/sbin/x16-network.sh
  1. 准备:
1
G:\tools\start_x16_wsl.bat
  1. 使用时双击 BAT。

34. 一句话记忆版

以后再遇到同样问题,只需要记住:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
不要折腾 WSL bridged。

保留 WSL NAT(eth0)用于 Internet;

用 Hyper-V External Switch + WSLAttachSwitch
增加 eth1;

eth1 固定:202.197.27.117/24;

x16:202.197.27.110;

最后双击:
G:\tools\start_x16_wsl.bat
Licensed under CC BY-NC-SA 4.0
最后更新于 Sep 16, 2026 07:11 UTC
Xenithya的个人博客
使用 Hugo 构建
主题 Stack 由 Jimmy 设计