Allow Server Running Inside WSL To Be Accessible Outside Windows 10 Host

11

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

2017 07 19 1526 - Allow Server Running Inside WSL To Be Accessible Outside Windows 10 Host

Under Inbound Rules add a New Rule

2017 07 19 1531 - Allow Server Running Inside WSL To Be Accessible Outside Windows 10 Host

Set rule type as Port.

2017 07 19 1532 600x488 - Allow Server Running Inside WSL To Be Accessible Outside Windows 10 Host

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

2017 07 19 1532 001 600x488 - Allow Server Running Inside WSL To Be Accessible Outside Windows 10 Host

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

2017 07 19 1532 002 600x488 - Allow Server Running Inside WSL To Be Accessible Outside Windows 10 Host

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

2017 07 19 1532 003 600x488 - Allow Server Running Inside WSL To Be Accessible Outside Windows 10 Host

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

2017 07 19 1533 600x488 - Allow Server Running Inside WSL To Be Accessible Outside Windows 10 Host

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

2017 07 19 1533 001 600x294 - Allow Server Running Inside WSL To Be Accessible Outside Windows 10 Host

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.

2017 07 19 1534 2 - Allow Server Running Inside WSL To Be Accessible Outside Windows 10 Host

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

11 COMMENTS

  1. 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.

  2. 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 ?

  3. 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

  4. 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.

  5. 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 ‘”

LEAVE A REPLY

Please enter your comment!
Please enter your name here