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