Skip to content

Instantly share code, notes, and snippets.

@SomajitDey
Last active April 18, 2024 14:30
Show Gist options
  • Save SomajitDey/68e8cd639e3bf592bded035630cf86ec to your computer and use it in GitHub Desktop.
Save SomajitDey/68e8cd639e3bf592bded035630cf86ec to your computer and use it in GitHub Desktop.
How to forward WSL2 port to Windows port and vice versa

Ref:

WSL2 to Windows

  1. Obtain the IP address of your host machine (Windows) by running this command from your Linux distribution:
cat /etc/resolv.conf
  1. Copy the IP address following the term: nameserver.
  2. Add rule to Firewall so that incoming connections to the target Windows port is allowed.
New-NetFirewallRule -DisplayName "<Alias>" -InterfaceAlias "vEthernet (WSL)" -Direction Inbound -Protocol TCP -LocalPort <Win_port> -Action Allow
  1. Connect to any Windows server using the copied IP address

Alternatively Windows server can be accessed with the host/domain: "$(hostname).local"

Alternatively IP of the Windows server may be accessed from

ip route show | grep -i default | awk '{ print $3}'

Port forward from Windows to WSL2

Powershell command:

netsh interface portproxy add v4tov4 listenport=<Win_port> listenaddress=0.0.0.0 connectport=<WSL2_port> connectaddress=<WSL2_IP>

Obtain <WSL2_IP> from

wsl hostname -I

To see existing port-forwardings:

netsh interface portproxy show all

To delete a particular port-forwarding:

netsh interface portproxy delete v4tov4 listenport=<port> listenaddress=<IP>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment