Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Choose which physical Nic to be used with the WSL. #4799

Open
serotonyn opened this issue Jan 4, 2020 · 7 comments
Open

Choose which physical Nic to be used with the WSL. #4799

serotonyn opened this issue Jan 4, 2020 · 7 comments
Labels
feature network wsl2 Issue/feature applies to WSL 2

Comments

@serotonyn
Copy link

serotonyn commented Jan 4, 2020

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.

@Biswa96
Copy link

Biswa96 commented Jan 4, 2020

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

@serotonyn
Copy link
Author

I see, thank you for your answering me :)

@craigloewen-msft craigloewen-msft added feature network wsl2 Issue/feature applies to WSL 2 labels Jan 6, 2020
@therealkenc
Copy link
Collaborator

This one is arguably a restatement #4150.

@luxzg
Copy link

luxzg commented Jan 18, 2020

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.

@micheldiemer
Copy link

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
Copy link

micheldiemer commented Apr 18, 2024

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

@chanpreetdhanjal
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature network wsl2 Issue/feature applies to WSL 2
Projects
None yet
Development

No branches or pull requests

7 participants