-
Notifications
You must be signed in to change notification settings - Fork 822
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
WSL2 cannot connect to localhost when the service is running on Windows #5211
Comments
hi, Going forward, you can create a tunnel from Windows to WSL if you need/want to use Hope this helps. |
Thanks for the reply. I notice that many related issues are all about the network between the two systems. I can create a tunnel or proxy for this. But may I know if the WSL team has a specific plan or eta for the network improvement? Thanks. |
let's wait for Build (and here I really am like you, I'm waiting on news) and see if something will done. still, I do agree with you that the "reverse forwarding" from Windows to WSL2 would be a great improvement |
|
@nunix can you give us an example? For example; I am trying to access Is it possible to do with the |
same problem , we need to use proxy on windows localhost |
In my case, I am unable to connect to a service running on windows on ANY IP. Always the response is:
|
@fatichar turn off your firewall , it cost me serveral hours to get it out . This is difinitely another big problem |
Actually, what I am looking for is something even more about proxy between WSL2 and Windows. I am expecting similar experiences like WSL1, where WSL and Windows are using the same address (at least users should not be bothered if services on WSL and Windows are listening on different addresses) and follow the same firewall rules. If the network is not unified, why not use VM directly and using Samba to share the filesystem? WSL1 provide grate convenience compared with VM. 🤔 Am I too greedy? 😂 |
hi @gencer I would suppose it's possible indeed, but on Let me know the output of netstat and I will provide you the command |
@nunix, Unfortunately it binds on localhost as follow:
However, we make it work on SSH/WSL by making vscode extension runing on UI side. This makes us to access localhost port and data. |
you can access Windows port from WSL2 using your local IP like 192.168.31.66 instead of localhost, though is not elegant |
I also have experienced this issue when accessing my Windows' Redis instance from my project running in WSL2. End up mapping Nameserver to localhost in You can find Nameserver from Created a shell script to make life simpler: https://gist.github.com/toryano0820/6ee3bff2474cdf13e70d972da710996a Not sure yet if there's a downside with this method. But it works! |
This is a complete duplicate of #4619, and all details there are applicable here. For those not wanting to scroll through everything there, there doesn't seem to be any indication that WSL2 will go back to handling networking in a similar manner to WSL1. Your two options are 1: Put together some hacks and maybe it'll work, or 2: Revert to WSL1 and hope WSL2 maybe someday changes its approach back to that of WSL1. |
I was having similar issues. For me it's a hit or miss scenario. Sometimes it works sometimes it doesn't. I started webpack dev server (0.0.0.0:500) and accessed it through 127.0.0.1:500, usually browser will wait for bundles to load but today it loaded html page then just disconnected. Reloading no longer loads the page. I had to disable my vpn before starting local server. After that turning on VPN seems to have no impact on server. I tried shutting down wsl instance (VPN still connected) and starting it again and everything seems to work fine. 🤔 |
I can confirm that I can connect to the WSL2's localhost using VPN but are unable to do so when I am disconnected. |
I personally ended up running containers with the WSL2 integration for services that can run on Docker (Mongo for example) |
This is so frustrating |
When I upgraded my distro I started having all sorts of DNS problems (even with McAfee firewall turned off) and then today hit this issue of not being able to connect to a port of a process running in Windows from my Ubuntu command line. I've reverted the distro to WSL 1: wsl.exe --set-version Ubuntu 1 and things seem to be back to normal now. |
It works . But I want to know why . I have added advanced rule in windows firewall , but it doesn't work . |
So you can't run SSH or any daemons (#994 (comment)) and you can't connect to servers run as Windows services (e.g. #3173). 😢 |
This is just a nightmare when doing software development. Was expecting to work this just out of the box after all those years. Can't believe it doesn't. Non-Windows-Users laughing at me. Again. Great job. |
@helgatheviking It did not ! But maybe back it up to be safe, or make sure you have the normal version installed as well.. |
I found an old article about localhost and bind 0.0.0.0. I'm posting it in case it can lead anyone who's troubleshooting to something new. https://www.bleepingcomputer.com/news/security/wsl2-now-supports-localhost-connections-from-windows-10-apps/ |
Thanks for the input. However, being able to access a listener/server started in WSL is not the problem, it's the other way around. You can't access servers started in Windows with WSL. |
This Microsoft page helped solve the problem on my end https://docs.microsoft.com/en-us/windows/wsl/networking Check in the "Accessing Windows networking apps from Linux (host IP)" section Works pretty fine👌 |
Yes, this was already mentioned in this thread a few times (e.g. #5211 (comment)) |
Did anyone got around "connection refused" issue even when using the I tried: disabled firewall, fast startup, allowed cat /etc/resolv.conf
> nameserver 172.24.176.1
curl -XGET http://172.24.176.1:4040/inspect/http -I -v
* Trying 172.24.176.1:4040...
* TCP_NODELAY set
* connect to 172.24.176.1 port 4040 failed: Connection refused
* Failed to connect to 172.24.176.1 port 4040: Connection refused
* Closing connection 0
curl: (7) Failed to connect to 172.24.176.1 port 4040: Connection refused |
@bodinsamuel does your windows host use a proxy lan setting or .pac script? if so, any recent changes? |
I also need some kind of localhost proxy from WSL2 to Windows host - in case I want to execute Windows binaries (that´s why WSL to Windows Interop exists), I cannot use IP Address. For example, if I Packer starts VirtualBox VM with port forwarding, it works until the moment when starts waiting for localhost:port. Boom! It ends in a never-ending loop... Better solution would be to utilize some kind of proxy - not workaround which is useless. No offense. All threads around the WEB repeats the same: “You cannot use localhost - you have to use IP address of the host!” Well... so what to solve it with “some kind” of iptables hack and redirect traffic to localhost (for specific ports of course - maybe higher than 1024? To avoid problems with accessing WSL’s own localhost)? UPDATE
|
I basically avoided this issue by using WSL1 for the past years, but I found a solution that works for me; If you don't encounter this issue often just use $(hostname).local 😒 TLDR; This changes
[boot]
command="sed -i \"s/127.0.0.1$(printf '\t')localhost/$(tail -1 /etc/resolv.conf | cut -d' ' -f2)$(printf '\t')localhost/g\" /etc/hosts 2>&1" This command replaces the default
$addr='0.0.0.0'; $ports=@(8080,27017); # set ports according to your use case, what ports from windows should be exposed.
$remoteport = (wsl hostname -I).Trim(); for( $i = 0; $i -lt $ports.length; $i++ ){ $port = $ports[$i]; echo "added portproxy ${addr}:${port} to ${remoteport}:${port}"; iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr"; iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport"; } You can reset these proxies anytime using Because the WSL2 ip address changes every restart you need to add two scheduled tasks; This for me solves many of the main issues why I didn't switch, it's not the same as using WSL1. But does the job since you don't have to change |
As a workaround, I added the following to my if [[ -n "$IS_WSL" || -n "$WSL_DISTRO_NAME" ]]; then
if [[ -z "$WINDOWS_HOST_IP_ADDRESS" ]]; then
export WINDOWS_HOST_IP_ADDRESS="$(powershell.exe -Command '(Get-NetIPConfiguration | Where-Object { $_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.Status -ne "Disconnected"}).IPv4Address.IPAddress' | tr -d '\r')"
fi
fi This way I can use the |
Had the same issue, mentioned workarounds relying only on IPs from /etc/resolv.conf or /etc/hosts are close but not enough, at least for me. Anyone who have this problem should try finding correct IP, for me it was one of those printed by |
For my use case, testing Akka HTTP micro framework, disabling IPv6 in Ubuntu Linux WSL2 allowed seamless access to URL such as http://localhost:8888/ |
Posting my solution to a similar (although possibly reverse) problem. I was trying to find a way to access RStudio Server (running on WSL2 Ubuntu) from another computer on my local network. RStudio Server is accessible on the Windows Host at localhost:8787. I needed to do both of these:
|
This is a frustrating topic. Exposing all the interfaces from windows via 0.0.0.0 is INSECURE. it would be great and makes most sense to just keep it behave the same as a loopback interface in WSL.. |
That moment when you scroll the whole thread since 2020 hoping at this point someone would have a good fix but nope 😂😕 |
No joke. I've been following this thread since it was opened, hoping someone far more intelligent than me would have solved this by now. Yet here we are lol |
Update since Major Version 2.0.9 For step 2, you can just do Original @obedm503 Thanks for pointing it out. Would have never known it was actually being addressed all this time. Had to take a few steps to get it working on my local environment. Just thought I'd share. The changes are on a pre-release of WSL so it won't be available through regular update paths.
Since getting it enabled, I haven't had a single issue with my localhost being inaccessible. I could safely enable IPV6 again too. Side note, pre-release v2.0.5 https://github.com/microsoft/WSL/releases/tag/2.0.5 moved the |
@dons20 thank you so much, it works fine for me. I have spent a lot of time trying to resolve this problem |
Awesome to hear! The release mentions it's a window 11 fix. Anyone on Windows 10 to confirm it works for them as well? |
This worked for me! |
This worked for me too on Windows 11. Thank you! |
@dons20 Thanks! Works for me on W11. This issue, WSL cannot connect to remote localhost, on my side had to do when the connected WiFi or LAN was set to "public network". On "private network" everything was fine. |
@dons20 cheers mate! |
Works! thank you so much! |
As mentioned by this comment: #5211 (comment) fixed by mirrored mode. |
Please fill out the below information:
Your Windows build number: (Type
ver
at a Windows Command Prompt)Microsoft Windows [Version 10.0.19041.264]
What you're doing and what's happening: (Copy&paste the full set of specific command-line steps necessary to reproduce the behavior, and their output. Include screenshots if that helps demonstrate the problem.)
I am using an HTTP proxy (ShadowSocks) on windows which is listening on localhost port 1080. Then, I want the command in WSL2 to go through that proxy.
What's wrong / what should be happening instead:
curl: (7) Failed to connect to 127.0.0.1 port 1080: Connection refused
Strace of the failing command, if applicable: (If
some_command
is failing, then runstrace -o some_command.strace -f some_command some_args
, and link the contents ofsome_command.strace
in a gist here).For WSL launch issues, please collect detailed logs.
In WSL1, all things are fine. In WSL2 I could connect to the HTTP proxy through my Windows IP. I believe it is caused by WSL2 running in a separate VM.
The text was updated successfully, but these errors were encountered: