Windows 10’s Windows Subsystem for Linux (WSL) is great. I’m using it as my main go-to development environment for some time now. With the most recent major Creators Update, Microsoft is really trying hard to win some web developers back that left its Windows eco-system decades ago. As Microsoft stated, one of the major benefits for WSL is targeted towards the ever-growing web developer community. WSL offers native build tool-chains without ugly hack workarounds for Windows is a huge plus. Most modern stack like Ruby on Rails and Node.js works fairly well under WSL (Creators Update at least).
If you have already using Bash on Ubuntu on Windows (aka WSL) to do web development you will notice that any server instance running inside the Bash by default can only be accessed via localhost:portnumber
. It would be OK if all you need is accessing the web app via the host-only method, meaning go to the address bar of the browser and enter http://localhost:8080. If you need to access WSL server instance outside the host Windows 10, you would found out that it’s not accessible. This is not due to a lack of features or support from Windows Subsystem for Linux, but rather Windows 10, the host doesn’t know which port to open for external access. Unlike some application that requests for firewall permissions when running the app the first time, WSL doesn’t prompt the host to allow for a specific port to open when you start a node or rails server inside the Ubuntu on Windows. Perhaps, in the future release, Microsoft would allow WSL to send a signal to the host when a specific port starts to listen inside WSL. Nonetheless, here is how to allow your server to be accessible externally outside Windows 10’s host machine.
Go to Windows Firewall with Advanced Security

Under Inbound Rules add a New Rule

Set rule type as Port.

Enter a Specific local port your server is running under WSL, in this case, I have port 3000 running my node server.

Under the Action tab, make sure to “Allow the connection”

If you aren’t sure which profile, select all to apply this rule.

Give this rule a descriptive name so it will be easily identifiable under the Firewall settings.

Once the rule is created, you will be able to find it in the table below.

Now try to access the server hosted inside WSL outside the host Windows 10 machine via hostname or IP address. You should be able to load the web page without any problem.

This is how to access a server running inside WSL outside the host Windows 10 machine. Of course, when your development server port changes you need to either update the rule or create a new rule to accommodate the new port.
June 7, 2020 Update:
If you are running with WSL 2, you can find the hyper-v adapter in the network adapters, and all you need is to configure a port forwarding to forward the port like below.
netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=3000 connectaddress=172.18.28.x
For additional info see https://github.com/microsoft/WSL/issues/4150
I found that I didn’t need to go through this process with the Fall Creators Update installed. I started up a simple web server in WSL on my laptop, made sure my laptop’s WiFi connection to my home network was configured as a private network, and then accessed the page from another device within my home network.
interesting, good to know!
thank you were the solution
I connected to postgresql which is running in wsl via pgadmin 4 with the above steps. The DB gets connected but I can’t able to perform insert or select queries. Is there any solution or a way to make it work query able in windows ?
this sounds like a WSL issue rather than accessibility. What version of Windows 10 are you running? Try the latest insider and check the WSL support for PostgreSQL with WSL.
Until WSL team fixes iptables (or comes up with an insmod location) this is going to continue to be an issue (connection actively refused). Defender FW rules do NOT apply to WSL
This really helped me a lot. Great article and thanks a lot!
You just need to proxy your ports via netsh so they are seen from outside the host:
https://github.com/microsoft/WSL/issues/4150#issuecomment-504051131
Thanks for the heads up Alexei!
If you are using WSL 1, you can just enter the computer’s IP address and the port number. This is what I do with my Mac and it works the same way with my Windows computer.
This didn’t work for me until I changed the ‘ifconfig’ command to ‘ip’:
Error:
/bin/bash: ifconfig: command not found
Fixed line 7:
$remoteport = bash.exe -c “ip a show eth0 | grep ‘inet ‘”