-
Notifications
You must be signed in to change notification settings - Fork 822
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
[WSL 2] WSL 2 cannot access windows service via localhost:port #4619
Comments
I was able to use graphic applications in WSL using XMING and Export DISPLAY=:0.0 However, once I upgraded to WSL2 and switching to Export DISPLAY = 172.28.801:0 (my host#) it failed to connect Things tried: downloading VcXsrv Here is a typical error message QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-lw' qt.qpa.screen: QXcbConnection: In #4106 you advise to run "vcxsrv -ac" from the bash command line and at least one user accepted that as a solution. I downloaded vcxsrv.exe from sourceforge as an .exe outside of bash, and I see no way to execute it from bash Many thanks |
is there any update? |
solved #4793 (comment) |
@numeric-lee that's not really solved - that's a workaround. In WSL1 you could hit:
From the WSL environment and that would access the windows service directly without having to:
Use cases are common including:
A different IP address, I could live with, though I would suggest that WSL should expose it via |
This is because despite the name (WSL=Windows Subsystem for Linux), WSL2 is not a Windows subsystem like WSL1 and Interix were. WSL2 is a lightweight Hyper-V-based virtual machine with its own separate network interfaces, IP addresses, etc. Localhost and 127.0.0.1 are host local references and having more than one host, they refer to different things in Windows host and Linux guest host. WSL2 already supplies some sort of special casing including accessing the Linux guest console/login for Windows terminal access to the Linux guest and means of accessing both the Windows host and Linux guest filesystems from both sides ("9p" Linux mounts vs. IFS redirector and UNC "\wsl$"). The best solution would be to generalize this to allow a configured list of port forwarding options. Then one could forward the server socket to the Linux guest allowing the guest to then believe and refer to the server as if it were local to the Linux guest host. For security and performance reasons, it would be good to use virtual machine specific socket system like that used by wslbridge2 (hvsocket). Another possibly simpler solution would be to come up with something that uses WSL_INTEROP sockets to create a generalized tunneling solution. I am not sure how stable they are though as they seem to depend on WSL sessions (perhaps a Windows service that opens a WSL session and sets up all the configured tunneling and then keeps the session open). |
To anyone needing a workaround, run this on the Windows side:
and this on WSL2:
Replace the IP with your windows IP. Edit: I may have commented on the wrong issue. It was one on the same error but related to Android studio. |
To workaround this on selenium+chromedriver, I´m running a socat on wsl2 relaying requests to |
Every time that I go back to wsl2 and give it a try, the same happens again. After updating windows, I'm not able to access localhost:port. Not event using wsl internal ip:port. Looks like it is a recurrent problem. Why we need to face it every update? |
@andreialecu That only works for one particular service related to Android Debug Bridge. @raelmax Yes, that is sort of what I suggesting by my comment about using WSL interop sockets. I had envisioned something with ncat (from nmap) but netcat and socat probably can also work. I thought about ncat because there should be both Windows and Linux versions available (sadly Windows Security identifies this as a "hacker tool" potential threat and quarantines it unless specifically allowed) so it was not too big of a stretch to just use pipes and depend on WSL interop to allow such to automatically cross the host-guest boundary. What commands are you using? How do you handle the Windows host side of things? Which Linux distribution are you using that has socat or did you compile that yourself? Providing the commands you are using would be helpful. Thank you. @jordaofranca I believe WSL1 is still available in later versions of Windows 10 but you do need to set the WSL version for each distribution (and you can set the default version for new distributions too). Obviously moving back to WSL1 is also just a workaround and not a solution. |
@Uzume I´m using Ubuntu 18.04 and I´ve installed My use case is as follow:
Since I can´t update my code to access chromedriver via an IP address, he always tries access chromedriver via the localhost. So to put chromedriver to run on localhost, I´m using the following command to relay requests from LOCALHOST:PORT to WINDOWSHOST:PORT. Command: So each request that I did from my wsl2 machine to "localhost:9515" is relayed to "ipaddress:9516". On the windows side, I just need to open the 9516 port on my firewall. If I can help you with anything else, reach me out! :) |
@raelmax It seems you are not using WSL interop sockets at all and instead just using socat to forward Linux guest client (selenium) accesses to a localhost port to a Windows host IP and port (which also requires that the chromedriver server allows connections from IPs other than its own localhost). I imagine you could have just as easily used netcat or ncat (from nmap package). Thank you, I appreciate the feedback. |
still same issue as of end of March 2020 ... WSL2 [Ubuntu 18.04] on WINDOWS 10 Pro cannot access a defined DISPLAY (no matter how I define it if via localhost:0, or the WSL2 vEthernet adapter IPv4 address) on X-Server (neither Xming nor VcxSrv with and without -ac parameters) running on WINDOWS host system to display any graphic apps (eg xeyes or xclock). |
I added some documentation for the issues that I found while trying to get DISPLAY working with WSL 2: https://github.com/cascadium/wsl-windows-toolbar-launcher#troubleshooting Ideally though, yes localhost would work and no firewall exceptions would be required (which I would suggest this ticket is primarily for). |
WSL2 networking really is a clusterfuck. |
@fquinner I created some scripts, which can be run either when boot or from WSL or from Administrator Powershell. They launch, in a transparent manner. a windowed KDE, using VcXsrv and the accompanying Xlaunch. It works very well and has no problems with reboots or shutting it down and back again A working version has been uploaded at: |
Hey thanks for this solution. What should I change on the code side to get it to use port 9516? |
I was stuck on this issue until I disabled Windows Firewall for Public Networks. Obviously this isn't the most secure solution, but one must understand that WSL2 networking is based on NAT for outbound (egress) connectivity. As a result, from the standpoint of Windows this is an external machine attempting to connect to its IP on the virtual network that WSL2 uses. So from Windows perspective, this is an external host trying to connect, and it's not on the private network as that's the one established via your Ethernet or Wi-Fi connection to your router or access point. The WSL2 network is, in essence, a public network so Windows is being cautious by default and not allowing inbound connectivity from WSL2. To do this properly, in a secure fashion, you need to find the Firewall rule under public networks that is likely disabling your access. For me, it was two entries for "VirtualBox Headless Frontend" that were blocking access for some reason. Once I killed those, I was able to access again. The networking between WSL2 and Windows is fine; it's likely Windows Firewall that is your issue. |
Seing this: Is it not possible for microsoft to solve this firewall issue? This issue has been open for 2.5 years now and has probably cost hundreds of dev hours. Please, microsoft, teach your firewall better manners! |
I just saw that microsoft has labeled this as Seing that this has been downvoted, I will argue why this is a bug, not a feature: This tool is named WSL2. It is obviously a newer, shinier and better version than it's predecessor, WSL(1). BUT: It does not work, at least not in the way many devs use it. That is what I call a horrible bug. |
it worked thanks :) |
Just encountered this issue myself. |
I made 2 scripts so I can invoke them whenever I "login" to my development session. On windows: # C:/Android/make_adb_server.ps1
taskkill /f /im adb.exe
Start-Process -FilePath adb.exe -ArgumentList "-a -P 5037 nodaemon server" -WindowStyle hidden
timeout 5
adb connect localhost:58526 On WSL2: # ~/start_adb_server.sh
powershell.exe "C:/Android/make_adb_server.ps1"
windows_ip_address=$(grep nameserver /etc/resolv.conf | awk '{print $2}')
export ADB_SERVER_SOCKET="tcp:${windows_ip_address}:5037"
# Verify it works:
adb devices Then you can setup your adb server directly from WSL2 by calling source ~/start_adb_server.sh |
Apparently Microsoft must've listened to you. I just tried |
Hi folks, we have put out a new update that aims to fix this issue in WSL. In your More info on this release and the changes can be found here in the blog post. Please note: You need to be on a Windows Insiders version to use the new networking settings (Any channel of Windows Insiders will do, including release preview). If you see the "These are not supported" messages it means that your current Windows version doesn't have support, and you will need to upgrade. These features will eventually be coming to Windows 11 22H2. |
These new networking features are now available on the latest version of Win11 22H2! Please make sure you're on the latest build to get these features, you can do that by clicking "Check for Updates" in Windows settings. You can check you have the right build by either ensuring you have KB5031354 installed, or run |
Are there any chances for the fixes to be backported to Win10? 🙏 |
FYI, latest update, |
I installed the version 2.0.14 on my Windows 10. Unfortunatelly the network fix does not work there. So hoping here that it will be available for Windows 10 as well, and soon (Company is not ready yet to update to W11 I'm afraid. Everyone is switching to Macs because it's easier to work when you need linux stuff.) |
The |
@craigloewen-msft @amahpour what is the minimal is this config enough? .wslconfig
do i have to restart my pc too? or is |
@deadcoder0904 Oddly enough I've got nothing configured except for Also, I presume your nginx test is Docker via the Windows command shell (not within a bash WSL 2 shell)? |
Kernel version: 5.15.133.1-1
WSLg version: 1.0.59
MSRDC version: 1.2.4677
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.22631.3155
i'm using powershell 7. but i'm going into |
Yeah, you're certainly up to date. So with older versions of WSL 2 (or maybe it was WSL 1) you had to create tunnels or address the IP of the WSL instance (just like you would with a VM). Not sure when the switch happened but Microsoft now defaults WSL to NAT mode. This means that if you've got a a Node app running in WSL (i.e. the example you gave), addressing it via Some follow up questions:
|
Ubuntu.
I don't remember. But I think I did
Haven't tried it yet.
I am not sure. I did check & added or removed something in Firewall but don't remember what I did. Is there a way to reset it?
For some reason, my IP never worked. The one I got when I did Bdw, I changed my What's your suggestion? Do I try removing |
What version of Ubuntu? One sure bet way of doing it is install a new distro via the Microsoft Store (you can have many WSL distros at the same time just like your would VMs) and make sure there's no firewall set up. Make sure your |
Regarding |
I tried No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy Alright, I'll try it again. Did you remove I'm just unsure because I've only recently installed WSL because I wanted to try Docker & Docker doesn't work without WSL but yeah, still some weird errors. |
I definitely do not have Regarding Docker: that's the beauty of Docker for Desktop. WSL 1 with Docker for Desktop was pretty lackluster but Microsoft did a phenomenal job integrating it with WSL 2. You just need to make sure you enable WSL 2 in the General settings and your distro (in this case Ubuntu 22.04) in Resources > WSL Integration. FYI, Docker for Desktop + WSL 2 is absolutely the way to go. If you're looking for alternatives (e.g. Docker CE within Linux or Rancher) it's not going to be as seamless. |
@amahpour so i removed everything in the only thing i had to change is use for some reason, wsl2 without docker for desktop works much better for me. docker for desktop spins a lot of fans for no reason. it doesn't help i'm using a low-end pc for now. but for low-specs pc, wsl2 without docker desktop is the way. thanks for the help bdw. would love to know why you can see countless examples here (i'm assuming they are on linux/mac but their windows colleagues will face issues) -> https://sourcegraph.com/search?q=ENV+HOSTNAME+localhost+file%3Adockerfile&patternType=standard&sm=0 |
Oh yes, I have the same issue, but after trying so many configurations from the issue or other issue. still not work. from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8010) This is a simple example. Then I run on window, normally, we will connect by
Vice Verse If you start your service on WSL, you want to connect from windows, you will need the ip of your wsl
and your could check from PowerShell, well
It's working for me here, I hope this will help you too. |
Is your feature request related to a problem? Please describe.
although now windows can access wsl service with localhost:port via "localhostForwarding", but wsl cannot access windows service via localhost:port, please fix it, Thank you.
Describe the solution you'd like
When WSL 2 try to connect to localhost:port and be refused, try to connect to windows again.
The text was updated successfully, but these errors were encountered: