microsoft/WSL

WSL2 localhost forwarding doesn't seem to work

impguard opened this issue · 209 comments

  • Your Windows build number: Microsoft Windows [Version 10.0.19008.1000]

  • What you're doing and what's happening:

Running Ubuntu 18.04 with WSL 2 enabled. For example running in Powershell

PS C:\Users> wsl --list --verbose
  NAME            STATE           VERSION
* Ubuntu-18.04    Running         2

Within a terminal for WSL2, I run a simple python HTTP server

❯  python -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

Then, trying to access the server from chrome within windows at localhost:8000. I get a "site could not be reached error"

  • What's wrong / what should be happening instead:

I believe it should be working since WSL2 supports forwarding. Unfortunately, I'm not familiar enough with the netstack between windows + WSL2 to diagnose. Let me know if I can provide more info. Thanks!

recently chrome added a change that forces all traffic to https. Try accessing localhost in firefox or setup ssl in your webserver for localhost.

@impguard localhost now works for you right?

We also added in some more improvements in build 19018, so if you're still experiencing the issue please upgrade and let us know if that resolves it. :)

Yes, it worked since the latest set of builds. Thanks!

This started happening to me recently. I'm currently running:

Windows 10 Insider Preview 19536.1000 (rs_prerelease)

I don't know why but vscode and wsl2 are having networking problems. When I get one working the other stops. 😢

microsoft/vscode-remote-release#2060

When I run nc -lk 0.0.0.0 3000 | bash I get no activity when I try to connect to it using chrome on the windows side.

Can confirm running on the latest Windows 10 Insider Preview 18536.1000 broke the localhost binding between WSL2 and the host. This was previously working for me before.

To reproduce:

On your WSL2 machine, as @meatherly pointed out:
nc -lk 0.0.0.0 3000

Attempt to access that machine using your favorite browser on your host Windows OS at localhost:3000, you'll see no activity.

Then, determine the WSL2 IP, which will generally be visible using ifconfig and looking for the IP assigned to eth0 (depending on your setup). Using that, go back into your browser at [ipaddress]:3000. You'll see the nc command now reports activity.

It appears the localhost binding between WSL2 and the host OS broke in the most recent release.

Updated to OS Build 19536.1000 yesterday, and the localhost binding broke to me too

same here - please fix!

Same! Went to bed with a functioning localhost:3000 and woke up without.

looks like this is the issue - #4769

@seanmetrix - indeed, fix is inbound.

Please fix this. If Chrome or Firefox runs localhost:3000 successfully, but VS-Code got issues in connectivity. and vice versa.

Seems like this problem is solved on OS build 19041.1

Right now i have this build. and i am facing issue in this build. This is the latest build so far i guess.
image

@talalsiddiqui Try to change your Insider settings to "Release Preview" and do Windows Update. That's what I'm using.

I have set my settings to FAST, plus now i changed to Preview Settings and click updates. there are no updates.
plus if i go back from updates to windows insider, it doesn't show me SELECTED SETTINGS. somehow it is not updating my settings to Preview

Do you know how i can go back to build 19041.

tony commented

I am in build 19536 and have to restart WSL 2 via powershell.

localhost URLs will stop working

Earlier builds work.

frct1 commented

19041 build. Localhost broken (switched to wsl2 to fix folder rename trouble, but there is another serious thing...)

I thought I was tripping. Glad I'm not the only one with this issue.

Any word on a fix release?

Regressed recently, tracking with #4769

I confirm that localhost:3000 (a meteorjs dev service) does not work on google chrome as @valeryan mentioned.
build 19541
Firefox work as expected.

build 19541. Localhost does not work on Chromium.

now updating to 19546. hope this build will fix the issue.

Version 10.0.19546.1 fixed it for me

frct1 commented

Nah
10.0.19546.1 allow connect to ports from windows but wsl can't reach windows ports...

Running Build 19541, localhost not working. Direct IP works.

Installing 19546.1000 now. Will edit in result. Edit: Working now.

on Build 19546 is not working as well.

I'm on 19541, I can't yet pin down what triggers it (though am trying to get some work done rather than debug this exclusively :) ).

From a fresh boot on my WSL2 system, I can invoke e.g. "docker-compose up" or "hugo server" and access my development systems on localhost: ports through a Windows-hosted browser.

Sometimes just hitting Ctrl+C and re-running the command will stop localhost from working. Sometimes I can stop and restart for hours/days before it will break.

I can still access localhost ports from inside Linux (maybe not surprisingly).

When it is broken, it doesn't seem to matter whether I address it via localhost or 127.0.0.1, it just times out.

At that point I just run powershell -command "Restart-Service lxssmanager", restart my Linux servers and localhost redirection is back again (less awful than a full reboot).

I'd love an explanation of how the redirect works from the Windows end.

I'm on 19541, I can't yet pin down what triggers it (though am trying to get some work done rather than debug this exclusively :) ).

From a fresh boot on my WSL2 system, I can invoke e.g. "docker-compose up" or "hugo server" and access my development systems on localhost: ports through a Windows-hosted browser.

Sometimes just hitting Ctrl+C and re-running the command will stop localhost from working. Sometimes I can stop and restart for hours/days before it will break.

I can still access localhost ports from inside Linux (maybe not surprisingly).

When it is broken, it doesn't seem to matter whether I address it via localhost or 127.0.0.1, it just times out.

At that point I just run powershell -command "Restart-Service lxssmanager", restart my Linux servers and localhost redirection is back again (less awful than a full reboot).

I'd love an explanation of how the redirect works from the Windows end.

Can confirm I have the same experience

For me this was fixed in 19551. Thanks! 🙏

Now on 19546 and localhost:3000 doesn't even work the first time I've start WSL, Docker etc. (system had been booted a couple of days). Restarting lxssmanager doesn't fix it now.

(edit...) It's working again in 19551! Will update if I can bait it into failing.

I promise it worked for me yesterday, today it doesn't work!

Today's build: 19041

EDIT: after a reboot, one of them works (I am running a Hadoop docker image). I can access some ports like 50070 (HDFS UI) but not others (9001 for Apache Zeppelin). I get a 404 error :S

Okay, apparently what happened is that the docker I was running never released one of the ports I was using (9001). This port is now always stuck in some PID 4 I cannot ever close.

I am running the same docker image on a new port binding 9002 and the service works fine. Now I am very curious whether I'll end up blocking 9002 as well

On build 19577, the problem is back and worse - I have one TCP port open on the Linux side and I can't connect to it any more, regardless of lxssmanager restarts. (I'll keep tracking this on #4769 I think).

localhost not working on build 19592

I confirm what thisguychris said, localhost domain doesn't work on build 19592

It's ok if we use WSL2 IP.

@thisguychris Same here on 19592.

localhost works on 19592

doesn't work for me on 19592

jlost commented

Broken on 19041

Same, broken on 19041.

riez commented

Try to enable Windows Sandbox & Windows Hypervisor on Windows features

juaan commented

@alchudry enable sandbox & hypervisor, restart it, and it worked like a charm, my version 19041.172

Restarting LxssManager from windows services worked for me. I hope it keeps on working 🤞

Restarting LxssManager from windows services worked for me. I hope it keeps on working 🤞

This worked for me like a charm! Really appreciated!

Enabling sandbox and hypervisor made the issue less frequent, but it still happens after a few hours. Restarting WSL or LxssManager works.

I first encountered the issue on 19041.

Encountered the same issue. Yeah, restarting WSL or the LxssManager helps, but I have to do it everytime I start my PC after shutdown. It's really annoying.

Encountered the same issue. Yeah, restarting WSL or the LxssManager helps, but I have to do it everytime I start my PC after shutdown. It's really annoying.

Well, for me I never had that issue again 🕺🏻

dsjm1 commented

I am on 19041.207

A couple of weeks back I was able to start a webserver on port 3000 under WSL2 and access it with Chrome. Yesterday I went to run that server again using port 3000 (then I tried 8080) but getting connection refused. Tried Firefox & Edge as well. Then went searching for known problems and got to this thread.

It is a pity that WSL2 keeps breaking in such important ways. I am having to revise our plans to switch to WSL2 for development at work. WSL2 is just not reliable enough. It seems we can't trust it to remain stable. Not being able to run systemctl cmds was bad enough, but not being able to access servers kills it.

N0xFF commented

A couple of weeks back I was able to start a webserver on port 3000 under WSL2 and access it with Chrome. Yesterday I went to run that server again using port 3000 (then I tried 8080) but getting connection refused.

I temporarily added additional 0 to all my ports and it keeps working (switching from 3000 to 30000).

This error is very hard for us to reproduce. If you're seeing it, please could you submit detailed logs for us? That will go a long way to help us fix it! Instructions on how to do so are here. Thanks!

dsjm1 commented

A couple of weeks back I was able to start a webserver on port 3000 under WSL2 and access it with Chrome. Yesterday I went to run that server again using port 3000 (then I tried 8080) but getting connection refused.

I temporarily added additional 0 to all my ports and it keeps working (switching from 3000 to 30000).

Thanks that did work (e.g. I used Rails s -b 0.0.0.0 -p 30000 and that allowed it to run). Unfortunately we were looking to use WSL2 as our dev environment in place of our prior Linux servers but because we use standard ports (such as 80, 8080, 3000) etc: we can no longer go ahead with the move to WSL2 so have now reverted back to Linux environment. The problems we keep hitting (1st with WSL1 now 2) have embarrassed those of us who were encouraging the move to WSL2 as a serious alternative for web dev.

I am still have this issue

I've submitted logs below

#4885 (comment)

I've just upgraded to build 19613.rs_prerelease.200417-1557.... issue persists

Also now upgraded to ubuntu 20.04 from the microsoft store.... issue persists.

#4885 (comment)

everything was working fine then suddenly this.. just commenting and following. hopefully this gets resolved soon.

dsjm1 commented
Pajn commented

Enabling sandbox & hypervisor worked for me as well

Seems like things work as expected on higher ports (3000, 8080) but not on lower ones (80, 443).

Not 100% what the issue is but the fix was manually adding my IP to my windows hosts file (C:\Windows\System32\drivers\etc\hosts):

  • enabling Windows Sandbox & Windows Hypervisor features did not fix the issue
  • restarting LxssManager did not fix the issue

Build Info:

  • Edition: Windows 10 Pro
  • Version: 2004
  • OS Build: 19041.208

@nowlena - If the port is in use on the Windows side the localhost forwarding won't work.

Does anyone having this issue have a network set up on the host windows 10 machine that is as straightforward as a laptop with one wifi card?

I'm looking for reasons why this happens to me on both my desktop and xps 13... Even with fresh install on win 10.

Both machines have a sonicwall VPN app on them (issue doesn't seen to correlate with the VPN being connected).

The desktop has both a wired and wireless NIC both connected to the same network but with different dhcp allocated IP addresses.

@timbophillips
I have a XPS 15 and same issue, just a single wifi card and no vpn.

Stopping everything and restarting LxssManagerseems to resolve the issue but it's indeed very frustrating to start multiple docker instances and build nodejs instances and find out that they're unreachable.

I'm thinking about setting up ssh on the wsl2 environment so I can create a reverse proxy instead of restarting everything.

Seems like things work as expected on higher ports (3000, 8080) but not on lower ones (80, 443).

Not 100% what the issue is but the fix was manually adding my IP to my windows hosts file (C:\Windows\System32\drivers\etc\hosts):

Can you explain a little bit more about your host entry? In my case, no port is working
Build info is,
19041.208

Enabling sandbox & hypervisor worked for me as well

In windows 10 home edition build 19041.208 I didn't find Sandbox option, do you know how i can enable it ?

Seems like things work as expected on higher ports (3000, 8080) but not on lower ones (80, 443).
Not 100% what the issue is but the fix was manually adding my IP to my windows hosts file (C:\Windows\System32\drivers\etc\hosts):

Can you explain a little bit more about your host entry? In my case, no port is working
Build info is,
19041.208

Again, generally localhost worked fine when running apps. I did however have to manually add the WSL IP to my window's host file so that all ports worked as intended.

My process currently looks like:

  1. run ifconfig within WSL to get my IP
  2. close WSL
  3. open PowerShell and terminate all instances of WSL via wsl -t Ubuntu-18.04
  4. update windows host file with the IP from ifconfig (C:\Windows\System32\drivers\etc\hosts)
  5. re-open WSL
  6. check that WSL is using updated host file content via cat /etc/hosts

Hope this helps!

Restaring LxssManager worked for me, but looking forward to a solution to the problem

Encountered this issue on 19041.208.
Cannot reach WSL2 services from Windows-land after system started, have to reboot to make it work every time.

Can we reopen this issue to keep it tracked?

Also, I still have this issue.
connect wsl server with localhost is correctly work, but connect wsl with external ip is not work.
(already check connect windows' server(not in wsl) connect with external ip)

  • version: 2004 preview build

19624 the same and restarting wsl or LxssManager do not work for me.

the same here 19041.208, I need to restart my pc.

Having the same issue, it was working when first setup but after installing Docker for Windows with WSL2 engine it stopped working.

To confirm with the hosts file change, are you guys replacing your current localhost IP (127.0.0.1) with your WSL vm IP (172....)? Ideally this is not what I want to do and would think it would cause other problems. Or is it being added as a different line?

EDIT: Just installed all this today, WSL 2 on Windows 2004 (19041.264), Docker Desktop Edge 2.3.0.1

EDIT 2: Scratch that, 1 more reboot fixed it!

Why is this closed?

blkc commented

I was deleting the contents within settings.json for the Terminal because Powershell deprecated globals and after that Ubuntu stopped connecting to localhost.

microsoft/terminal#5458

Restarting pc resolved the issue.

I started getting "The connection was reset" error all of a sudden for WSL2 Ubuntu. Restarting WSL/Lxss manager didn't help. However running the script from #4150 seems to have resolved the issue.

It might be related to windows firewall, I have no idea what was the exact cause or solution, I'm just leaving this information here in case somebody is desperate. Your mileage may vary.

note: you have to modify the script to list the exact ports you want to use

EDIT:
It turns out that running that script locally was the start of my woes, removing all port forwarding rules that were set with it fixed the issue. I believe it's because the script rerouted all my traffic and caused it to be checked by firewall which then blocked it.

Any news about this?

I'm getting tired of having to restar Lxss everytime.

Any news about this?

I'm getting tired of having to restar Lxss everytime.

I'm also waiting in earnest for a solution

Currently on my machine on build 19592.1001 it appears that localhost only works with ports 4754 and above.

If you are having trouble with a nodejs app that utilizes react-scripts, set the environment variable PORT to your desired port number:
export PORT=8888

Other applications should have similar support for setting the port.

Randomly happened for me, I need to restart the WSL wsl.exe --shutdown

Experiencing similar issue. Went to bed with it working last night, and today... dololo.

Microsoft, please fix.

[EDIT]
Suggestion by @khanhhuy worked for me.
Running wsl --shutdown and starting it up again resolved the issue (for now -_-)

[EDIT2]
It seems I have to restart WSL everytime I boot up now

The solution from @nowlena seems to work.

But the stargane thing that yesturday I was able to just open localhost to see the web page from WSL 2

But today, i need to open exact addess from ifconfig e.g. http://192.168.78.32/ to see page from WSL2

No changes were applied to /etc/hosts on both windows and wsl2

I am starting to have this same problem with the wsl2/Ubuntu-20.04 on windows 2004 May update (insiders slow).

So opening a folder on Ubuntu with VScode and having a single .html file and when I try to open the file in chrome, Firefox, on localhost:5500 using the live server extension I get the can't access this page and I've tried all I know but didn't work. It was working fine before the upgrade to windows 2004. Any help?

2: Scratch that, 1 more reboot fixed it!

restart wsl seem to work for me to.

Rebooting wsl.exe seem to have temporarily fixed the issue. Thanks, @khanhhuy.

On the side note, Microsoft this needs to be fixed.

Edit

I am using the official release:

Edition - Windows 10 Pro
Version - 2004
Installed on - 27/05/2020
OS Build - 19041.264
Experience - Windows Feature Experience Pack 120.2202.130.0

Can we re-open this?

To everyone subscribed on this thread: Turn off Fast Startup.

Until the bug is fixed turning off Fast Startup seems best solution for now on build 19041.
https://www.tenforums.com/tutorials/4189-turn-off-fast-startup-windows-10-a.html

We're tracking the fast startup issue in #5298.

wsl --shutdown does not help for me. I am using latest 2004 build

Any ideas why localhost would work but not 127.0.0.1 or any other alias that points to that loopback IP?

Turn off the firewall and then turn it on.

# 1: run Powershell as Administrator
# 2: disable Firewall
Set-NetFirewallProfile -Profile Domain, Public, Private -Enabled False
# 3: enable Firewall
Set-NetFirewallProfile -Profile Domain, Public, Private -Enabled True

Why are there still 10 different threads on this going back 6+ months? Every thread has a different solution - not to mention dozens of blog and forum posts.. This is ridiculous - it's localhost forwarding between a VM and host system. Microsoft cannot expect to be taken seriously by the Linux community if they can't get this stuff right.

Why are there still 10 different threads on this going back 6+ months? Every thread has a different solution - not to mention dozens of blog and forum posts.. This is ridiculous - it's localhost forwarding between a VM and host system. Microsoft cannot expect to be taken seriously by the Linux community if they can't get this stuff right.

Agreed. Ran in to this this morning (out of nowhere).

To resolve the issue I had to reboot WSL:

  • Open your Terminal as an admin and run Get-Service LxssManager | Restart-Service

the @glenmcrae solution works.
But after execute the command, Docker Desktop I couldn't start the distro "docker-desktop-data" I had to manually start it with the command
wsl -d docker-desktop-data
then restart docker dekstop

I was able to resolve by setting the following environment variable in my shell (I use fish)

set -gx DISPLAY :0

It doesn't really make much sense to me why this variable would be set incorrectly - and I don't know if this will fix the issue for everyone but maybe it will fix for someone.

None of the solutions here work for me, unfortunately. I can't wrap my head around how such a basic thing might be shipped broken.

@krojew - I would suggest creating a new issue. You seem to be hitting something different.

@benhillis that is possible, but the symptoms are very similar - I cannot access any port in WSL2 on 127.0.0.1.

@krojew I think I'm having the same problem as you. I can't use 127.0.0.1 after upgrading to WSL2.
Some applications that I'm using just automatically switch "localhost" (which works) to 127.0.0.1 and I get "connection refused".
Did you find any workaround?

It's really disheartening something like this is happening, I was actually really excited with Window's may update, but now I think I will revert back to WSL 1.

@benhillis do you know if a fix for this problem is coming soon?
Will I have to be in Windows test program to get it?
Because last time I joined I had to reset my Windows to get out, it was a pain..

@renatop7 Take a look at this
https://docs.microsoft.com/en-us/windows/wsl/compare-versions#additional-networking-considerations

Servers running in WSL2 should work fine if they are bind to 0.0.0.0 instead of localhost/127.0.0.1

@onomatopellan That is not the same - and can open you up to security issues if you are not on a trusted home/office network.