Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WSL 2 TCP NETWORK FORWARDING
# WSL2 network port forwarding script v1
# for enable script, 'Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser' in Powershell,
# for delete exist rules and ports use 'delete' as parameter, for show ports use 'list' as parameter.
# written by Daehyuk Ahn, Aug-1-2020
# Display all portproxy information
If ($Args[0] -eq "list") {
netsh interface portproxy show v4tov4;
exit;
}
# If elevation needed, start new process
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
# Relaunch as an elevated process:
Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path),"$Args runas" -Verb RunAs
exit
}
# You should modify '$Ports' for your applications
$Ports = (22,80,443,8080)
# Check WSL ip address
wsl hostname -I | Set-Variable -Name "WSL"
$found = $WSL -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
if (-not $found) {
echo "WSL2 cannot be found. Terminate script.";
exit;
}
# Remove and Create NetFireWallRule
Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock';
if ($Args[0] -ne "delete") {
New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $Ports -Action Allow -Protocol TCP;
New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $Ports -Action Allow -Protocol TCP;
}
# Add each port into portproxy
$Addr = "0.0.0.0"
Foreach ($Port in $Ports) {
iex "netsh interface portproxy delete v4tov4 listenaddress=$Addr listenport=$Port | Out-Null";
if ($Args[0] -ne "delete") {
iex "netsh interface portproxy add v4tov4 listenaddress=$Addr listenport=$Port connectaddress=$WSL connectport=$Port | Out-Null";
}
}
# Display all portproxy information
netsh interface portproxy show v4tov4;
# Give user to chance to see above list when relaunched start
If ($Args[0] -eq "runas" -Or $Args[1] -eq "runas") {
Write-Host -NoNewLine 'Press any key to close! ';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
}
@qishen
Copy link

qishen commented Jun 23, 2021

Is there any way I can use this script at Windows startup without having to manually confirm and switch to the Administrator Powershell every time Windows starts?

@daehahn
Copy link
Author

daehahn commented Jun 23, 2021

Is there any way I can use this script at Windows startup without having to manually confirm and switch to the Administrator Powershell every time Windows starts?

That is only possible for the 'services', not for the apps or scripts as I know. You'd better use another static IP methods which peoples are discussing on the thread.

@woensug-choi
Copy link

woensug-choi commented Feb 16, 2022

It suddenly does not work. It's not catching any of the wsl's IP. I believe it is my problem. Any ideas?

# result of wsl hostname -I
172.19.249.200 172.18.0.1 172.17.0.1
# result of this script
Listen on ipv4:             Connect to ipv4:

Address         Port        Address         Port
--------------- ----------  --------------- ----------
0.0.0.0         10000       192.168.42.226  10000
0.0.0.0         3000        192.168.42.226  3000
0.0.0.0         5000        192.168.42.226  5000

Solved (not knowing what exactly why):

# shutdown wsl first at cmd
wsl --shutdown
# run this script
./wsl2-network.ps1
# run wsl and restart ssh inside
sudo service ssh restart

@demfabris
Copy link

demfabris commented Mar 23, 2022

Works but only for a minute. The connection suddenly closes after a short while

@dezza
Copy link

dezza commented May 17, 2022

I have separate normal and administrator user on Windows, so I can't run wsl as normal user and firewall/portproxy rules as administrator. I haven't found a way where this is possible.

@composite
Copy link

composite commented Jun 7, 2022

@woensug-choi Just add $WSL = $WSL.split(" ")[0] after $WSL variable definition.
@demfabris You must run any long-running live WSL process like service. sudo service ssh start for example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment