Skip to content

Instantly share code, notes, and snippets.

@hongymagic
Created June 17, 2022 04:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hongymagic/252d226b4d2ed7755a16c5af085cf512 to your computer and use it in GitHub Desktop.
Save hongymagic/252d226b4d2ed7755a16c5af085cf512 to your computer and use it in GitHub Desktop.
Windows 11 Forward ports to WSL 2 instance
<#
.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