DEV Community

loading...

WSL Port Forwarding

vishnumohanrk profile image Vishnumohan R K Updated on ・2 min read

When you start a server in WSL, both windows and linux share the same local host, however your WSL local IP is shared only with windows and cannot be accessed within your local network. This was possible in WSL1, but not with WSL2.

So, in order to access the server from you local devices, you need to port forward the WSL local IP using netsh.

Create network.ps1 powershell script file with the following content in any directory.

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))

{   
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}

$remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
$found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';

if( $found ){
  $remoteport = $matches[0];
} else{
  echo "The Script Exited, the ip address of WSL 2 cannot be found";
  exit;
}

$ports=@(3000, 3001, 3002, 5500, 19000, 19001);

iex "netsh interface portproxy reset";
for( $i = 0; $i -lt $ports.length; $i++ ){
  $port = $ports[$i];
  iex "netsh interface portproxy add v4tov4 listenport=$port connectport=$port connectaddress=$remoteport";
}
iex "netsh interface portproxy show v4tov4";
Enter fullscreen mode Exit fullscreen mode

The script will ask for admin access.

The script gets the WSL IP address and then executes netsh commands to forward the connection to your Windows' local IP, which you can see by executing ipconfig in either powershell or cmd prompt or at your router's admin page.

The script also resets all previous forwards and at the end displays the list of ipv4 connections forwarded.

You can modify the ports array with your commonly used ports.

Since WSL2 uses virtual network adapters, its IP address resets after every startup.

Notes

  • Run this command (in Powershell, as admin)
netsh interface portproxy reset
Enter fullscreen mode Exit fullscreen mode

to reset all forwards

  • Run this command (in Powershell, as admin)
netsh interface portproxy show v4tov4
Enter fullscreen mode Exit fullscreen mode

to view all ipv4 forwards.

  • Save network.ps1 in Windows file system.

  • Have a desktop shortcut for the script. It could be handy.

  • The network should be a private network.

  • If your primary purpose is to only check your website for responsive design with your mobile, there is a simple way for that in chrome. See Chrome remote debugging

References

Discussion (2)

pic
Editor guide
Collapse
timhass6 profile image
Tim Hass

Great post, thank, this script is very handy until WSL finds a way to do it as built-in or at least provide a way to set "static" ip for the wsl.
after your permission if you will, I just modified your code so it would map host port to different ports on WSL
*don't forget to enable host ports on windows firewall ;)
*install net-tools on wsl , $sudo apt install net-tools

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))

{

$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}

$remoteip =bash.exe -c "ifconfig eth0 | grep 'inet '"
$found = $remoteip -match '\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}';

if( $found ){
$connectaddress = $matches[0];
} else{
echo "The Script Exited, the ip address of WSL 2 cannot be found";
exit;
}

$ports=@(3388,3306);
$connectports=@(3389,3306)

iex "netsh interface portproxy reset";
for( $i = 0; $i -lt $ports.length; $i++ ){
$port = $ports[$i];
$connectport=$connectports[$i];
iex "netsh interface portproxy add v4tov4 listenport=$port connectport=$connectport connectaddress=$connectaddress";
}
iex "netsh interface portproxy show v4tov4";

Collapse
pauloch8 profile image
Paulo Henrique

Hello! Thank you for sharing this. Is there any problem in listening to port 80? I have a Gitlab docker container running on port 80. I ran your script but it is not being reached in my local network