Skip to content

Instantly share code, notes, and snippets.

@danvy
Created September 5, 2020 21:04
Show Gist options
  • Save danvy/9486bf730371436131cb888ff4c2ceb6 to your computer and use it in GitHub Desktop.
Save danvy/9486bf730371436131cb888ff4c2ceb6 to your computer and use it in GitHub Desktop.
Reset your WSL network connection trying to fix WSL2 media disconnected error
# Check these threads before proceeding:
# https://github.com/microsoft/WSL/discussions/5857
# https://github.com/microsoft/WSL/issues/5821
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
$CmdLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CmdLine
Exit
}
# Restart the Host Network Service
Restart-Service -Force -Name hns
# Restart the Windows Subsystem for Linux Manager
Restart-Service LxssManager
# Restart the WSL Network adapter
Restart-NetAdapter -Name "vEthernet (WSL)"
@Rizary
Copy link

Rizary commented Sep 6, 2020

Restart-NetAdapter -Name "vEthernet (WSL)" cannot be run because vEthernet (WSL) is gone when Restart-Service LxssManager.

But, when I start my windows terminal, then open Ubuntu WSL, I can restart the vEthernet (WSL). Is it common behavior? Because it's like everytime I shutdown the WSL, then the hyper-v adapter is also gone.

@mwoodpatrick
Copy link

This script is working for me, many thanks, I have seen the case where "vEthernet (WSL)" disappears not sure why, if I run "wsl --shutdown" and the adaptor still shows up,

@danvy
Copy link
Author

danvy commented Sep 7, 2020

Restart-NetAdapter -Name "vEthernet (WSL)" cannot be run because vEthernet (WSL) is gone when Restart-Service LxssManager.

But, when I start my windows terminal, then open Ubuntu WSL, I can restart the vEthernet (WSL). Is it common behavior? Because it's like everytime I shutdown the WSL, then the hyper-v adapter is also gone.

@Rizary Does it help if you restart the adapter before Lxss ?

@alohaninja
Copy link

alohaninja commented Sep 9, 2020

Restarting eventually fixed it for me - I had to toggle WSL integration on/off for my distro after reboot. Also realizing that toggling on Windows-container mode broke WSL networking again...

image

@dfquicazanr
Copy link

I added

wsl --shutdown
wsl ping google.com -c 4

At the end just to check that the wsl internet connection works at the end

@kabascolby
Copy link

kabascolby commented Sep 11, 2020

Thanks
It's working for me. Is there any permanent solution?
This is very dangerous and annoying.

@ivanfarkas2
Copy link

It worked for me. Thx

@mwoodpatrick
Copy link

It worked for me. Thx

What exactly did you get to work I'm having mixed luck using this script, what version of windows are you currently running

@kabascolby
Copy link

kabascolby commented Sep 25, 2020 via email

@nour-s
Copy link

nour-s commented Sep 29, 2020

It didn't work for me.
I had to add a firewall rule to whitelist the Network Interface.

@Alia5
Copy link

Alia5 commented Sep 30, 2020

This is a godsend!

@berndverst
Copy link

This did the trick for me!

@sethidden
Copy link

This works :) I initially copy pasted your code directly into PowerShell terminal and it didn't work. Then I created a separate .ps1 file and ran it and then it worked. Thanks!

@ahmednrana
Copy link

This is works.
Thanks

@13garth
Copy link

13garth commented Jul 21, 2021

I added

wsl --shutdown
wsl ping google.com -c 4

At the end just to check that the wsl internet connection works at the end

This fixed things for me, Thank you!

@dtraparic
Copy link

After 1h30 of trying everything on stackoverflow, this worked ! I am surprised this is still needed and still works after 2 years.

@danvy
Copy link
Author

danvy commented Jan 21, 2023

@dtraparic Happy that it help you. I'm also surprised that it's still relevant.

@hadamrd
Copy link

hadamrd commented Jul 31, 2024

Thank you so much! You saved me an entire day of frustration. I was stuck reconfiguring IPs, default routes, interface preferences, restarting LxssManager, my PC, and WSL multiple times. Even when everything seemed perfectly set up, nothing worked.

I ran your script, restarted my WSL instance, and suddenly everything went back to normal. The key component I was missing was:

Restart-Service -Force -Name hns

For those interested in why this works:

The Host Network Service (HNS) is a critical component in Windows that manages network virtualization and container networking. It's responsible for creating and managing virtual networks, including the one used by WSL.

When HNS encounters issues or gets into an inconsistent state, it can cause networking problems for WSL, even if all the configurations look correct. Restarting HNS forces it to reinitialize, which can clear up any stuck or inconsistent states.

This script effectively performs a "hard reset" on the networking components related to WSL:

  1. It restarts HNS, clearing any network virtualization issues.
  2. It restarts the LxssManager, which manages WSL instances.
  3. It restarts the WSL network adapter, ensuring it's in a clean state.
  4. Finally, it shuts down all WSL instances, forcing them to reinitialize their network stack on next start.

This comprehensive approach addresses multiple potential points of failure in the WSL networking stack, which is why it's so effective at resolving complex networking issues that persist even after seemingly correct configuration.

Again, thank you for sharing this solution. It's a real time-saver for anyone encountering stubborn WSL networking problems!

@danvy
Copy link
Author

danvy commented Aug 1, 2024

Thank you @hadamrd
Your additional information will help users too.

@vuthanhdatt
Copy link

Thanks God this script save my life :)))

@d-kja
Copy link

d-kja commented Oct 9, 2024

wsl ping google.com -c 4

thanks man, all that I needed to do was ping and it just setup a new IP and Adapter by itself

@maddbuzz
Copy link

Thank you very much!!!

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