Skip to content

A PowerShell script that allows you to bridge ports between your WSL 2 environment and Windows host.

License

Notifications You must be signed in to change notification settings

rogvc/wslbridge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

WSL Bridge

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.

What this is for

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

How does it work?

The tool perform the following steps:

  1. Get Ip Address of WSL 2 machine
  2. Remove previous port forwarding rules
  3. Add port Forwarding rules
  4. Remove previously added firewall rules
  5. 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.

How to set it up:

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.

  1. Install a WSL 2 distro and make sure it's ready to go.

  2. 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.
  3. 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.
  4. Add the script to your PATH for easy access.

    • This is optional, but highly recommended.
  5. Now you should be ready to call wslbridge in any PowerShell terminal window!

Limitations

  • Although the script attempts to port all UDP and TCP connections between host and WSL, I have only tested it with TCP
  • 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.

About

A PowerShell script that allows you to bridge ports between your WSL 2 environment and Windows host.

Resources

License

Stars

Watchers

Forks

Packages

No packages published