Skip to content

Instantly share code, notes, and snippets.

@wllmsash
Last active April 18, 2024 23:19
Show Gist options
  • Save wllmsash/1636b86eed45e4024fb9b7ecd25378ce to your computer and use it in GitHub Desktop.
Save wllmsash/1636b86eed45e4024fb9b7ecd25378ce to your computer and use it in GitHub Desktop.
Assigning Static IP Addresses in WSL2

Assigning Static IP Addresses in WSL2

WSL2 uses Hyper-V for networking. The WSL2 network settings are ephemeral and configured on demand when any WSL2 instance is first started in a Windows session. The configuration is reset on each Windows restart and the IP addresses change each time. The Windows host creates a hidden switch named "WSL" and a network adapter named "WSL" (appears as "vEthernet (WSL)" in the "Network Connections" panel). The Ubuntu instance creates a corresponding network interface named "eth0".

Assigning static IP addresses to the network interfaces on the Windows host or the WSL2 Ubuntu instance enables support for the following scenarios:

  • Connect to an Ubuntu instance from the Windows host using a static IP address
  • Connect to the Windows host from an Ubuntu instance using a static IP address

This guide assumes PowerShell 7 and:

Variable Value
WSL distribution Ubuntu 20.04
WSL instance name Ubuntu-20.04
Windows host IP address 192.168.2.1
Ubuntu instance IP address 192.168.2.2
Network subnet (subnet mask) 192.168.2.0/24 (255.255.255.0)

Note It's best to pick a subnet in the private address range.

Manual Steps

Configure connectivity from the Windows host to the Ubuntu instance:

  1. Assign the Ubuntu instance IP address to the "eth0" network interface in Ubuntu (after every restart).

    sudo ip address add 192.168.2.2/24 brd + dev eth0

Configure connectivity from the Ubuntu instance to the Windows host:

  1. Add a Windows firewall allow rule (once only).

    The "vEthernet (WSL)" network interface uses the "Public" Windows network profile so all traffic from the Ubuntu instance to the host is blocked by default. Allow all inbound traffic from the "vEthernet (WSL)" network interface.

    # Requires "Run as Administrator"
    New-NetFirewallRule -Name 'WSL' -DisplayName 'WSL' -InterfaceAlias 'vEthernet (WSL)' -Direction Inbound -Action Allow

    Note Any existing rules blocking inbound traffic for applications on the Windows host will take precedence over this rule, so remove or disable these where required. Such rules can be created automatically by Windows when an application is first run. Windows shows the user a UAC modal asking for permission to create a firewall rule.

  2. Assign the Windows host IP address to the "WSL" network interface in Windows (after every restart).

    # Requires "Run as Administrator"
    New-NetIPAddress -InterfaceAlias 'vEthernet (WSL)' -IPAddress '192.168.2.1' -PrefixLength 24

PowerShell Script

All the steps above in a PowerShell script.

$WslInstanceName = 'Ubuntu-20.04'
$WindowsHostIPAddress = '192.168.2.1'
$UbuntuInstanceIPAddress = '192.168.2.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 'pwsh' -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 "if !(ip address show dev $UbuntuNetworkInterfaceName | grep -q $UbuntuInstanceIPAddress/$SubnetMaskNumberOfBits); then ip address add $UbuntuInstanceIPAddress/24 brd + dev $UbuntuNetworkInterfaceName; fi"
Write-Host 'Finished configuring Ubuntu instance network'
@electropolis
Copy link

electropolis commented Jan 4, 2024

It doesn't work when the Interface receives 169.254.*.* each time and can't set the IP
But please explain what is the purpose of setting an IP on WSLHost instead of the default one? Each time this is the same NAT but using different IP Class (default is 172.x.x.x). So in my opinion it's pointless. Instead of doing this the best scenario would be bridge vSwitch with Ethernet/Wifi adapter. WSL 1 works fine but has lack of some functions.

@micheldiemer
Copy link

Anyone coming here please find below a detailed working procedure on how to do this with WSLAttachSwitch

Script : microsoft/WSL#4799 (comment)

Text : microsoft/WSL#4799 (comment)

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