wsl2 如何通过 LAN 访问
wsl 如果默认不更改配置,仅仅通过 NAT,windows 官方提供的,也没有效果
经过多方查找,终于找到想要的配置,如下
ubuntu.exe
可以替换为支持的发行版
现在除了发起定时任务,也可以直接用管理员权限运行
wsl 如果默认不更改配置,仅仅通过 NAT,windows 官方提供的,也没有效果
经过多方查找,终于找到想要的配置,如下
ubuntu.exe
可以替换为支持的发行版
现在除了发起定时任务,也可以直接用管理员权限运行
netsh interface portproxy add v4tov4 listenport=<yourPortToForward> listenaddress=0.0.0.0 connectport=<yourPortToConnectToInWSL> connectaddress=(wsl hostname -I)
$ports = @(80, 443, 10000, 3000, 5000, 22);
$wslAddress = ubuntu.exe -c "ifconfig eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'";
if ($wslAddress -match '^(\d{1,3}\.){3}\d{1,3}$') {
Write-Host "WSL IP address: $wslAddress" -ForegroundColor Green;
Write-Host "Ports: $ports" -ForegroundColor Green;
}
else {
Write-Host "Error: Could not find WSL IP address." -ForegroundColor Red;
exit;
}
$listenAddress = '0.0.0.0';
foreach ($port in $ports) {
netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$listenAddress;
netsh interface portproxy add v4tov4 listenport=$port listenaddress=$listenAddress connectport=$port connectaddress=$wslAddress;
}
$fireWallDisplayName = 'WSL Port Forwarding';
Remove-NetFireWallRule -DisplayName $fireWallDisplayName;
New-NetFireWallRule -DisplayName $fireWallDisplayName -Direction Outbound -LocalPort $ports -Action Allow -Protocol TCP;
New-NetFireWallRule -DisplayName $fireWallDisplayName -Direction Inbound -LocalPort $ports -Action Allow -Protocol TCP;
powershell.exe -File "C:\Users\hqwuz\Bridge-WslPorts.ps1"