WSL2 cannot connect to localhost when the service is running on Windows
Please fill out the below information:
-
Your Windows build number: (Type
verat 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_commandis failing, then runstrace -o some_command.strace -f some_command some_args, and link the contents ofsome_command.stracein 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.
To simplify the issue, I will show the telnet result, 172.21.80.1 is the Windows IP on WSL switch.

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
See also #5131 @nunix do you see this as default behavior with the portproxy on WSL?
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.
@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
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,
netshis 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
hi, the issue here is that while Windows can see the
localhostports of WSL, the inverse in not true by default. The ports forwarding are from WSL to Windows. If you do anetstat -an | grep 1080on 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
localhostin your proxy (see:netsh interface portproxycommand)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
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:
~/tcc/network-info$ telnet 172.17.0.1 5432
Trying 172.17.0.1...
telnet: Unable to connect to remote host: Connection refused
@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, the issue here is that while Windows can see the
localhostports of WSL, the inverse in not true by default. The ports forwarding are from WSL to Windows. If you do anetstat -an | grep 1080on 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 uselocalhostin your proxy (see:netsh interface portproxycommand) Hope this helps. WSL Corsair@nunix can you give us an example? For example; I am trying to access
10134port from WSL.10134port 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
Let me know the output of netstat and I will provide you the command
@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.
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 /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!
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