Last active
March 21, 2023 18:07
-
-
Save blinkinglight/ef988a00fae958dceb7c0f384311a1fb to your computer and use it in GitHub Desktop.
This file contains 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
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine |
This file contains 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
$WslInstanceName = 'Ubuntu-20.04' | |
$WindowsHostIPAddress = '192.168.255.1' | |
$UbuntuInstanceIPAddress = '192.168.255.2' | |
$SubnetMaskNumberOfBits = 24 | |
$WslFirewallRuleName = 'WSL' | |
$WslNetworkInterfaceName = 'vEthernet (WSL)' | |
$UbuntuNetworkInterfaceName = 'eth0' | |
# Ensure the "vEthernet (WSL)" network adapter has been created by starting WSL. | |
Write-Host 'Ensure WSL network exists...' | |
wsl --distribution "$WslInstanceName" /bin/false | |
Write-Host 'WSL network exists' | |
# All inbound traffic from Ubuntu through Windows firewall and assign a static IP address to the "vEthernet (WSL)" | |
# network adapter in Windows. | |
Write-Host 'Configuring Windows host network...' | |
Start-Process 'powershell.exe' -Verb RunAs -Wait -ArgumentList '-ExecutionPolicy Bypass', @" | |
-Command & { | |
Write-Host 'Checking firewall...' | |
If (-Not (Get-NetFirewallRule -Name '$WslFirewallRuleName' -ErrorAction SilentlyContinue)) { | |
Write-Host 'Configuring firewall...' | |
New-NetFirewallRule -Name '$WslFirewallRuleName' -DisplayName '$WslFirewallRuleName' -InterfaceAlias '$WslNetworkInterfaceName' -Direction Inbound -Action Allow | |
Write-Host 'Finished configuring firewall' | |
} | |
Else { | |
Write-Host 'Already configured firewall' | |
} | |
Write-Host 'Checking network interface...' | |
If (-Not (Get-NetIPAddress -InterfaceAlias '$WslNetworkInterfaceName' -IPAddress '$WindowsHostIPAddress' -PrefixLength $SubnetMaskNumberOfBits -ErrorAction SilentlyContinue)) { | |
Write-Host 'Configuring network interface...' | |
New-NetIPAddress -InterfaceAlias '$WslNetworkInterfaceName' -IPAddress '$WindowsHostIPAddress' -PrefixLength $SubnetMaskNumberOfBits | |
Write-Host 'Finished configuring network interface' | |
} | |
Else { | |
Write-Host 'Already configured network interface' | |
} | |
} | |
"@ | |
Write-Host 'Finished configuring Windows host network' | |
# Assign a static IP address to the "eth0" network interface in Ubuntu. | |
Write-Host 'Configuring Ubuntu instance network...' | |
wsl --distribution "$WslInstanceName" --user root /bin/sh -c "ifconfig eth0 0" | |
wsl --distribution "$WslInstanceName" --user root /bin/sh -c "ip ro del default" | |
wsl --distribution "$WslInstanceName" --user root /bin/sh -c "if !(ip address show dev $UbuntuNetworkInterfaceName | grep -q $UbuntuInstanceIPAddress/$SubnetMaskNumberOfBits); then ip address add $UbuntuInstanceIPAddress/24 brd + dev $UbuntuNetworkInterfaceName; fi" | |
wsl --distribution "$WslInstanceName" --user root /bin/sh -c "ip ro add default via $WindowsHostIPAddress dev eth0" | |
wsl --distribution "$WslInstanceName" --user root /bin/sh -c "echo nameserver $WindowsHostIPAddress > /etc/resolv.conf" | |
Write-Host 'Finished configuring Ubuntu instance network' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment