Skip to content

Instantly share code, notes, and snippets.

@vilicvane
Last active December 3, 2022 06:25
Show Gist options
  • Save vilicvane/0edcb3bec10339a3b633bc9305faa8b5 to your computer and use it in GitHub Desktop.
Save vilicvane/0edcb3bec10339a3b633bc9305faa8b5 to your computer and use it in GitHub Desktop.
Access Windows host from WSL 2
# Access host ports from WSL 2.
# https://gist.github.com/vilic/0edcb3bec10339a3b633bc9305faa8b5
# Make sure WSL gets initialized.
bash.exe -c exit
# Record host name for /etc/hosts that points to host IP.
$HOST_NAME = "host.wsl";
# Ports listened on host localhost to forward, you don't need to add the port if it listens all addresses.
$HOST_LOCALHOST_PORTS = @(52698);
$FIREWALL_RULE_NAME = "wsl";
$FIREWALL_RULE_DISPLAY_NAME = "WSL";
Write-Output "Detecting WSL IP address...";
$hostIP = wsl -- bash -c "tail -1 /etc/resolv.conf | cut -d' ' -f2";
$wslIP = (wsl -- ip address show eth0 | Select-String -Pattern "inet ([\d.]+)").Matches.Groups[1].Value;
Write-Output "Host IP address: $hostIP";
Write-Output "WSL IP address: $wslIP";
Write-Output "Updating hosts record $HOST_NAME ($hostIP) for WSL...";
wsl --user root -- echo "$hostIP`t$HOST_NAME" ">>" /etc/hosts;
Write-Output "Updating firewall rule...";
Remove-NetFireWallRule -Name $FIREWALL_RULE_NAME -ErrorAction Ignore;
New-NetFireWallRule `
-Name $FIREWALL_RULE_NAME `
-DisplayName $FIREWALL_RULE_DISPLAY_NAME `
-Direction Inbound `
-LocalAddress @($hostIP)`
-Action Allow;
Write-Output "Setting up localhost port proxies...";
foreach ($port in $HOST_LOCALHOST_PORTS) {
$previousRecordGroups = (netsh interface portproxy show v4tov4 | Select-String "(\S+)\s+$port\s+127\.0\.0\.1\s+$port").Matches.Groups;
if ($null -ne $previousRecordGroups) {
$previousHostIP = $previousRecordGroups[1].Value;
netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$previousHostIP | Out-Null;
}
netsh interface portproxy add v4tov4 listenport=$port listenaddress=$hostIP connectport=$port connectaddress=127.0.0.1 | Out-Null;
}
Write-Output "Done.";
@PavelSosin-320
Copy link

I reconfigure my network in the top-down manner i.e. had configured IPV4 DNS list in my router, then configured Windows, shut down and re-start WSL, and in some magic way Resolv.conf got the top DNS in my DNS list in the Router's Resolv.conf. In other words, there are smart home routers, like Technicolor that can manage the Home network as an SDN network.
From the other hand, I afraid that it blocks any access to devices or WSL VMs via ports reserved for Internet using its built-in firewall

@sellonen
Copy link

Thank you, this helped me a great deal!

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