WSL: Unable to connect to local server started in WSL 2 from windows host machine
Please use the following bug reporting template to help produce issues which are actionable and reproducible, including all command-line steps necessary to induce the failure condition. Please fill out all the fields! Issues with missing or incomplete issue templates will be closed.
If you have a feature request, please post to the UserVoice.
If this is a console issue (a problem with layout, rendering, colors, etc.), please post to the console issue tracker.
Important: Do not open GitHub issues for Windows crashes (BSODs) or security issues. Please direct all Windows crashes and security issues to secure@microsoft.com. Ideally, please configure your machine to capture minidumps, repro the issue, and send the minidump from "C:\Windows\minidump".
Please fill out the below information:
-
Your Windows build number: (Type
verat a Windows Command Prompt) 10.0.18922.1000 -
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.)
-
What’s wrong / what should be happening instead: I am unable to connect to my node server (started with
firebase servein my WSL 2 Ubuntu distro). I understand thatlocalhostcan not be reached from windows for wsl 2 server yet. Therefore, I followed the instructions as shown in this Microsoft blog post. However, I am still not able to connect to my server. I also added a rule to my firewall rules to allow connections to any port for my WSL 2 ip address. -
Strace of the failing command, if applicable: (If
some_commandis failing, then runstrace -o some_command.strace -f some_command some_args, and link the contents ofsome_command.stracein a gist here). -
For WSL launch issues, please collect detailed logs.
See our contributing instructions for assistance.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 43 (4 by maintainers)
wsl --shutdownhelped me in Win 2004I solved this by port forwarding.
on WSL 2 run the following commands
ifconfigthis will show your ip address. (my ip address in WSL 2 was 172.23.18.82 )uvicorn app.main:app --host 0.0.0.0 --port 8000 --reloadthis will run my fastapi server in port 8000on windows run the following command as Administrator
netsh interface portproxy add v4tov4 listenport=8000 listenaddress=0.0.0.0 connectport=8000 connectaddress=172.23.18.82this will do the magic.now I’m able to access this service from any device which are connected to the same network
note : you need to use your windows local ip address (you can use ipconfig cmd) while connecting from other devices.
Thanks! Can confirm it helps in Win 2004!
Unable to connect in build 18963 / WSL 2. Not sure if this because of this build or not.
Why would you close this @PritamSangani ? A workaround is not a fix.
Also experienced this. This problem occurs randomly and I can not reproduce it on purpose. But
wsl --shutdownworks for me.I have exactly the same problem. Only solution is a WSL restart. Windows version 20H2
This randomly pops up for me, but enough to be annoying as hell. Like 70/80% of the time.
wsl --shutdownhelps, but is a pain in the butt, especially when I have multiple tabs open in terminal, Docker running and VS Code in remote. Which is all the time.No upgade from wsl1, have this issue since I moved over to WSL2 from linux about a year ago. Other than this, the experience is good, but this needs to be fixed!
@Balaji2810 thx for your comment. It helped me to find the actual ip of the wsl instance to connecto to the websocket
wsl --shutdownnot working for me (WSL2 / Win 21H2). As @nycynik said. it will immediately prompt to restart Docker backend. No matter if I restart it or not, I still can’t accesslocalhost:4200. If I try serving the app on another port (say 3000 or 4000 -ng serve --port 3000) then it works, I can access the app. But I haven’t done anything to block 4200, not sure why it’s not working (and I’ve also randomly tried 4201, 4202 - also not working… not sure why ports with 42xx aren’t working).P. S. I’m not running inside a Docker container or anything. I just have a web app under the WSL file system which is served (by default) on port 4200. If I try serving the app on random ports, not sure why some ports work (randomly tried 3000, 4280) while other ports don’t work (4200, 4201, 4202).
EDIT: Found this post: https://hungyi.net/posts/wsl2-reserved-ports/ saying that some dynamic port ranges are “aggresively reserved by Hyper-V” (WHY? 😭) Indeed, running
netsh int ipv4 show excludedportrange protocol=tcp(in a Windows shell - like Command Prompt, NOT in WSL2/Ubuntu) gave me this output:So, in my case port 4200 is within the reserved range [4116 - 4215].
Same here,
wsl --shutdown.Thank you! I’ve had the connectivity problems despite latest builds and setting up forwarding and everything. It seems this was the only thing which fixed the issue.
For what it’s worth, I created by distro (Ubuntu) before I upgraded to WSL2, then converted it to WSL2 and noticed problems. After shutting down all seems to be fine and Flask apps run again.
I am running a flask app in WSL2 and am not able to connect to it via chrome on the windows 10 host. I bound to
0.0.0.0and it still didn’t work.For sanity, I also ran
sudo nc -l 80and made an http request on port 80 to localhost from windows and did not see the request reachncMicrosoft Windows [Version 10.0.19041.172]
I got this working: so apparently there was some confusion about these posts regarding the linux file system, I was creating my project in the ubuntu terminal under /mnt/c/Users/seand , thinking this was the linux file system, but actually it seems that is some kind of virtualized service… if I go to /home, and create a project my personal folder, it works great… I guess it’s the difference between windows file system and linux file system and it’s new to WSL 2. The performance difference is dramatic also.
I’ve been writing about WSL 2 on my medium blog, I’ve added the notes there: https://medium.com/swlh/wsl-2-docker-edge-tech-preview-native-linux-containers-w-o-emulation-b41667e6dbef