Created
June 17, 2022 04:49
-
-
Save hongymagic/252d226b4d2ed7755a16c5af085cf512 to your computer and use it in GitHub Desktop.
Windows 11 Forward ports to WSL 2 instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.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. | |
Run: | |
Setup-WSL-Port-Forwarding.ps1 | |
#> | |
$PortsToForward = @(80, 443, 1025, 3000, 3001, 4444, 5000, 5900, 8025, 8080, 8081, 8888, 8900, 9090, 9091, 9190, 9191, 5432, 6379) | |
$DestinationIP4Address = (wsl hostname -I).Trim() | |
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment