WSL: Cannot connect to 127.0.0.1, localhost, or linux IP address

This was working for me prior to 18945

Please fill out the below information:

  • Your Windows build number: (Type ver at a Windows Command Prompt) 10.0.18945.1001]

  • 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 screen shots if that helps demonstrate the problem.) Trying to access network ports in my WSL2 (port forwarded via ssh over vpn) via localhost or 127.0.0.1 or eth0 IP address and getting connection refusal

    • for example

ssh -i ~/.ssh/your.key -l username -L 27027:mongo.server.ip:27017 -N -f mongo.server.ip

  • What’s wrong / what should be happening instead: I should be able to connect from the windows host (eg in browser, or sql editor) to linux ports eg 127.0.0.1:8080 or localhost:27017
    • when I run NoSQLBooster for MongoDB in windows I get the error: "message" : "failed to connect to server [localhost:27027] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27027]

Connecting from Linux out (eg to initialize ssh port forwarding or lynx localhost:8080) works. I can ping the linux IP but not connect to that either

Network Info from wsl: ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: bond0: <BROADCAST,MULTICAST,MASTER> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 5e:ac:68:50:7d:20 brd ff:ff:ff:ff:ff:ff
3: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 9a:99:a8:e8:66:84 brd ff:ff:ff:ff:ff:ff
4: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
    link/sit 0.0.0.0 brd 0.0.0.0
5: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:15:5d:d1:ac:d2 brd ff:ff:ff:ff:ff:ff
    inet 172.27.47.10/16 brd 172.27.255.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::215:5dff:fed1:acd2/64 scope link
       valid_lft forever preferred_lft forever

ip r

default via 172.27.32.1 dev eth0
172.27.0.0/16 dev eth0 proto kernel scope link src 172.27.47.10

From windows

ifconfig

Ethernet adapter vEthernet (WSL):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::d121:7256:fceb:d6f0%42
   IPv4 Address. . . . . . . . . . . : 172.27.32.1
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . :

ping

ping 172.27.47.10

Pinging 172.27.47.10 with 32 bytes of data:
Reply from 172.27.47.10: bytes=32 time<1ms TTL=64
Reply from 172.27.47.10: bytes=32 time<1ms TTL=64
Reply from 172.27.47.10: bytes=32 time<1ms TTL=64

route print -4

 route print -4
===========================================================================
Interface List
 14...28 16 a8 04 45 1a ......Surface Ethernet Adapter #2
 17...00 15 5d f8 6b 1e ......Hyper-V Virtual Ethernet Adapter
 27...00 15 5d 84 73 11 ......Hyper-V Virtual Ethernet Adapter #2
 13...c6 9d ed aa cc 3c ......Microsoft Wi-Fi Direct Virtual Adapter #3
 18...c6 9d ed aa c9 3c ......Microsoft Wi-Fi Direct Virtual Adapter #4
 21...00 ff 8e 6b 18 73 ......TAP-Windows Adapter V9
 30...c4 9d ed aa cd 3d ......Marvell AVASTAR Wireless-AC Network Controller
 28...c4 9d ed aa cd 3e ......Bluetooth Device (Personal Area Network)
  1...........................Software Loopback Interface 1
 42...00 15 5d 72 59 7a ......Hyper-V Virtual Ethernet Adapter #3
===========================================================================

IPv4 Route Table
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0      192.168.1.1    192.168.1.128     35
         10.0.0.0      255.255.0.0        10.8.0.21        10.8.0.22    291
        10.0.75.0    255.255.255.0         On-link         10.0.75.1    271
        10.0.75.1  255.255.255.255         On-link         10.0.75.1    271
      10.0.75.255  255.255.255.255         On-link         10.0.75.1    271
         10.8.0.1  255.255.255.255        10.8.0.21        10.8.0.22    291
        10.8.0.20  255.255.255.252         On-link         10.8.0.22    291
        10.8.0.22  255.255.255.255         On-link         10.8.0.22    291
        10.8.0.23  255.255.255.255         On-link         10.8.0.22    291
        10.25.0.0      255.255.0.0        10.8.0.21        10.8.0.22    291
        10.64.0.0      255.255.0.0        10.8.0.21        10.8.0.22    291
       10.110.0.0      255.255.0.0        10.8.0.21        10.8.0.22    291
       10.233.0.0      255.255.0.0        10.8.0.21        10.8.0.22    291
        127.0.0.0        255.0.0.0         On-link         127.0.0.1    331
        127.0.0.1  255.255.255.255         On-link         127.0.0.1    331
  127.255.255.255  255.255.255.255         On-link         127.0.0.1    331
       172.18.0.0      255.255.0.0        10.8.0.21        10.8.0.22    291
     172.23.208.0    255.255.240.0         On-link      172.23.208.1    271
     172.23.208.1  255.255.255.255         On-link      172.23.208.1    271
   172.23.223.255  255.255.255.255         On-link      172.23.208.1    271
       172.24.0.0      255.255.0.0        10.8.0.21        10.8.0.22    291
      172.27.32.0    255.255.240.0         On-link       172.27.32.1   5256
      172.27.32.1  255.255.255.255         On-link       172.27.32.1   5256
    172.27.47.255  255.255.255.255         On-link       172.27.32.1   5256
       172.31.0.0      255.255.0.0        10.8.0.21        10.8.0.22    291
       172.42.0.0      255.255.0.0        10.8.0.21        10.8.0.22    291
       172.64.0.0      255.255.0.0        10.8.0.21        10.8.0.22    291
      192.168.1.0    255.255.255.0         On-link     192.168.1.128    291
    192.168.1.128  255.255.255.255         On-link     192.168.1.128    291
    192.168.1.255  255.255.255.255         On-link     192.168.1.128    291
        224.0.0.0        240.0.0.0         On-link         127.0.0.1    331
        224.0.0.0        240.0.0.0         On-link         10.8.0.22    291
        224.0.0.0        240.0.0.0         On-link         10.0.75.1    271
        224.0.0.0        240.0.0.0         On-link     192.168.1.128    291
        224.0.0.0        240.0.0.0         On-link      172.23.208.1    271
        224.0.0.0        240.0.0.0         On-link       172.27.32.1   5256
  255.255.255.255  255.255.255.255         On-link         127.0.0.1    331
  255.255.255.255  255.255.255.255         On-link         10.8.0.22    291
  255.255.255.255  255.255.255.255         On-link         10.0.75.1    271
  255.255.255.255  255.255.255.255         On-link     192.168.1.128    291
  255.255.255.255  255.255.255.255         On-link      172.23.208.1    271
  255.255.255.255  255.255.255.255         On-link       172.27.32.1   5256
===========================================================================
Persistent Routes:
  None

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 37 (3 by maintainers)

Most upvoted comments

Thanks for posting. I have identified an issue with the localhost feature and we are working on a fix. Binding to either localhost or 0.0.0.0 should work, but it seems only the later is working currently.

It would be very useful if somebody could identify an app or workflow that can reliably trigger any remaining issues.

I have postgres:10 running in a Docker container on ports 0.0.0.0:5432->5432/tcp on Windows 10.0.19551.1005: I am able to access it from WSL 1 with no issue, but on WSL 2 I get the following error:

PG::ConnectionBad - could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?

I believe WSL2 in current shape is useless for anything that involves networking. And in today’s world, that’s pretty much everything. If you want to develop web apps, having Apache on WSL2 is useless, as there are bunch of hoops to jump through, and even those aren’t guaranteed to work. You can’t reliably access that web server from outside, nor occasionally from local PC. If you want to use that WSL2 for management & monitoring tools to manage other (real) Linux servers using remote management/monitoring, again you have to rely on buggy and badly thought through network implementation. I’m not even talking about bunch of other scenarios people do or try to do with WSL2. I’m talking about 99% of what WSL was made to solve - give developers and admins (be it net/sys/db) access to tools from Linux ecosystem. From my point of view, it fails for most admin and dev uses with current networking stack. We’re all way better off creating our own lightweight VMs on Hyper-V, that’s just 4GB disk space and 10min of time. Slightly longer than installing WSL distro through Store, but you have 100% compatibility, 100% access to networking, and it is a reliable solution.

We aren’t meant to be browsing web from WSL2, and that’s just about it in a list of networking features that work on WSL2.

I beg excuse of all developers that are spending countless hours on this project, indeed a wonderful project with huge potential. But I hope you people still get the point. Perhaps you should just ask project managers to rethink the strategy…

You’ve not copied and pasted a full set of commands to reproduce. Can you give a minimal example?

One odd thing with the new localhost win10->wsl2 implementation is that the linux process needs to bind to 0.0.0.0 in order for win10 to access on localhost. I suspect this will be fixed before long.

example: ng serve This starts an angular/npm app but doesn’t specify which interface to bind to. It cannot be accessed from win10 ng server --host 0.0.0.0 This binds to all interfaces and win10 can access on http://localhost:4200

For what it’s worth, I also use postgresql on 9.5 and on a Rails setup.

I can’t imagine such a critical issue going unfixed for so much time, are people just not using WSL2 for developing anything on the web? I was hoping to but I guess with this issue the hype just comes crashing to the ground when a basic function can’t work correctly.

Big fan of accessing development stuff via localhost without having to configure security and access. localhost on WSL2 is great news.

Now, if you have to set 0.0.0.0 on every piece of software you don’t really benefit of the localhost simple access feature. Tried mysql and postgresql, they need to bind to 0.0.0.0 to accept connections from outside. RabbitMQ admin plugin web interface works fine via localhost (guest account by default works only via localhost).

I hope the localhost access would expand to most services without additional setup (like bind 0.0.0.0), if possible. localhost accesss made WSL so much easier to use compared to the traditional development vm.

for me the only that works accessing WSL2 server apps from windows 10 :

  • Obtain the IP address of your distro by running the command ip addr inside of your WSL distro and finding it under the inet value of the eth0 interface.
    • You can find this more easily by filtering the output of the command using grep like so: ip addr | grep eth0.
  • Connect to your Linux server using the IP you found above. from release notes of WSL2 WSL-1870.zip founded here .

made a small server in go and tested http://172.20.203.35:3006/ works fine !

Fixed in 18970.

Other landing zone over at #4851. For else, feel enouraged to open a new issue following the template.

I don’t think so. Still experiencing it on 19577.

I can reliably reproduce this with ssh -L bindings, like the OP. Start up ssh client with -L 1234:remote:1234, use that port forward a little in a browser, then use ~C SSH escape command to add another port forward using -L 5678:localhost:5678 and it more often than not won’t be available on localhost:5678. This can be confirmed using netstat -a -b -n, as the only result for wslhost.exe is the 1234 port from the original binding. Once it breaks like this, I also have to use wsl --shutdown to get localhost port forwarding working again in SSH client. Non-localhost connectivity (eg, binding to 0.0.0.0 using -L 0.0.0.0:5678:remote:5678 and connecting to the 172.x.x.x IP) works without a hitch.

I’m using Laravel.

Running php artisan serve --host 0.0.0.0 with default port of 8000. The app is not accessible from chrome at 127.0.0.1:8000

Same with Spring/Tomcat. My issues began when I installed Docker Desktop Edge for Windows with wsl2 integration.

Edit: @sitepodmatt is correct. I can run wsl --shutdown and reopen the terminal. This gives me the ability to hit apps running in wsl2 on localhost from windows. I was about 5 minutes from wiping the machine and starting fresh.

At least now I can wsl --shutdown and reopen a terminal and get back localhost connectivity (and /mnt/c access)

But it (localhost and mnt/c - I guess the whole interop services) never works on resume from standby/sleep (i.e. close laptop in evening and open in morning) - wondering is the standby/resume issue is considered somewhere else or perhaps laptop usage isn’t considered at the moment

@benhillis is there an issue for sleep resume? cant seem to find it (perhaps using wrong terminology - standby/suspend/resume/hibernation/sleep? )

@kangzj make sure you didn’t hit this bug #5298

you are a life saver thanks heaps

I’m using build 10.0.19041.21, also have the same problem. In my memory, I have successfully connected to 127.0.0.1:8080 only once using python -m SimpleHTTPServer 8080. Form then on, I cannot connect to the localhost any more.

Any progress made so far?

I realized that, if I put:

::1             test.tld localhost

instead of

::1             localhost test.tld

in the Windows hosts file, it works, I don’t know if this is a normal behaviour for IPv6

I can now confirm that even with a fresh reinstall of WSL2 with Ubuntu 18.04, it still has the same issue.

I’m on 18999, and I cannot use localhost. Trying to get to a mysql server.

172.29.181.182 works, but not 127.0.0.1 or localhost. in my.cnf , I commented bind-address.

do I have to do something special for localhost to work?

Fixed in 18970.