microsoft / WSL

Issues found on WSL
https://docs.microsoft.com/windows/wsl
MIT License
17.39k stars 819 forks source link

Cannot connect to 127.0.0.1, localhost, or linux IP address #4353

Closed pacanukeha closed 4 years ago

pacanukeha commented 5 years ago

This was working for me prior to 18945

Please fill out the below information:

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

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
owennewo commented 5 years ago

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

enachesilviu commented 5 years ago

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.

pacanukeha commented 5 years ago

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

Good point. I've added a specific example of ssh port forwarding that normally works - it is connecting to a remote mongodb instance but anything that you have in your network should behave similarly.

owennewo commented 5 years ago

If I'm reading it right you right you have the following setup

interesting pattern!

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

and setting GatewayPorts yes in /etc/ssh/sshd_config

My inspiration came from this: https://stackoverflow.com/questions/23781488/how-to-make-ssh-remote-port-forward-that-listens-0-0-0-0

As I mentioned in an earlier post you need for ports to bind to 0.0.0.0 with wsl2 at the moment - I'm hoping the suggestion above will cause the ssh port binding to bind to 0.0.0.0 and the GatewayPorts would expose the port to Win10 (and potentially to other PCs on your network!)

benhillis commented 5 years ago

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.

pacanukeha commented 5 years ago

interesting pattern!

Yeah - I have a number of mongo servers running elsewhere that don't have the ports exposed I normally connect to from wsl using the mongo cli but I have some Windows GUI tools that make some things easier so necessitating the song-and-dance

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

This works.! Brilliant, thank you.

and setting GatewayPorts yes in /etc/ssh/sshd_config

This does not appear to be needed - I'm guessing because I'm using ssh and not sshd to provide the forwarding.

Thanks for the help and hopefully Ben will have a fix Soon (tm)

Gorthog commented 5 years ago

is there any work around for docker?

sxd111 commented 5 years ago

is there any work around for docker?

try this, only for docker:docker-desktop-wsl2-tech-preview

Lips7 commented 5 years ago

Still not work in 18963😭😭😭

benhillis commented 5 years ago

Fixed in 18970.

sirber commented 5 years ago

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?

sirber commented 5 years ago

I rebooted. VM IP is now 192.168.229.123/20 localhost works!

sitepodmatt commented 5 years ago

Still broke in 18995. Was more reliable than before, but gave up on mysql after a few hours

sirber commented 5 years ago

Still broke in 18995. Was more reliable than before, but gave up on mysql after a few hours

I'm on 18995.1, it works using "localhost"

sitepodmatt commented 5 years ago

Still broke in 18995. Was more reliable than before, but gave up on mysql after a few hours

I'm on 18995.1, it works using "localhost"

Yes, it works "most of the time", if you boot up you can access anything run on WSL2 via localhost:port, but alas after a few hours it gets flakey again, not 100% for example already established long running connection such as persistent DB connection remain but struggles establishing new connections. It's much more reliable than earlier "localhost" featured versions

benhillis commented 5 years ago

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

sitepodmatt commented 5 years ago

Just another observation, after last night hibernation I can't access /mnt/c/ inside WSL2 or localhost:port from windows, the WSL2 kernel has been up evident by uptime. wsl --shutdown and opening terminal again worked. Perhaps suspension/hibernation has an impact?

enzanki-ars commented 4 years ago

I just switched to the insider previews, and switched my WSL distro to v2, and while it worked once, rebooting my system for something else caused this problem to begin.

$ ip addr
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 6a:05:33:e7:f5:ed brd ff:ff:ff:ff:ff:ff
3: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 06:79:51:51:a7:30 brd ff:ff:ff:ff:ff:ff
4: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:15:5d:8f:2e:0a brd ff:ff:ff:ff:ff:ff
    inet 172.24.170.19/20 brd 172.24.175.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::215:5dff:fe8f:2e0a/64 scope link
       valid_lft forever preferred_lft forever
5: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
    link/sit 0.0.0.0 brd 0.0.0.0

$ ip route
default via 172.24.160.1 dev eth0
172.24.160.0/20 dev eth0 proto kernel scope link src 172.24.170.19

I am able to ping out of ubuntu, but starting a simple python3 -m http.server 5000 is enough to trigger the issue for me. I can not connect to servers hosted in WSL via localhost or 127.0.0.1, but I am via the rotating IP. I am going to try wiping my WSL install and starting it from scratch is enough to fix the issue.

Edit: Forgot Windows build version:

> ver
Microsoft Windows [Version 10.0.19041.1]
> wsl --list --verbose
  NAME            STATE           VERSION
* Ubuntu-18.04    Running         2

Also was running Ubuntu 19.04 (updated 18.04 to 19.04 by changing lts to normal in /etc/update-manager/release-upgrades.)

enzanki-ars commented 4 years ago

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

sitepodmatt commented 4 years ago

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

enzanki-ars commented 4 years ago

I tried the wsl --shutdown trick, but even that was not enough to fix the issue. I am continuing to see if I can find something that can point to what's going on better.

enzanki-ars commented 4 years ago

It does look to be something sleep related. After a fresh reboot yesterday, I was able to access items via localhost, but today, I have been unable to after placing my system into sleep mode.

haljac commented 4 years ago

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.

sitepodmatt commented 4 years ago

Another one https://twitter.com/hmemcpy/status/1181258918682664961

douglascorrea commented 4 years ago

I have a issue that looks like this but was better described on #4347 which was closed as duplicated by this.

Anyways, here is the description:

Apache2 default site is working great from Windows with localhost.

When I create a new Virtual Host on Apache2, with a different tld like test.tld, it works from inside WSL2, but not from the windows machine.

I put test.tld on Ubuntu hosts file and also on Windows hosts file.

I'm describing it here again because @craigloewen-msft closed #4347 in favor of this issue, but I'm not seeing any description that looks like my use case here, so I'm documenting it.

douglascorrea commented 4 years ago

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

webash commented 4 years ago

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.

didibaba commented 4 years ago

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?

lackovic commented 4 years ago

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?
odbayar commented 4 years ago

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

I have PostgreSql 8.4 installed on an WSL distro (Ubuntu 18.04). If I kill a locked JDBC (running an alter table query on a read locked table using DataGrip) connection to the database server, all listening sockets on wslhost.exe disappears. Maybe wslhost.exe crashes?

gerryhd commented 4 years ago

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.

luxzg commented 4 years ago

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

emranhamdan commented 4 years ago

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

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

therealkenc commented 4 years ago

Fixed in 18970.

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

kangzj commented 4 years ago

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.

onomatopellan commented 4 years ago

@kangzj make sure you didn't hit this bug https://github.com/microsoft/WSL/issues/5298

kangzj commented 4 years ago

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

you are a life saver thanks heaps