WSL2 not forwarding ports
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)) docker/for-win#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.
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.
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
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?
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.
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?
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! :)
Apologies if I'm misunderstanding, but sounds like https://github.com/microsoft/WSL/issues/4150#issuecomment-504209723 - case where WSL doesn't use Host's IP (with Powershell-script scolution)
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: https://github.com/microsoft/WSL/issues/5368 https://github.com/microsoft/WSL/issues/5211 https://github.com/microsoft/WSL/issues/4983 https://github.com/microsoft/WSL/issues/4885 https://github.com/microsoft/WSL/issues/4851 https://github.com/microsoft/WSL/issues/4769 https://github.com/microsoft/WSL/issues/4729 https://github.com/microsoft/WSL/issues/4671 https://github.com/microsoft/WSL/issues/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.
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
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.
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.
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.
Please turn off Fast Startup and restart Windows. If the issue continues at least we can discard bug docker/for-win#5298
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.
Re-enabled fastboot, still working. Closing this issue - apologies for the concern :)
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?
@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)
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...
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 docker/for-win#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
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: https://github.com/microsoft/WSL/issues/4636#issuecomment-706967150
Also, make sure you've configured the windows firewall. Windows Start -> search for "advanced"