Skip to content

Instantly share code, notes, and snippets.

@sidneyspe
Created April 12, 2024 14:20
Show Gist options
  • Save sidneyspe/301ae42341e2c301f26cbe2c04513a59 to your computer and use it in GitHub Desktop.
Save sidneyspe/301ae42341e2c301f26cbe2c04513a59 to your computer and use it in GitHub Desktop.

Recipe: Persistent Network Configuration in WSL 2 using Hyper-V Virtual Switch

Problem Description

Connecting to services running in WSL 2 from external sources can be challenging due to the instances being on a different network. This guide offers a solution to replace the internal virtual switch of WSL 2 with an external version in Windows 20H2 (WSL 2.0) and configure it for better networking control.

Solution Overview

This recipe uses a Hyper-V virtual switch to bridge the WSL 2 network, providing improved control and visibility of Windows' network adapters within Ubuntu. The configuration supports both dynamic and static IP addressing, eliminating the need for port forwarding and simplifying network setup.

Steps

  1. Enable Hyper-V and Management PowerShell Features:

    • Open PowerShell as administrator and run:
      Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V
      Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-Management-PowerShell
  2. Create an External Virtual Switch in Hyper-V:

    • Open PowerShell as administrator and run:
      New-VMSwitch -Name "External Switch" -NetAdapterName eth0
  3. Modify the WSL Configuration:

    • Create or modify the .wslconfig file in your user profile directory ($env:USERPROFILE/.wslconfig) with the following content:
      [wsl2]
      networkingMode=bridged
      vmSwitch="External Switch"
      dhcp=true
      ipv6=true
      
  4. Enable systemd in the WSL Distribution:

    • Edit the /etc/wsl.conf file in your WSL distribution and add the following lines:
      [boot]
      systemd=true
      [network]
      hostname = HOSTAGE
      generateResolvConf = false
      
  5. Configure Network Addressing:

    • For dynamic address configuration, ensure the following is present in /etc/systemd/network/10-eth0.network:
      [Match]
      Name=eth0
      [Network]
      DHCP=yes
      
    • For static address configuration, use:
      [Match]
      Name=eth0
      [Network]
      Address=192.168.x.xx/24
      Gateway=192.168.x.x
      DNS=192.168.x.x
      
  6. Link systemd Resolv.conf:

    • Create a symbolic link to link resolv.conf from systemd:
      ln -sfv /run/systemd/resolve/resolv.conf /etc/resolv.conf
  7. Verification:

    • Restart the WSL 2 instance and verify the network configuration with:
      ip addr show eth0
@sidneyspe
Copy link
Author

or add:

networkingMode=mirrored

in .wslconfig on user folder on windows

@sidneyspe
Copy link
Author

[wsl2]
memory=30GB # limita a memória da VM.
processors=14 # limita o uso da VM para 14 processadores virtuais
networkingMode=mirrored

@qingzheng-web
Copy link

It didn't work in my windows 10.

@sidneyspe
Copy link
Author

sidneyspe commented Oct 30, 2024

currenty I use this .wslconfig file:

[wsl2]
memory=30GB # limita a memória da VM.
processors=14 # limita o uso da VM para 14 processadores virtuais
dnsTunneling=true
autoProxy=true
networkingMode=mirrored
useWindowsDnsCache=true

[experimental]
autoMemoryReclaim=gradual
sparseVhd=true
hostAddressLoopback=true
bestEffortDnsParsing=true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment