How to access localhost of linux subsystem from windows
The answer to this is surprisingly simple which is why searching isn't giving you the right results.
All that WSL does is provide a translation layer between the Linux apps and the windows kernel, very much like how Wine works on Linux. Because of this some core parts of the Ubuntu system are just not there, networking being one of them. WSL translates the Linux system calls into windows ones so the Ubuntu network data flows through the exact same TCP/IP stack as the windows data.
In short this means to access the Linux localhost you just access the windows one, they are the same. localhost:4567
or 127.0.0.1:4567
will do what you want.
As an aside I've been using rails on WSL, it seems to work perfectly except the swing and listen gems don't play well, I had to disable them.
I needed to access my WSL-hosted port from another device in my LAN
(e.g. for mobile development),
and I found @Owen Tourlamain's answer of didn't help for that.
I needed to set up portproxy
, and I found that using connectaddress=localhost
or connectaddress=127.0.0.1
did not work for me (WSL2, Windows 10 20H2).
So I still needed the answer the the OP's question, and it turns out it's surprisingly simple:
- Startup WSL
-
Output like:wsl hostname -I
172.10.200.10
Note:
You have to trim whitespace if you're using it inline in a powershell command, e.g.:
netsh interface portproxy add v4tov4 listenport=19000 listenaddress=0.0.0.0 `
connectport=19000 connectaddress=$($(wsl hostname -I).Trim());