Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WSL2 not forwarding ports #5439

Closed
dalgibbard opened this issue Jun 18, 2020 · 28 comments
Closed

WSL2 not forwarding ports #5439

dalgibbard opened this issue Jun 18, 2020 · 28 comments
Assignees
Labels

Comments

@dalgibbard
Copy link

Environment

Windows build number: Microsoft Windows [Version 10.0.20150.1000]
Your Distribution version: Release:        20.04
Whether the issue is on WSL 2 and/or WSL 1: Linux version 4.19.121-microsoft-WSL2-standard (oe-user@oe-host) (gcc version 8.2.0 (GCC)) #1 SMP Thu May 14 20:25:24 UTC 2020

Steps to reproduce

Associated Feedback Hub link: https://aka.ms/AA8qyij

Initially after upgrade to Win 10 20150, my existing WSL2 build was working great.
I unregistered that WSL distro, and on recreating it, I could no longer connect to ports hosted in WSL, from Windows.

I've uninstalled the WSL features (including Containers + Hyper-V etc), and the Ubuntu apps that came from the Microsoft Store, and uninstalled VSCode (the application i'm trying to use primarily)

Reinstalled all these, fresh WSL2 install of Ubuntu 20.04. Installed VSCode + the WSL remote extension, and it installs and starts the VS Code server listening on a particular port; but errors that it cannot connect to it.
This port is accessible from within WSL, but not from Windows (eg. browser etc - it responds with a 404 at least in WSL, but ERR_CONNECTION_REFUSED from a browser in windows.)

Expected behavior

WSL should forward ports of running services, to be accessible from Windows (as per previous behaviour).

Actual behavior

Ports opened in WSL are not accessible from Windows.

lxcore_logs.zip

@ad-on-is
Copy link

I'm on 20150 and no problem here. VSCode Remote and all ports work like they did prior to the update. I didn't recreate the VM though. Also I'm running Docker Desktop for the web-dev ports.

@xddz9
Copy link

xddz9 commented Jun 19, 2020

Same issue on 19042. I've combed through a lot of similar issues here but no one solution seems to fix it for good (including disabling fast refresh and rebooting Ubuntu with wsl --shutdown).

It was working fine for months, then it broke out of nowhere. It randomly works again with some ports, only to stop working altogether minutes later, so it's really hard to reproduce the problem.

I've resorted to this for now.

Microsoft Windows [Version 10.0.19042.330]
Ubuntu 18.04.4 LTS

@ad-on-is
Copy link

There was an port-forwading issue a while back, which was got resolved. Just some thoughts. Did you check your firewall settings? Maybe some third-party interferes there?

@xddz9
Copy link

xddz9 commented Jun 19, 2020

There was an port-forwading issue a while back, which was got resolved. Just some thoughts. Did you check your firewall settings? Maybe some third-party interferes there?

Not a firewall issue for sure. Looking at other open issues it seems like I'm not the only one having this problem still. I apprecciate this sort of thing is pretty complex to debug, so I'm just chiming in to let the devs know it might be worth looking into.

Disclosure: I haven't tried reinstalling my distro, but I'm running the standard Ubuntu 18 with Node.js to serve a web server on different ports (300x, 500x, 900x). VS Code is running just fine and so is everything else, I just can't reliably get a web server on Linux to load on Window's localhost. (Yes, it's running on 0.0.0.0).
I'm confident it's not a problem in my app, I've tried with a couple generic web servers to no avail, so it's definitely a networking issue.
I might be doing something terribly wrong on my side, but since it appears to work on an off when rebooting windows/linux, and since other people have reported the same thing, I felt like adding my 2 cents as well.

@ad-on-is
Copy link

I'm confident it's not a problem in my app, I've tried with a couple generic web servers to no avail, so it's definitely a networking issue.

Thats for sure.

TBH, I'm running everything in Docker containers, NodeJS servers too. This has the benefit that it allows me to test stuff from my smartphone, accessing 192.168.1.100:3000 (Windows PC). And this is a network functionality that WSL2 does not offer, unfortunatelly. Maybe it's somehow correlated.

Could you test running the servers inside Docker containers?

@xddz9
Copy link

xddz9 commented Jun 19, 2020

This has the benefit that it allows me to test stuff from my smartphone, accessing 192.168.1.100:3000 (Windows PC). And this is a network functionality that WSL2 does not offer, unfortunatelly. Maybe it's somehow correlated.

That was my use case as well. I actually managed to get it working before, back when localhost forwarding was working properly by running this on PowerShell as admin:

netsh interface portproxy add v4tov4 listenport=5000 listenaddress=0.0.0.0 connectport=5000 connectaddress=127.0.0.1
netsh interface portproxy add v4tov4 listenport=5001 listenaddress=0.0.0.0 connectport=5001 connectaddress=127.0.0.1

(I got this snippet to work with this solution, but it works only for one port. Maybe related to this issue, which was closed but other people still report as not working)

Worked like a charm with a simple firewall rule on Windows Defender to open those two ports.

I might give Docker a go, but honestly I doubt it will work, and if it's indeed a networking issue I'm way out of my depth here. Thank you for your comments though! :)

@lefnire
Copy link

lefnire commented Jun 19, 2020

Apologies if I'm misunderstanding, but sounds like #4150 (comment) - case where WSL doesn't use Host's IP (with Powershell-script scolution)

@xddz9
Copy link

xddz9 commented Jun 19, 2020

Apologies if I'm misunderstanding, but sounds like #4150 (comment) - case where WSL doesn't use Host's IP (with Powershell-script scolution)

Definitely related, but that wasn't supposed to be needed as of 18945, as evidenced by the fact that it does work by default on newer builds, though unreliably.

I might be wrong but I'm under the impression that there are several open issues reporting the same problem in different terms, with multiple fixes that work for at least some people but not for others.

From a quick search:
#5368
#5211
#4983
#4885
#4851
#4769
#4729
#4671
#4636

@craigloewen-msft mentions in one of those that a similar issue is being tracked here, but with so many open issues and possible solutions there are bound to be other people out there as confused as me. I think what's most troubling is that it seems to randomly stop working after a while, so what seemed like a fix yesterday suddenly doesn't work anymore today.

@dalgibbard
Copy link
Author

I'll check some more ports on Monday to see if it persists for all ports/services; but my issue is specifically that I've uninstalled everything and reinstalled, fresh distro, no restore from backups or anything weird, and it just isn't working.
For intermittent port functionality, or on other builds, it's probably best to comment on the other mentioned tickets to keep issues specific.

I have no firewalls other than Windows default stuff; no issues or errors reported from antivirus etc.
Hopefully the debug information provided in this issue + the feedback hub captures are helpful.
Happy to jump on a call to assist with debug too as needed etc

@dalgibbard
Copy link
Author

I'm on 20150 and no problem here. VSCode Remote and all ports work like they did prior to the update. I didn't recreate the VM though. Also I'm running Docker Desktop for the web-dev ports.

Please read the ticket description. Mine was also working fine after the upgrade. It ceased working when I tried to wipe my existing distro and reinstall fresh. The new distro does not map ports.
If you fancy risking your deploy, feel free to unregister your distro and reinstall to test... But probably use the original windows 7 imager to take a system image first! Note that restoring the distro from a tar backup does not work.

@ad-on-is
Copy link

ad-on-is commented Jun 19, 2020

If you fancy risking your deploy, feel free to unregister your distro and reinstall to test... But probably use the original windows 7 imager to take a system image first! Note that restoring the distro from a tar backup does not work.

Since I have a lot of work going on, I unfortunatelly cannot unregister/reregister, etc. my current distro... but...

The new distro does not map ports.

I've installed a fresh new kali-distro from the Windows Store, installed NodeJS, run a quick server on port 9070 and localhost:9070 works as expected. I will keep an eye on it for the next few days, and report if it still works or not.

@ad-on-is
Copy link

As usually, I've put my PC in hybernation mode overnight, I tested the NodeJS server after waking up my PC, and it still works.

@onomatopellan
Copy link

Please turn off Fast Startup and restart Windows. If the issue continues at least we can discard bug #5298

@dalgibbard
Copy link
Author

dalgibbard commented Jun 20, 2020

OK I already had fast boot turned off, so it wasn't that.

I went into 'Windows Defender Firewall with Advanced Security', deleted all 'Inbound Rules' for 'node', and now it seems to be working. Though I don't recall getting a firewall prompt for it lately, I guess it must have been that? Even now that it's working, I don't see any entries in Windows Defender firewall Inbound Rules for it though.

@dalgibbard
Copy link
Author

Re-enabled fastboot, still working. Closing this issue - apologies for the concern :)

@b-hayes
Copy link

b-hayes commented Aug 10, 2020

I can't get any services such as node js to work unless I run the commands via vscode and then ctrl+click the link from there. If I simply run the command and try accessing in the browser it doesn't work until Ictrl+click the link from vscode first.

Is this intentional, ie wsl is dependant on vscode to open ports? or should ports just open when run via terminal without vscode?

@pavloskl
Copy link

@b-hayes thank you. I had this problem scratching my head for hours. I remember with WSL(1) I never had this problem. But now I could not access my VS Code project running on WSL2 *Ubuntu 20.04).
What you proposed solved my problem. It seems that when you click on the URL in the terminal the port is not what it seems. For example my nodejs app runs on port 3031 but when clicking on the URL it shows a different port on the browser URL.
https://prnt.sc/u3fzib
https://prnt.sc/u3fztp

I don't know why this is happening but I guess is some kind of internal port forward that vscode (or node) does on WSL2 ?!?!

Hopefully your solution will help others solve their problem temporarily until the actual port that the app is running is working (like it did with WSL1)

@mo3rfan
Copy link

mo3rfan commented Sep 3, 2020

Elaborating on the above comments, starting a service on 0.0.0.0 and doing the left ctrl + click doesn't work. You have to start it on 127.0.0.1 and then do the left Ctrl + click on the URL from the vs code terminal for it to work. And yeah, the ports do show up different when they open in the browser. Looks like there's some automatic mapping going on...

@Mart-Bogdan
Copy link

Mart-Bogdan commented Sep 16, 2020

Same problem
Microsoft Windows [Version 10.0.19041.508]

>wsl -l -v
  NAME            STATE           VERSION
* Ubuntu-20.04    Running         2
$ uname -a
Linux DESKTOP-45IIJ91 4.19.104-microsoft-standard #1 SMP Wed Feb 19 06:37:35 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

I've launched docker

service docker start
docker run -d \
    --name some-postgres \
    -e POSTGRES_PASSWORD=mysecretpassword \
    -e PGDATA=/var/lib/postgresql/data/pgdata \
	-p 5432:5432 \
    -v /home/user/pgdata:/var/lib/postgresql/data \
    postgres

and I can not access runing container on localhost:5432

I've checked ifconfig inside WSL and tryed IP of eth0: 172.17.129.254 but same no results

On loclahost: conneciton refused
On direct ip: connection timeout

@MichaelJCole
Copy link

MichaelJCole commented Dec 1, 2020

This is the first search result on google. You maybe interested in running this in powershell:

wsl --shutdown

and/or performing a "network reset" as described here: #4636 (comment)

Also, make sure you've configured the windows firewall. Windows Start -> search for "advanced"

@taffit
Copy link

taffit commented Dec 10, 2020

What helped in my case was setting the ranges for dynamic ports to a higher range, as described in #5306 (comment) and docker/for-win#3171 (comment)

@MichaelJCole
Copy link

This is an intermittent issue for me that re-appeared after the last WSL update. What work for me to fix it was Start -> Settings -> Network & Internet -> Network Reset (blue link)

@csMACnz
Copy link

csMACnz commented Feb 23, 2021

I've been around in circles with this and haven't found a single suggestion that helped so thought I should post here what did eventually work.

My symptom was that after many requests to a port mapped through docker (wsl2 mode) traffic would just stop with connection refused, closed, reset etc errors on the ipv6 resolved local host (::1) wsl --shutdown reset the problem until more request load retriggers it again, reliably. 127.0.0.1:49153 keeps working while localhost:49153 hangs.

I ran netstat -ano | findstr :49153 (49153 being my mapped port)
I noticed there were 3 LISTENING

  TCP    0.0.0.0:49153          0.0.0.0:0              LISTENING       20304
  TCP    [::]:49153             [::]:0                 LISTENING       20304
  TCP    [::1]:49153            [::]:0                 LISTENING       22920

20304 is Docker Desktop Backend which is what is meant to be listening, and why ipv4 works.
22920 was one of the wslhost.exe (Microsoft Windows Subsystem for Linux Background Host) processes running - there are a couple.

I killed the offending process matching the id and the problem was fixed. I couldn't reproduce until i shutdown (or restart) the machine.

It is possible this is related to the Fast Startup issue, but i have this disabled according to the UI #6162.

hope that helps someone stuck on this, maybe even inspire someone on where to look for a permanent fix even?

@csMACnz
Copy link

csMACnz commented Feb 23, 2021

Looks like I have very specifically hit this issue, now that I know what I am looking for:
#4851

@N0xFF
Copy link

N0xFF commented Jun 22, 2021

This has the benefit that it allows me to test stuff from my smartphone, accessing 192.168.1.100:3000 (Windows PC). And this is a network functionality that WSL2 does not offer, unfortunatelly. Maybe it's somehow correlated.

That was my use case as well. I actually managed to get it working before, back when localhost forwarding was working properly by running this on PowerShell as admin:

netsh interface portproxy add v4tov4 listenport=5000 listenaddress=0.0.0.0 connectport=5000 connectaddress=127.0.0.1
netsh interface portproxy add v4tov4 listenport=5001 listenaddress=0.0.0.0 connectport=5001 connectaddress=127.0.0.1

(I got this snippet to work with this solution, but it works only for one port. Maybe related to this issue, which was closed but other people still report as not working)

Worked like a charm with a simple firewall rule on Windows Defender to open those two ports.

I might give Docker a go, but honestly I doubt it will work, and if it's indeed a networking issue I'm way out of my depth here. Thank you for your comments though! :)

Periodically I couldn't connect to this port (OS build 19043). How I fix it:

  1. Enable IPv6 anywhere.
    1. In Windows vEthernet (WSL) network adapter.
    2. Removes generateHosts = false from /etc/wsl.conf.
    3. Uncomment IPv6 hosts in /etc/hosts.
    # The following lines are desirable for IPv6 capable hosts
    ::1     ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    
  2. Run service additionally with IPv6 support on ::1 IP address (127.0.0.1 alternative).
  3. Use v4tov6:
netsh interface portproxy add v4tov6 listenaddress=0.0.0.0 listenport=3000 connectaddress=::1 connectport=3000 protocol=tcp

@rafaelbernabeu
Copy link

rafaelbernabeu commented Sep 26, 2022

OK I already had fast boot turned off, so it wasn't that.

I went into 'Windows Defender Firewall with Advanced Security', deleted all 'Inbound Rules' for 'node', and now it seems to be working. Though I don't recall getting a firewall prompt for it lately, I guess it must have been that? Even now that it's working, I don't see any entries in Windows Defender firewall Inbound Rules for it though.

This works for me!! Thanks

@bunderas
Copy link

I had some issues with the WSL2 port forwarding. I checked all the solutions, turn the hibernate off, etc.
It worked for a while, but after some time it stops working.

What worked for me is using different listenport and connectport in the 'netsh interface portproxy add...' command.

@scruel
Copy link

scruel commented May 10, 2023

To prevent rebooting the whole system, you can try: wsl-restart.bat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests