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

Network down after reboot #3

Open
srchild opened this issue Dec 27, 2024 · 6 comments
Open

Network down after reboot #3

srchild opened this issue Dec 27, 2024 · 6 comments

Comments

@srchild
Copy link

srchild commented Dec 27, 2024

Thank you for this "recipe". It works for me to enable networking to my WSL instance from a different computer on my local network - excellent!

My issue (perhaps expected? perhaps not) is that on a reboot of my windows 10 host for WSL2 windows networking is broken. It is fixable and I've worked out what seem to be the minimum steps to get it working again. But I wonder whether the problem can be avoided, or recovered from in fewer steps (or recovered automatically perhaps via a startup script)?

After a reboot of windows I can get working again if I do the following:

  1. open WSL (start/run wsl)
  2. open Powershell as administrator
  3. open "Network and Internet" settings, and then open "Change adapter options"
  4. In "Change adapter options", Select Ethernet adaptor, open properties, untick "Hyper-V Extensible Virtual Switch"
  5. In Powershell, run:
    • Set-VMSwitch "WSL" -SwitchType Private
    • Set-VMSwitch "WSL" -NetAdapterName "Ethernet" (Ethernet is the name shown when I do Get-NetAdapter)
  6. Networking may now be fixed, both windows and WSL, but check Ethernet configuration in case it has lost any settings e.g. I have a static connection and sometimes it has lost gateway or DNS

Is there an easier way?

Is there a way to script this so that it can all get fixed automatically e.g. when Windows does a reboot in the middle of the night after installing updates? Probably the Powershell commands can be scripted, perhaps WSL can be set to run on startup(?), but how to script the unticking in adapter properties?

NB, in case relevant, I have a static IP on my Windows PC, and I have set a static IP for WSL (as per your instructions).

I have added a line to /etc/hosts on WSL, and also on my linux server and also to windows hosts file C:\windows\system32\drivers\etc\hosts

192.168.1.8 wsl

Thanks for the very useful and working recipe!

@colemar
Copy link
Owner

colemar commented Dec 27, 2024

Unfortunately you stumbled on the same issue I discovered too late.
I have not discovered a practical fix other than what you already did, sorry.

@srchild
Copy link
Author

srchild commented Dec 28, 2024

It looks as though it will be possible to script it.

My adapter name, from Get-NetAdapter, is "Ethernet"

This unticks the binding of Ethernet to HyperV Extensible switch

Disable-NetAdapterBinding -Name "Ethernet" -ComponentID vms_pp

Set the switchtype to Private, twice

Set-VMSwitch "WSL" -SwitchType Private
Set-VMSwitch "WSL" -SwitchType Private

Once is enough if it works. But it doesn't always work first go, and doing it a second time doesn't do any harm if it did work first time

Then configure the Switch

Set-VMSwitch "WSL" -NetAdapterName "Ethernet"

Then do Get-NetAdapter again, to get the name of the new virtual ethernet adapter, in my case it is "vEthernet (WSL)" The interfaceindex (30 in the case below) could be used instead in the next steps, but that appears to change on each boot whereas the name stays constant.

Name InterfaceDescription ifIndex Status MacAddress LinkSpeed

VMware Network Adapte...8 VMware Virtual Ethernet Adapter for ... 20 Up 00-50-56-C0-00-08 100 Mbps
Ethernet Intel(R) Ethernet Connection I217-V 12 Up 20-25-64-0D-2F-F9 1 Gbps
vEthernet (Default Swi... Hyper-V Virtual Ethernet Adapter 13 Up 00-15-5D-30-36-B2 10 Gbps
VirtualBox Host-Only N... VirtualBox Host-Only Ethernet Adapter 5 Up 0A-00-27-00-00-05 1 Gbps
VMware Network Adapte...1 VMware Virtual Ethernet Adapter for ... 3 Up 00-50-56-C0-00-01 100 Mbps
vEthernet (WSL) Hyper-V Virtual Ethernet Adapter #2 30 Up 20-25-64-0D-2F-F9 1 Gbps

Then reset the desired parameters for the virtual Ethernet adapter

The 'New' seems necessary if the desired IP address has not been allocated, but will error (doing no harm) if it is already correct

New-NetIPAddress -InterfaceAlias "vEthernet (WSL)" -IPAddress 192.168.1.61 -PrefixLength 24 -DefaultGateway 192.168.1.1

Similarly, "Set"ting it may error if it is already set but it may be required to set it and will do no harm if it is already set

Set-NetIPAddress -InterfaceAlias "vEthernet (WSL)" -IPAddress 192.168.1.61 -PrefixLength 24

Then set desired DNS addresses in case they are not set

Set-DnsClientServerAddress -InterfaceAlias "vEthernet (WSL)" -ServerAddresses ("192.168.1.1","8.8.8.8")

And now Windows networking is working as well as WSL networking :)

@colemar
Copy link
Owner

colemar commented Dec 28, 2024

If you manage to make an usable script, I would gladly include it in this repository!

@srchild
Copy link
Author

srchild commented Dec 28, 2024

It is working for me now :)

I'll test it and tweak it a bit before considering it successful. But essentially it is what I have already shown you.

I used Windows+R then shell:startup to create a vbs script in startup group containing just

set ws=wscript.CreateObject("wscript.shell")
ws.run "wsl", 0

If wanting to specify distribution could edit that to

ws.run "wsl -d <Distro>", 0

See microsoft/WSL#8854 (comment)

This starts a background WSL process on windows startup.

This is my Powershell script

Disable-NetAdapterBinding -Name "Ethernet" -ComponentID vms_pp
Set-VMSwitch "WSL" -SwitchType Private
Set-VMSwitch "WSL" -SwitchType Private
Set-VMSwitch "WSL" -NetAdapterName "Ethernet"
New-NetIPAddress -InterfaceAlias "vEthernet (WSL)" -IPAddress 192.168.1.61 -PrefixLength 24 -DefaultGateway 192.168.1.1
Set-NetIPAddress -InterfaceAlias "vEthernet (WSL)" -IPAddress 192.168.1.61 -PrefixLength 24
Set-DnsClientServerAddress -InterfaceAlias "vEthernet (WSL)" -ServerAddresses ("192.168.1.1","8.8.8.8")

Obviously for wider use that will need some explanation about using the correct adapter names, or some extra coding to detect them automatically.

I had to allow Powershell scripts:

Set-ExecutionPolicy RemoteSigned

I have put it in a file fix-network.ps1 in C:\posh. I configured Task Scheduler to run it on startup, with highest privileges, and with a delay of 60seconds (to ensure that WSL has already started up).

Maybe that delay can be reduced - needs some experimentation. But once that script has run it all seems OK.

@colemar
Copy link
Owner

colemar commented Dec 28, 2024

  1. Why using a vbs script in shell:startup instead of a more simple .bat, .cmd, or even a shortcut (.lnk)?
  2. Is the purpose of the last 3 lines of the script to configure WSL interface IP? If so, is the configuration of systemd-networkd in the linux guest still required? (like for example having a working /etc/systemd/network/10-eth0.network)

@srchild
Copy link
Author

srchild commented Dec 29, 2024

I'm pretty new to WSL (and this is my first time using Powershell). When googling for a way to start up WSL on boot it seemed that it isn't always straightforward e.g. it can shut itself down after 30s. The vbs was a way which way said to work, easy to try out, and does work, so I haven't tried others. There may well be other better approaches (I hope), especially as vbs is deprecated now.

As I have observed it, Windows usually binds to the physical ethernet adapter, but after that is bound to HyperV Extensible Switch Windows seems to find the vEthernet (WSL) virtual adapter and bind to that though often with incorrect or missing values. Those three lines are configuring Windows to use the vEthernet (WSL) adapter with my preferred static ip address 192.168.1.61.

As per your recipe WSL itself is already configured to use 192.168.1.8 as its static IP address and that seems to survive a reboot without any need to do anything other than start up WSL - what needs repair is the configuration of the physical adapter to act as a virtual switch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants