Skip to content

Instantly share code, notes, and snippets.

@antriver
Created April 21, 2022 12:43
Show Gist options
  • Save antriver/d32431b1626a9074830c6e53936ec0f8 to your computer and use it in GitHub Desktop.
Save antriver/d32431b1626a9074830c6e53936ec0f8 to your computer and use it in GitHub Desktop.
Sets the WSL instance to a static IP address of 192.168.50.2 (from https://github.com/microsoft/WSL/issues/4210#issuecomment-856482892)
wsl -d Ubuntu -u root ip addr del $(ip addr show eth0 ^| grep 'inet\b' ^| awk '{print $2}' ^| head -n 1) dev eth0
wsl -d Ubuntu -u root ip addr add 192.168.50.2/24 broadcast 192.168.50.255 dev eth0
wsl -d Ubuntu -u root ip route add 0.0.0.0/0 via 192.168.50.1 dev eth0
wsl -d Ubuntu -u root echo nameserver 1.1.1.1 ^> /etc/resolv.conf
powershell -c "Get-NetAdapter 'vEthernet (WSL)' | Get-NetIPAddress | Remove-NetIPAddress -Confirm:$False; New-NetIPAddress -IPAddress 192.168.50.1 -PrefixLength 24 -InterfaceAlias 'vEthernet (WSL)'; Get-NetNat | ? Name -Eq WSLNat | Remove-NetNat -Confirm:$False; New-NetNat -Name WSLNat -InternalIPInterfaceAddressPrefix 192.168.50.0/24;"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment