Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Attempt to fix networking issues described in https://github.com/microsoft/WSL/issues/3438
# Attempt to fix broken network in WSL2.0
$guest_ip = bash -c "/sbin/ifconfig eth0 | egrep -o 'inet [0-9\.]+' | cut -d ' ' -f2"
Write-Output "Guest IP IS: $guest_ip"
$gateway_ips = Get-NetIPAddress -InterfaceAlias "vEthernet (WSL)" | select IPAddress
$gateway_ip = $gateway_ips[1].IPAddress
Write-Output "Gateway (local WSL adapter) IP is: $gateway_ip"
bash -c "sudo ifconfig eth0 netmask 255.255.240.0"
bash -c "sudo ip route add default via $gateway_ip"
@jordansissel
Copy link

jordansissel commented Aug 12, 2019

Attempting to reduce this to something that can be run from WSL directly:

 echo '(Get-NetIPAddress -InterfaceAlias "VEthernet (WSL)" | select -last 1).IPAddress' |powershell.exe -NoProfile -NoLogo | grep 192 | tr -d '\r' | read gateway; sudo ifconfig eth0 netmask 255.255.240.0; sudo ip route add default via $gateway; echo "Set gateway to $gateway"

@ConradMearns
Copy link

ConradMearns commented Aug 26, 2019

Hmm, both failed for me on build 190816
SIOCSIFNETMASK: Cannot assign requested address
the bash script fails the same but also says that the Command line is not complete

@dzhenway
Copy link

dzhenway commented Oct 6, 2019

echo '(Get-NetIPAddress -InterfaceAlias "VEthernet (WSL)" | select -last 1).IPAddress' |powershell.exe -NoProfile -NoLogo | grep 192 | tr -d '\r' | read gateway; sudo ifconfig eth0 netmask 255.255.240.0; sudo ip route add default via $gateway; echo "Set gateway to $gateway"

echo '(Get-NetIPAddress -InterfaceAlias "VEthernet (WSL)" | select -last 1).IPAddress' |powershell.exe -NoProfile -NoLogo | grep 192 | tr -d '\r' | read gateway; sudo ifconfig eth0 netmask 255.255.240.0; sudo ip route add default via $gateway; echo "Set gateway to $gateway"
powershell.exe: command not found

@ArrowRaider
Copy link

ArrowRaider commented Oct 27, 2020

RTNETLINK answers: File exists

@mnadeem
Copy link

mnadeem commented Aug 8, 2021

Attempt to fix broken network in WSL2.0

$guest_ip = bash -c "ip a show eth0 | egrep -o 'inet [0-9.]+' | cut -d ' ' -f2"
Write-Output "Guest IP IS: $guest_ip"
$gateway_ips = Get-NetIPAddress -InterfaceAlias "vEthernet (WSL)" | select IPAddress
$gateway_ip = $gateway_ips[1].IPAddress
Write-Output "Gateway (local WSL adapter) IP is: $gateway_ip"
bash -c "sudo ip a add 192.168.0.1/255.255.240.0 dev eth0"
bash -c "sudo ip route add default via $gateway_ip"

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