Created
September 5, 2020 21:04
-
-
Save danvy/9486bf730371436131cb888ff4c2ceb6 to your computer and use it in GitHub Desktop.
Reset your WSL network connection trying to fix WSL2 media disconnected error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)" |
Thank you @hadamrd
Your additional information will help users too.
Thanks God this script save my life :)))
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
Thank you very much!!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
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!