- Download the above script
- Place it somewhere in your drive
- The run it
Setup-WSL-Port-Forwarding.ps1
NOTE: Tested on a machine with a single distro. May need to change the wsl hostname -I
command to grab your desired distro's IP
<# | |
.Description | |
Setup-WSL-Port-Forwarding function forwards commonly used ports from host to WSL. | |
The forwarded ports include standard HTTP, HTTPS, Postgres, Redis and other development ports. | |
#> | |
$PortsToForward = @(80, 443, 3000, 3001, 5000, 8080, 8081, 8888, 9090, 9091, 9190, 9191, 5432, 6379) | |
$DestinationIP4Address = (wsl hostname -I) -join "`n" | |
function ForwardPort([int]$port, [string]$ip) { | |
Write-Output ("Forwarding 0.0.0.0:$port to " + ($ip.Trim()) + ":" + $port) | |
netsh.exe interface portproxy add v4tov4 listenport=$port connectport=$port connectaddress=($ip.Trim()) | |
} | |
Write-Host "Setting up port forwarding for from host -> WSL2 network interface $DestinationIP4Address" | |
foreach ($port in $PortsToForward) { | |
ForwardPort $port $DestinationIP4Address | |
} |