Skip to content

WSL2 Set static ip? #4210

Closed
Closed

Description

@scrossan-crown

This maybe the wrong place to post this but not sure where else to post. Is it currently possible to set a static ip for the WSL2 machine?

I'm loving WSL2 so far with docker and vscode and my only issue is dynamic ip at the moment.

Activity

  1. edwindijas commented on Jun 22, 2019

    @edwindijas

    There is a work around. You can port forward to host ip.
    Check the below link.
    #4150

  2. zippaaa commented on Jun 22, 2019

    @zippaaa

    I agree with the author of the question, please, Wsl2 must have option "static IP". Maybe cmd: wsl --ip <Distro> <IP address>?

  3. scrossan-crown commented on Jun 24, 2019

    @scrossan-crown
    Author

    My issue isn't just with port forwarding and docker. The biggest issue was that I am using VSCode remote development extension and remote developing in WSL and if I need to restart WSL2 to release some resources then I can't just reconnect in the usual way and any unsaved changes get lost which accidentally happened last week.

  4. MiklerGM commented on Jul 3, 2019

    @MiklerGM

    @alekseymvt
    You could execute any command using wsl like this

    wsl -- ifconfig eth0
    
  5. yanyan33333 commented on Jul 13, 2019

    @yanyan33333

    Any solution?
    also see MicrosoftDocs/WSL#418 (comment)

    The Hyper-V Switch IP change everytime when the windows reboot, so the gateway in wsl 2 also need to change

  6. GhostvOne commented on Aug 1, 2019

    @GhostvOne

    Looking for a solution too :)

  7. MiklerGM commented on Aug 1, 2019

    @MiklerGM

    With the latest update, you can access remote ports(WSL2) as local on Windows Host

    You can have a look at this Windows Service - assign a static hostname for WSL machine

  8. zippaaa commented on Aug 1, 2019

    @zippaaa

    With the latest update, you can access remote ports(WSL2) as local on Windows Host

    Can anybody check?:
    Will "http://mysite" work too, if I add "127.0.0.1 mysite" in "windows/hosts"? or localhost only?

  9. MiklerGM commented on Aug 1, 2019

    @MiklerGM

    Yes, it should work

  10. scrossan-crown commented on Aug 1, 2019

    @scrossan-crown
    Author

    It would be nice but instead of WSL 2 ports being bound to 127.0.0.1 they are bound to ::1 and that doesn't work in the browser or hosts file. So we got localhost support but not in a way we can map other domains to it.

  11. moigagoo commented on Aug 1, 2019

    @moigagoo

    Michael Orlov (@MiklerGM)

    Yes, it should work

    It doesn't, unfortunately. To point a custom domain to WSL, I'm adding a line like 172.25.203.130 mysite.com, where 172.25.203.130 is the IP of eth0 interface of the WSL VM: ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | cut -d/ -f1

  12. MiklerGM commented on Aug 1, 2019

    @MiklerGM
  13. moigagoo commented on Aug 1, 2019

    @moigagoo

    Michael Orlov (@MiklerGM) Thanks, I've seen it. I'm OK with my own self-written script.

  14. MiklerGM commented on Aug 1, 2019

    @MiklerGM

    btw, I have no problem accessing the custom domain, for example, test.com from my browser
    My hosts file on Windows host machine

    127.0.0.1 localhost api db test.com
    172.20.124.104 wsl.local
    

    image

  15. 196 remaining items

  16. desarrollonenas2 commented on Oct 12, 2023

    @desarrollonenas2

    uwu

  17. FansinZhao commented on Oct 12, 2023

    @FansinZhao
  18. keith-horton commented on Oct 13, 2023

    @keith-horton
    Member
  19. craigloewen-msft commented on Oct 24, 2023

    @craigloewen-msft
    Member

    These new networking features are now available on the latest version of Win11 22H2!

    Please make sure you're on the latest build to get these features, you can do that by clicking "Check for Updates" in Windows settings. You can check you have the right build by either ensuring you have KB5031354 installed, or run cmd.exe /c ver and ensure that your build number is 22621.2428 or higher (Including the minor build number which is after the . as this was a backport!)

  20. luxzg commented on Oct 25, 2023

    @luxzg

    Don't get me wrong, I applaud this with all my remaining strength!

    (But 4.5 years in the making, I guess 99% of users moved on)

    I still look forward to testing the new functionality, gives me a reason to install WSL2 again, even if just for a quick spin.

    Thank you Craig Loewen (@craigloewen-msft) and rest of the team for making this finally happen!

  21. Lucyfurnice commented on Dec 1, 2023

    @Lucyfurnice

    Assigning Static IP Addresses in WSL2 does not work anymore. It seems the vm-switch has changed from vEthernet (WSL) to WSL (Hyper-V firewall)
    and replacing that in the above script does not work either.

    I can't believe all the trouble i have to go through just to get caddy server and gitea in a docker container so i can program finally.

    This is going on for long enough for me to derail my study and go get a network certification for hyper-v.

    PS C:\Windows\system32> Get-VMSwitch
    
    Name                   SwitchType NetAdapterInterfaceDescription
    ----                   ---------- ------------------------------
    Default Switch         Internal
    WSL (Hyper-V firewall) Internal
    
    
    PS C:\Windows\system32> New-NetFirewallRule -Name 'WSL' -DisplayName 'WSL' -InterfaceAlias 'WSL (Hyper-V firewall)' -Direction Inbound -Action Allow
    New-NetFirewallRule : The specified interface was not found on the system.
    At line:1 char:1
    + New-NetFirewallRule -Name 'WSL' -DisplayName 'WSL' -InterfaceAlias 'W ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (MSFT_NetFirewallRule:root/standardcimv2/MSFT_NetFirewallRule) [New-Net
       FirewallRule], CimException
        + FullyQualifiedErrorId : HRESULT 0x80070057,New-NetFirewallRule
    
    PS C:\Windows\system32> New-NetFirewallRule -Name 'WSL (Hyper-V firewall)' -DisplayName 'WSL (Hyper-V firewall)' -InterfaceAlias 'WSL (Hyper-V firewall)' -Direction Inbound -Action Allow
    New-NetFirewallRule : The specified interface was not found on the system.
    At line:1 char:1
    + New-NetFirewallRule -Name 'WSL (Hyper-V firewall)' -DisplayName 'WSL  ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (MSFT_NetFirewallRule:root/standardcimv2/MSFT_NetFirewallRule) [New-Net
       FirewallRule], CimException
        + FullyQualifiedErrorId : HRESULT 0x80070057,New-NetFirewallRule

    I will have to buy a new computer to do that because my laptop NIC does not support hyper-v correctly...

    PS C:\Windows\system32> Get-NetAdapter
    
    Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed
    ----                      --------------------                    ------- ------       ----------             ---------
    ...
    Wi-Fi                     Killer(R) Wi-Fi 6 AX1650i 160MHz Wir...      12 Up           XX-14-XX-B5-C0-25     144.4 Mbps
    ...

    The shear amount of pain and suffering I have experienced because of WSL is ridiculous.
    I bought windows 10 pro for Hyper-V. didn't work.
    I bought windows 11 pro for Hyper-V again. didn't work.
    I configured WSL 1 and 2. It worked until 2.0.9.0 at which point it started to use Hyper-V networking (from what i understand)

    I have not even moved forwards one step because of all the technical problems.

    I tried cygwin. but i had issues with the software i was using after i compiled all of it and did what i thought would be the best configuration for the stuff i needed.
    So then I tried mingw but that didnt work either for the compilation process.
    and mysys-2 mingw is probably the same, I don't really know. And also did not work.

    I am starting to think that windows is not going to be adequate for the simple programming tasks i need to log and complete.

  22. FansinZhao commented on Dec 1, 2023

    @FansinZhao
  23. AzureZeng commented on Dec 1, 2023

    @AzureZeng

    Assigning Static IP Addresses in WSL2 does not work anymore. It seems the vm-switch has changed from vEthernet (WSL) to WSL (Hyper-V firewall) and replacing that in the above script does not work either.

    I can't believe all the trouble i have to go through just to get caddy server and gitea in a docker container so i can program finally.

    This is going on for long enough for me to derail my study and go get a network certification for hyper-v.

    PS C:\Windows\system32> Get-VMSwitch
    
    Name                   SwitchType NetAdapterInterfaceDescription
    ----                   ---------- ------------------------------
    Default Switch         Internal
    WSL (Hyper-V firewall) Internal
    
    
    PS C:\Windows\system32> New-NetFirewallRule -Name 'WSL' -DisplayName 'WSL' -InterfaceAlias 'WSL (Hyper-V firewall)' -Direction Inbound -Action Allow
    New-NetFirewallRule : The specified interface was not found on the system.
    At line:1 char:1
    + New-NetFirewallRule -Name 'WSL' -DisplayName 'WSL' -InterfaceAlias 'W ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (MSFT_NetFirewallRule:root/standardcimv2/MSFT_NetFirewallRule) [New-Net
       FirewallRule], CimException
        + FullyQualifiedErrorId : HRESULT 0x80070057,New-NetFirewallRule
    
    PS C:\Windows\system32> New-NetFirewallRule -Name 'WSL (Hyper-V firewall)' -DisplayName 'WSL (Hyper-V firewall)' -InterfaceAlias 'WSL (Hyper-V firewall)' -Direction Inbound -Action Allow
    New-NetFirewallRule : The specified interface was not found on the system.
    At line:1 char:1
    + New-NetFirewallRule -Name 'WSL (Hyper-V firewall)' -DisplayName 'WSL  ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (MSFT_NetFirewallRule:root/standardcimv2/MSFT_NetFirewallRule) [New-Net
       FirewallRule], CimException
        + FullyQualifiedErrorId : HRESULT 0x80070057,New-NetFirewallRule

    I will have to buy a new computer to do that because my laptop NIC does not support hyper-v correctly...

    PS C:\Windows\system32> Get-NetAdapter
    
    Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed
    ----                      --------------------                    ------- ------       ----------             ---------
    ...
    Wi-Fi                     Killer(R) Wi-Fi 6 AX1650i 160MHz Wir...      12 Up           XX-14-XX-B5-C0-25     144.4 Mbps
    ...

    The shear amount of pain and suffering I have experienced because of WSL is ridiculous. I bought windows 10 pro for Hyper-V. didn't work. I bought windows 11 pro for Hyper-V again. didn't work. I configured WSL 1 and 2. It worked until 2.0.9.0 at which point it started to use Hyper-V networking (from what i understand)

    I have not even moved forwards one step because of all the technical problems.

    I tried cygwin. but i had issues with the software i was using after i compiled all of it and did what i thought would be the best configuration for the stuff i needed. So then I tried mingw but that didnt work either for the compilation process. and mysys-2 mingw is probably the same, I don't really know. And also did not work.

    I am starting to think that windows is not going to be adequate for the simple programming tasks i need to log and complete.

    #4210 (comment)
    WSL supports static IP already (if updated to the latest version), or you can try networkingMode=mirrored

  24. micheldiemer commented on Apr 19, 2024

    @micheldiemer

    See also WSLAttachSwitch

    Script : #4799 (comment)

    Text : #4799 (comment)

  25. FansinZhao commented on Apr 19, 2024

    @FansinZhao
  26. d-jagoda commented on May 11, 2025

    @d-jagoda

    route2wsl can workaround this limitation https://github.com/d-jagoda/route2wsl by altering windows routing rules.

  27. FansinZhao commented on May 11, 2025

    @FansinZhao
  28. benhillis commented on May 18, 2026

    @benhillis
    Member

    There isn't a way to configure a specific fixed IP in .wslconfig today, but in NAT networking mode WSL already attempts to give your distro the same IP across reboots:

    • The NAT subnet/gateway are persisted to HKLM and the per-user assigned IP to HKCU on shutdown (WslCoreConfig.cpp L211-226).
    • On the next boot they're read back (L401-415) and WSL tries to re-create the NAT endpoint with the saved IP, deleting a conflicting endpoint first if one holds that address (NatNetworking.cpp L201-311).
    • The IP only changes if the saved subnet becomes unusable or another process claimed the address.

    If you need WSL's IP to track the host's exactly, networkingMode=mirrored in .wslconfig does that by design. Docs: https://learn.microsoft.com/windows/wsl/networking#mirrored-mode-networking.

  29. FansinZhao commented on May 18, 2026

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions