WSL icon indicating copy to clipboard operation
WSL copied to clipboard

WSL2 cannot connect to localhost when the service is running on Windows

Open bingzhangdai opened this issue 6 years ago • 87 comments

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. image

  • 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 run strace -o some_command.strace -f some_command some_args, and link the contents of some_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.

bingzhangdai avatar May 14 '20 09:05 bingzhangdai

To simplify the issue, I will show the telnet result, 172.21.80.1 is the Windows IP on WSL switch. image

bingzhangdai avatar May 14 '20 09:05 bingzhangdai

hi, the issue here is that while Windows can see the localhost ports of WSL, the inverse in not true by default. The ports forwarding are from WSL to Windows. If you do a netstat -an | grep 1080 on WSL, it should return nothing. However, when you pass it trough the Gateway (WSL switch), it will reach it.

Going forward, you can create a tunnel from Windows to WSL if you need/want to use localhost in your proxy (see: netsh interface portproxy command)

Hope this helps. WSL Corsair

nunix avatar May 14 '20 11:05 nunix

See also #5131 @nunix do you see this as default behavior with the portproxy on WSL?

bplasmeijer avatar May 14 '20 15:05 bplasmeijer

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.

bingzhangdai avatar May 15 '20 04:05 bingzhangdai

let's wait for Build (and here I really am like you, I'm waiting on news) and see if something will done. @bplasmeijer I remember the SSH, actually right now, netsh is what I use for many of my demos.

still, I do agree with you that the "reverse forwarding" from Windows to WSL2 would be a great improvement

nunix avatar May 15 '20 06:05 nunix

let's wait for Build (and here I really am like you, I'm waiting on news) and see if something will done. @bplasmeijer I remember the SSH, actually right now, netsh is what I use for many of my demos.

still, I do agree with you that the "reverse forwarding" from Windows to WSL2 would be a great improvement

cc: @craigloewen-msft @benhillis @sirredbeard

bplasmeijer avatar May 15 '20 09:05 bplasmeijer

hi, the issue here is that while Windows can see the localhost ports of WSL, the inverse in not true by default. The ports forwarding are from WSL to Windows. If you do a netstat -an | grep 1080 on WSL, it should return nothing. However, when you pass it trough the Gateway (WSL switch), it will reach it.

Going forward, you can create a tunnel from Windows to WSL if you need/want to use localhost in your proxy (see: netsh interface portproxy command)

Hope this helps. WSL Corsair

@nunix can you give us an example? For example; I am trying to access 10134 port from WSL. 10134 port is on Windows assigned by Logitech Options application. I would like to access this port from inside WSL.

Is it possible to do with the portproxy? Currently my WSL IP is: 172.27.80.1

gencer avatar May 18 '20 19:05 gencer

same problem , we need to use proxy on windows localhost

eromoe avatar May 19 '20 15:05 eromoe

In my case, I am unable to connect to a service running on windows on ANY IP. Always the response is:

~/tcc/network-info$ telnet  172.17.0.1 5432
Trying 172.17.0.1...
telnet: Unable to connect to remote host: Connection refused

fatichar avatar May 27 '20 15:05 fatichar

@fatichar turn off your firewall , it cost me serveral hours to get it out . This is difinitely another big problem

eromoe avatar May 28 '20 01:05 eromoe

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? 😂

bingzhangdai avatar May 28 '20 04:05 bingzhangdai

hi, the issue here is that while Windows can see the localhost ports of WSL, the inverse in not true by default. The ports forwarding are from WSL to Windows. If you do a netstat -an | grep 1080 on WSL, it should return nothing. However, when you pass it trough the Gateway (WSL switch), it will reach it. Going forward, you can create a tunnel from Windows to WSL if you need/want to use localhost in your proxy (see: netsh interface portproxy command) Hope this helps. WSL Corsair

@nunix can you give us an example? For example; I am trying to access 10134 port from WSL. 10134 port is on Windows assigned by Logitech Options application. I would like to access this port from inside WSL.

Is it possible to do with the portproxy? Currently my WSL IP is: 172.27.80.1

hi @gencer I would suppose it's possible indeed, but on powershell if you do a netstat -an, on which interface is port 10134 mapped? if it's localhost, then WSL2 cannot reach it indeed, if it's bound to 0.0.0.0 then from WSL2 distro, try connecting to the :10134

Let me know the output of netstat and I will provide you the command

nunix avatar May 28 '20 21:05 nunix

@nunix, Unfortunately it binds on localhost as follow:

  TCP    127.0.0.1:10134        0.0.0.0:0              LISTENING
  TCP    127.0.0.1:10134        127.0.0.1:50456        ESTABLISHED
  TCP    127.0.0.1:10134        127.0.0.1:50833        ESTABLISHED
  TCP    127.0.0.1:10134        127.0.0.1:56123        ESTABLISHED
  ... and vice-versa
  TCP    127.0.0.1:56123        127.0.0.1:10134        ESTABLISHED
  TCP    127.0.0.1:50456        127.0.0.1:10134        ESTABLISHED

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.

gencer avatar May 28 '20 23:05 gencer

you can access Windows port from WSL2 using your local IP like 192.168.31.66 instead of localhost, though is not elegant

djfos avatar Oct 11 '20 05:10 djfos

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 /etc/hosts.

You can find Nameserver from /etc/resolv.conf, it looks like: nameserver 172.18.144.1 Then you can add line in /etc/hosts like: 172.18.144.1 localhost

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!

toryano0820 avatar Nov 15 '20 17:11 toryano0820

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.

bsplosion avatar Nov 17 '20 18:11 bsplosion

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. 🤔

itsakt avatar Nov 20 '20 14:11 itsakt

I can confirm that I can connect to the WSL2's localhost using VPN but are unable to do so when I am disconnected.

hasan-hasanov avatar Nov 24 '20 17:11 hasan-hasanov

I personally ended up running containers with the WSL2 integration for services that can run on Docker (Mongo for example)

troll-os avatar Dec 03 '20 11:12 troll-os

This is so frustrating

medmin avatar Dec 29 '20 04:12 medmin