A PowerShell script that allows you to bridge ports between your WSL 2 virtual environment and the Windows host, by changing port forwarding and firewall rules on the host machine.
If you ever found yourself developing something in WSL (say, a website using Node.js and Vue.js) and wanted to connect external devices to the address and port being used by the application server, you may have noticed that external devices get a CONNECTION REFUSED
error when trying to reach the running service. Or you might have tried using localhost
on the host machine which got you to right place, but inputting your machine's IP address (which should be what localhost
is an alias of) gives you a similar error as described above.
This error happens (as far as I'm aware) because even though Windows binds localhost
ports between the virtual linux environment and itself automatically, it doesn't seem to do that for its own static IP. What happens, then, is that if you have a certain WSL process running on a certain port and use an external device to ask Windows to be connected with that port, Windows does not look at WSL ports and subsequently refuses the connection request, because it decides that there are no processes bound to that port.
This tool will allow you to specify which port(s) you want shared (forwarded, if you will) between the host and WSL, and then it will set new firewall and port forwarding rules to tell Windows to look for WSL processes running in the specified port(s).
The tool perform the following steps:
- Get Ip Address of WSL 2 machine
- Remove previous port forwarding rules
- Add port Forwarding rules
- Remove previously added firewall rules
- Add new Firewall Rules
Some of the steps require an elevated (with administrator rights) terminal window to work. If you're uncomfortable with the idea of running this file (and with the multiple warnings that Windows gives you about running script files downloaded from the web, it wouldn't surprise me if you are), feel free to look through the file and learn exactly what the script is doing. That being said, the script will automatically prompt the user to open an elevated PowerShell window if the current one isn't elevated.
Before we start, note that you need to be using WSL2 for this script to work. I am unsure whether this will work on WSL 1 or if WSL 1 even needs this at all since I believe it shares its same network adapter with the host machine. You can learn more about the differences between WSL 1 & 2 here.
Also, I recommend using the cross-platform PowerShell 7.
-
Install a WSL 2 distro and make sure it's ready to go.
-
Make sure you have the
net-tools
package installed in your default WSL.- Try running
sudo apt install net-tools
if you don't have it installed already.
- Try running
-
Download the latest wslbridge.ps1 file somewhere in your computer.
- I recommend saving it in
C:\Users\{YOUR_USER}\.wslconfig
or somewhere you usually have your development tools installed. - You might need to edit the file for Windows to allow you to run it, since it's not digitally signed. If you run into this issue, simply open the script in your code editor of choice and hit save to make the file yours.
- I recommend saving it in
-
Add the script to your PATH for easy access.
- This is optional, but highly recommended.
-
Now you should be ready to call
wslbridge
in any PowerShell terminal window!
- Although the script attempts to port all
UDP
andTCP
connections between host and WSL, I have only tested it withTCP
- Changes are not persistent, so you will need to run this script every time your computer boots, or as needed by your applications.
- The script cannot revert the changes it makes. If I have time, I plan on implementing that.