How to expose wsl port to other interfaces
While working on a project using WSL, I noticed that I was not able to access my development server from other devices connected within the same local network. This is because WSL creates a virtual LAN that is not exposed to other network interfaces. To make my development server accessible from other devices on the network, I had to expose the WSL port to other interfaces. Here is how I did it.
Command
To expose a WSL port to other interfaces, use the following netsh command in Windows PowerShell or Command Prompt:
netsh interface portproxy set v4tov4 listenport=8888 listenaddress=0.0.0.0 connectport=8888 connectaddress=$(wsl hostname -I)
- listenport=8888: The port on the Windows host you want to listen to.
- listenaddress=0.0.0.0: Listens on all interfaces.
- connectport=8888: The port inside WSL you want to expose.
- connectaddress=$(wsl hostname -I): The IP address of the WSL instance.
reference - https://stackoverflow.com/questions/64513964/wsl-2-which-ports-are-automatically-forwarded 🔗
Adding Port to Firewall (Windows)
Merely exposing the port didn’t work for me, I also had to add the port to the firewall.
I Followed these steps to add the port to the Windows Firewall:
- Click Start, then navigate to Control Panel.
- Select Windows Firewall, then choose Advanced Settings.
- On the left pane, click Inbound Rules, then click New Rule on the right pane.
- Choose Port as the rule type, then click Next.
- On the Protocol and Ports page, select either TCP or UDP, and specify Specific local ports or All local ports.
- Enter the port number you want to open, then click Next.
- On the Action page, select Allow the connection, and click Next.
- On the Profile page, choose the appropriate options for your environment, and click Next.
- Finally, name the rule and click Finish.