WSL icon indicating copy to clipboard operation
WSL copied to clipboard

Choose which physical Nic to be used with the WSL.

Open serotonyn opened this issue 6 years ago • 7 comments

Is your feature request related to a problem? Please describe. No.

Describe the solution you'd like I want to use my wireless card for WSL networking, and my ethernet card for Host networking (windows).

Describe alternatives you've considered I've tried to set a new external virtual switch, but that didn't work.

I'm using Wsl 2.

serotonyn avatar Jan 04 '20 08:01 serotonyn

At least, now we can't do it. LxssManager.dll does everything for us.

Biswa96 avatar Jan 04 '20 13:01 Biswa96

I see, thank you for your answering me :)

serotonyn avatar Jan 04 '20 17:01 serotonyn

This one is arguably a restatement #4150.

therealkenc avatar Jan 16 '20 07:01 therealkenc

One of many issues with DefaultSwitch and not having other options. Just like #4210 / #4601 / #4467 / #4150 ; to name a few.

I'm taking my time today to mark all these issues.

luxzg avatar Jan 18 '20 14:01 luxzg

Windows 11 22H2 build 22621 (Pro version at least) WSL 2.1.5 Create a virtual switch in Hyper-V and use WSLAttachSwitch.exe (may not be possible for you)

# RunAs Administrator

# settings
$switchName="StaticIPs"
$submaskCidrBits=24
$winIp="192.168.10.1"
$wslIp="192.168.10.2"
# WSL path for WSLAttachSwitch.exe, will be downloaded via curl
$exeFile="/mnt/c/Windows/System32/WSLAttachSwitch.exe"

# set / check firewal rules
Set-NetFirewallHyperVVMSetting -Name '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}' -DefaultInboundAction Allow
Get-NetFirewallHyperVVMSetting -PolicyStore ActiveStore -Name '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}'

# create a virtual switch and create the IP address of the windows host
New-VMSwitch -SwitchName $switchName -SwitchType Internal
$netadapter=Get-Netadapter | where-Object Name -Like "*$switchName*"
New-NetIPAddress -PrefixLength $submaskCidrBits -InterfaceIndex $netadapter.ifIndex -IPAddress $winIp

# out of scope / optional : connect other HyperV machines to the VMSwitch

# download WSLAttachSwitch
$cmd = "curl -L -o $exeFile https://github.com/dantmnf/WSLAttachSwitch/releases/download/latest/WSLAttachSwitch.exe" 
wsl -u root bash -c $cmd

## UNCOMMENT THIESE LINES IF YOU WANT TO TEST BEFORE MAKING PERMANENT CHANGES
## # attach switch to vm
## $cmd = "sudo $exeFile  $switchName"
## wsl bash -c $cmd
## # assign static IP to vm
## $cmd = "sudo ip addr add $wslIp dev eth1"
## wsl bash -c $cmd
## # all the machines connected to $switchName can ping / communicate with each other

## permanent changes using /etc/rc.local and /etc/wsl.conf
# create or append commands to /etc/rc.local
$cmd = "[ ! -f /etc/rc.local ] && echo '#!/bin/sh -e' > /etc/rc.local"
wsl -u root bash -c $cmd
# assign switch to WSL vm
$cmd = "echo $exeFile $switchName | sudo tee -a /etc/rc.local"
wsl bash -c $cmd
# assign ip address to WSL vm
$cmd = "echo 'ip addr add $wslIp dev eth1' >> /etc/rc.local"
wsl -u root bash -c $cmd
# make sure /etc/rc.local is loaded on boot via /etc/wsl.conf
wsl -u root bash -c "sudo chmod u+x  /etc/rc.local"
wsl -u root bash -c "echo '[boot]' >>  /etc/wsl.conf "
wsl -u root bash -c "echo 'command=/etc/rc.local' >> /etc/wsl.conf "


# Enjoy all machines connected to the switch being able to communicate with each other !

micheldiemer avatar Apr 18 '24 22:04 micheldiemer

Please find below a more textual description of the above script :

  1. Create a Hyper-V virtual switch or use it. Its name is "VirtualSwitch"
  2. Adjust firewall rules of WSL
Set-NetFirewallHyperVVMSetting -Name '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}' -DefaultInboundAction Allow
Get-NetFirewallHyperVVMSetting -PolicyStore ActiveStore -Name '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}'
  1. Download WSLAttachSwitch.exe on your Windows hard drive (not WSL), e.g. /mnt/c/Windows/System32/WSLAttachSwitch.exe
  2. Attach the switch to the WSL vm

File /etc/rc.local (must be executable)

#!/bin/sh -e
/mnt/c/Windows/System32/WSLAttachSwitch.exe VirtualSwitch
# bonus : assign ip address
# ip addr add 192.168.0.2 dev eth1

File /etc/wsl.conf

[boot]
command=/etc/rc.local

Gist in french : https://gist.github.com/micheldiemer/e32a294cd484eff7bcd362bf8f3330b3#file-wsl_hyperv_switch-md

micheldiemer avatar Apr 18 '24 22:04 micheldiemer

Hi. Can you please collect networking logs by following the instructions below? https://github.com/microsoft/WSL/blob/master/CONTRIBUTING.md#collect-wsl-logs-for-networking-issues

chanpreetdhanjal avatar Apr 29 '24 20:04 chanpreetdhanjal