WSLAttachSwitch icon indicating copy to clipboard operation
WSLAttachSwitch copied to clipboard

Multiple switches

Open tbarbette opened this issue 1 year ago • 2 comments

Hi all,

There's an "UPDATE" comment on the main page telling that this repository is outdated because there's an official way with the .wslconfig file now to attach a switch.

Is there a way to attach multiple switches with .wslconfig, or should I still use this great tool you made for that use case?

Having multiple switches allows using MPTCP and never loose connections when hopping between 4G, WiFi and ethernet :)

Thanks, Tom

tbarbette avatar Jun 06 '23 09:06 tbarbette

this repository is outdated

It's not outdated, it now focuses on attaching multiple switches

Is there a way to attach multiple switches with .wslconfig

No, at least there is no official document yet.

or should I still use this great tool you made for that use case?

Yes

kenvix avatar Jul 29 '23 03:07 kenvix

This is not the right place but please find below a detailed procedure on how to use WSLAttachSwitch. It could appear in the documentation.

Procedure

Text

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

Script

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 23:04 micheldiemer