You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

WSL2通过交换机与自制设备通信同时Windows保持本地网络连接的配置方案咨询

WSL2通过交换机与自制设备通信同时Windows保持本地网络连接的配置方案咨询

Hey there! Let me walk you through how to get this dual-network setup working smoothly. I’ve tackled similar WSL2 networking headaches before, so let’s break it down step by step.

先明确核心配置目标

We need three things to coexist:

  • Windows 11 stays connected to your lab’s local network (with a fixed 192.168.X.X IP, so other lab machines can reach it)
  • WSL2 Ubuntu can communicate with all your homemade devices (HDs) on the 10.X.X.X subnet via the USB adapter + switch
  • No network conflicts between the two segments

Step 1: Hardware Connection Check

First, make sure your physical setup is right:

  • Plug your PC’s built-in wired Ethernet into the lab’s local network (this will hold the 192.168.X.X IP)
  • Connect your PC’s USB Ethernet adapter to the switch, then plug all HDs into the same switch
  • Use a basic unmanaged switch (if it’s a managed switch, skip VLAN configs for now to keep things simple)

Step 2: Configure Windows’ USB Ethernet Adapter

We need to isolate this adapter to the HD subnet so it doesn’t conflict with the lab network:

  1. Open Network and Sharing Center → click Change adapter settings
  2. Find the USB Ethernet adapter’s connection, right-click → Properties
  3. Double-click Internet Protocol Version 4 (TCP/IPv4)
  4. Set a static IP in the HD subnet:
    • IP address: 10.X.X.1 (pick any unused IP in the 10.X.X.X range, just avoid your HDs’ IPs)
    • Subnet mask: 255.255.255.0
    • Leave Default Gateway and DNS servers blank (this subnet only needs to talk to WSL2 and HDs, no external routing)
  5. Click OK to save changes

Step 3: Set Up Hyper-V Virtual Switch

This is the bridge between WSL2 and the USB adapter:

  1. Open Hyper-V Manager → click Virtual Switch Manager on the right sidebar
  2. Select External under "Create virtual switch", then click Create Virtual Switch
  3. Name it something like WSL-HD-Switch
  4. Under "Connection type", select your USB Ethernet adapter from the dropdown
  5. Check the box for "Allow management operating system to share this network adapter" (this lets Windows and WSL2 both use the USB adapter)
  6. Click OK to create the switch

Step 4: Configure WSL2 Network Settings

We need to make sure WSL2 can reach both the HD subnet and the lab network:

  1. First, shut down WSL2 completely to reset network configs: open a Windows Command Prompt and run wsl --shutdown
  2. Launch WSL2 Ubuntu again, then open a terminal
  3. Check your existing network interfaces with ip addr show — you’ll see eth0 (connected to the Hyper-V switch) and eth1 (default NAT interface for Windows/lab network)
  4. Set a static IP for eth0 (in the same subnet as your Windows USB adapter):
    sudo ip addr flush dev eth0
    sudo ip addr add 10.X.X.2/24 dev eth0
    
  5. Add a route to send all HD subnet traffic through eth0:
    sudo ip route add 10.X.X.0/24 dev eth0
    
  6. Make sure WSL2’s default gateway points to the NAT interface (so it can reach Windows and the lab network). First check the existing default route with ip route show — it’ll look like default via 172.X.X.1 dev eth1 (the 172.X.X.1 is Windows’ NAT gateway). If it’s missing, add it:
    sudo ip route add default via 172.X.X.1 dev eth1
    
  7. To make these settings persistent across WSL2 restarts:
    • Edit the wsl.conf file: sudo nano /etc/wsl.conf
    • Add these lines:
      [network]
      generateResolvConf = false
      
    • Save and exit (Ctrl+O, Enter, Ctrl+X)
    • Create a startup script ~/startup-network.sh:
      nano ~/startup-network.sh
      
      Paste this content (replace 10.X.X.2 and 172.X.X.1 with your actual values):
      #!/bin/bash
      sudo ip addr flush dev eth0
      sudo ip addr add 10.X.X.2/24 dev eth0
      sudo ip route add 10.X.X.0/24 dev eth0
      sudo ip route add default via 172.X.X.1 dev eth1
      
    • Make it executable: chmod +x ~/startup-network.sh
    • Add it to your ~/.bashrc so it runs on login: echo "~/startup-network.sh" >> ~/.bashrc

Step 5: Verify Connectivity

Test all the key connections to make sure everything works:

  • In WSL2, ping an HD: ping 10.X.X.10 (should get replies)
  • In WSL2, ping Windows’ lab IP: ping 192.168.X.X (should work)
  • In WSL2, ping another lab machine: ping 192.168.X.Y (should go through)
  • In Windows, ping an HD: ping 10.X.X.10 (should work)
  • From another lab machine, ping Windows’ 192.168.X.X (should be reachable)

Common Troubleshooting Tips

  • WSL2 can’t ping HDs: Check the Hyper-V switch is bound to the correct USB adapter. Also, check Windows Firewall — add an inbound/outbound rule allowing traffic on the 10.X.X.0/24 subnet.
  • WSL2 can’t reach the lab network: Double-check the default gateway points to the NAT interface (eth1). Also, make sure your resolv.conf has the lab’s DNS server: edit /etc/resolv.conf and add nameserver 192.168.X.1 (replace with your lab’s DNS).
  • Windows can’t ping HDs: Verify the USB adapter’s static IP is in the same subnet as the HDs, and no firewall rules are blocking the traffic.

备注:内容来源于stack exchange,提问作者C. Crt

火山引擎 最新活动