Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WSL2 Set static ip? #4210

Open
scrossan-crown opened this issue Jun 21, 2019 · 218 comments
Open

WSL2 Set static ip? #4210

scrossan-crown opened this issue Jun 21, 2019 · 218 comments

Comments

@scrossan-crown
Copy link

scrossan-crown commented Jun 21, 2019

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.

@edwindijas
Copy link

edwindijas commented Jun 22, 2019

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

@zippaaa
Copy link

zippaaa commented Jun 22, 2019

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

@scrossan-crown
Copy link
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.

@MiklerGM
Copy link

MiklerGM commented Jul 3, 2019

@alekseymvt
You could execute any command using wsl like this

wsl -- ifconfig eth0

@yanyan33333
Copy link

yanyan33333 commented Jul 13, 2019

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

@GhostvOne
Copy link

Looking for a solution too :)

@MiklerGM
Copy link

MiklerGM commented Aug 1, 2019

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

@zippaaa
Copy link

zippaaa commented Aug 1, 2019

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?

@MiklerGM
Copy link

MiklerGM commented Aug 1, 2019

Yes, it should work

@scrossan-crown
Copy link
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.

@moigagoo
Copy link

moigagoo commented Aug 1, 2019

@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

@MiklerGM
Copy link

MiklerGM commented Aug 1, 2019

@moigagoo check out this https://github.com/shayne/go-wsl2-host you can do it automatically with this service

@moigagoo
Copy link

moigagoo commented Aug 1, 2019

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

@MiklerGM
Copy link

MiklerGM commented Aug 1, 2019

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

@moigagoo
Copy link

moigagoo commented Aug 1, 2019

@MiklerGM Huh, that's interesting. Tried that several times, it never worked, adn still doesn't 🤔

It works even if you remove the last line, doesn't it? Maybe, you did something else apart from editing hosts file, like installed something like dnsmasq? Also, maybe this Go service does more than just writes to hosts?

@yanyan33333
Copy link

maybe build 18945
https://devblogs.microsoft.com/commandline/whats-new-for-wsl-in-insiders-preview-build-18945/

@scrossan-crown
Copy link
Author

scrossan-crown commented Aug 1, 2019

@yanyan33333 I'm already on build 18945 but still no joy

image

@GhostvOne
Copy link

GhostvOne commented Aug 1, 2019

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?

Yes, it should work

I confirm it doesn't work.
I would add my custom domain with 127.0.0.1 but doesn't work when I try to access with this domain.
localhost work, but not custom hosts

@scrossan-crown
Copy link
Author

scrossan-crown commented Aug 1, 2019

One work around I've found is if you are using docker and you install the latest edge version. It has support for WSL2 and it uses some magic (not sure what it's doing) but now 127.0.0.1 is working for in my hosts file for the apps in my docker containers on WSL2.

@yanyan33333
Copy link

@scrossan-crown it works ,I start nginx on wsl and I can visit the website in windows brower by using localhost

@scrossan-crown
Copy link
Author

@yanyan33333 I can do the same but can't use 127.0.0.1 in my hosts file with a domain name and access my docker container running on port 80 but I can now that I'm using docker desktop edge version

@zippaaa
Copy link

zippaaa commented Aug 2, 2019

@yanyan33333

I start nginx on wsl and I can visit the website in windows brower by using localhost

Check custom domain:
WSL2
/etc/nginx/conf.d/mysite.conf ->

server {
    server_name mysite;
    listen 80;
    ...
}

Windows
hosts -> 127.0.0.1 mysite -> http://mysite

@MiklerGM
Copy link

MiklerGM commented Aug 2, 2019

@alekseymvt it is working for me
image

I do not have dnsmasq, but I've got service in Go (I should've removed it by now), and a started systemd service. /etc/hosts on my WSL and windows systems are in sync, I did not change anything on WSL.

From my knowledge, this should not affect the GET requests. You can even send GET request via telnet. The domain in your browser's address bar only represents the HTTP Header for HOST directive.

Steps to debug

  • Check that nginx is working correctly on WSL (with all your domains and server_names)
  • Check if windows resolve your hosts correctly.
  • Check windows build number >18945.1001
  • Send the GET request to nginx

In case it is not working

  • Check that nothing is listening to 80 port on your host system or change the nginx port

@zippaaa
Copy link

zippaaa commented Aug 2, 2019

@MiklerGM

it is working for me

Thank you, I saw your answer. I answered to @yanyan33333, because he wrote about localhost.
I will check a custom domain later for myself, I need download 3Gb :)

@craigloewen-msft
Copy link
Member

We also had a bug where you needed to bind your Linux applications to 0.0.0.0 instead of 127.0.0.1 to access it, which we've fixed. This is being tracked in issue #4353

@webfaqtory
Copy link

On build 18963 I can access http://localhost fine but in my Windows host file I have defined domains:
127.0.0.1 www.tripal.john
127.0.0.1 www.tripal3.john
127.0.0.1 clean.tripal3.john
These refuse to connect. Even added them to /etc/hosts. No joy

@sth4me
Copy link

sth4me commented Aug 29, 2019

Set 127.0.0.1 domain in Windows hosts file does not work all the time,even only do nothing.
File I/O in /mnt is slow, so I put my project files in ~/, then IDE should use them by sftp. But when sftp connect for a while, all connections by hosts file will fail.
I use script change hosts file ip to wsl ip, and let project files in /mnt, it works well.
Static IP may be better.

@sonvirgo
Copy link

Still no official feature. All workaround makes thing more complicated by adding extra stuff to already buggy Wsl's DNS, and host Windows's VPN.

@FansinZhao
Copy link

FansinZhao commented Mar 12, 2023 via email

@aki-k
Copy link

aki-k commented Mar 12, 2023

@travelr Powershell has get-vmswitch/set-vmswitch/new-vmswitch but they are not installed probably until you enable the Hyper-V Windows feature, which is not available for Windows Home.

PS C:\Windows\system32> get-vmswitch
get-vmswitch : The term 'get-vmswitch' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ get-vmswitch
+ ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (get-vmswitch:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

@AzureZeng
Copy link

AzureZeng commented May 23, 2023

I have found something interesting in the latest version of WSL2.
The WSL Network IP range and gateway IP can be found, and be changed in the following registry key:
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\NatGatewayIpAddress Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\NatNetwork
You can find and change your WSL instance IP in this registry key:
Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss

image
image

The WSL instance IP and host IP relative to WSL instance will not change even you restart your computer, if you leave these registry keys unmodified.

If you want to change WSL IP, just edit these registry keys, then restart your computer, and new WSL IP setting will be applied when you open WSL.
As another word, now it is really OK to set a static IP for WSL.

My OS is Windows 11 Enterprise (Build 22621.1702), and WSL version is 1.2.5.0.
Also, there is one thing to mention, that my VMware NAT/HostOnly Network and WSL instance are now able to access mutually through IP addresses.

@FansinZhao
Copy link

FansinZhao commented May 23, 2023 via email

@timesnewmen
Copy link

I have found something interesting in the latest version of WSL2. The WSL Network IP range and gateway IP can be found, and be changed in the following registry key: Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\NatGatewayIpAddress Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\NatNetwork You can find and change your WSL instance IP in this registry key: Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss

image image

The WSL instance IP and host IP relative to WSL instance will not change even you restart your computer, if you leave these registry keys unmodified.

If you want to change WSL IP, just edit these registry keys, then restart your computer, and new WSL IP setting will be applied when you open WSL. As another word, now it is really OK to set a static IP for WSL.

My OS is Windows 11 Enterprise (Build 22621.1702), and WSL version is 1.2.5.0. Also, there is one thing to mention, that my VMware NAT/HostOnly Network and WSL instance are now able to access mutually through IP addresses.

Thanks. It works on my Win10 too. Build 10.0.19045.

@aki-k
Copy link

aki-k commented May 24, 2023

About 200 comments on a much-wanted feature and no comments from Microsoft? The WSL release page doesn't even have any comment on this https://github.com/microsoft/WSL/releases

Edit: I can also confirm that this works with WSL 1.2.5 (Windows 11 Pro)

@LHozzan
Copy link

LHozzan commented May 24, 2023

About 200 comments on a much-wanted feature and no comments from Microsoft?

Very good question! So, for what you paying to the Microsoft? For ignorating from billion company?
OK, I am sure, that mans from Microsoft not be worried, if we switch to different solution. Microsoft isnt only one company, who making desktop OS, right?

@obriat
Copy link

obriat commented May 25, 2023

@timesnewmen: not working on my PC 10.0.19045 Build 19045.
with the IP 192.168.64.100 (192.168.64.0/24 192.168.64.1)
IP is not set and the registry keys are reset to default values.

@timesnewmen
Copy link

The subnet won't change after reboot on my PC. But the IP address still could change after reboot.

Here is what I do to set the IP address. Save the below script as /etc/wsl.init for example. Edit /etc/wsl.conf and add line:

[boot]
command = /etc/wsl.init

#! /bin/sh
WSLIP="169.254.169.199/24"
WSLBC="169.254.169.255"
WSLGW="169.254.169.1"

echo "IP Address initial script started..." >>/tmp/wsl.init.log
echo `date` >> /tmp/wsl.init.log
# Getting the current IP
ip address show eth0 | grep -oE '(192\.168\.|169\.254\.|172\.[1-9][0-9]\.|10\.).*/2[0-4]\b' >/tmp/oip.txt

for s_ip in `cat /tmp/oip.txt`
do
   echo "IP to remove>>> $s_ip"
   echo "Original IP Address of eth0 is: $s_ip. Removing..." >>/tmp/wsl.init.log
   sudo ip addr del $s_ip dev eth0
done
echo "Setting new IP $WSLIP on eth0..." >>/tmp/wsl.init.log
sudo ip addr add $WSLIP broadcast $WSLBC dev eth0 label eth0
# Setting default gateway
sudo route del -net 0.0.0.0/0 >/dev/null 2>&1
sudo route add -net 0.0.0.0/0 gw $WSLGW >>/tmp/wsl.init.log 2>&1

@aki-k
Copy link

aki-k commented May 26, 2023

@obriat

not working on my PC 10.0.19045 Build 19045

Are you on WSL 1.2.5 (wsl -v) ? AzureZeng said it's working with the latest WSL release, which is 1.2.5 currently.

https://github.com/microsoft/WSL/releases

@obriat
Copy link

obriat commented May 28, 2023

@aki-k
yes, I use pre-release updates:

Version WSL : 1.2.5.0
Version du noyau : 5.15.90.1
Version WSLg : 1.0.51
Version MSRDC : 1.2.3770
Version direct3D : 1.608.2-61064218
Version de DXCore : 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Version de Windows : 10.0.19045.2965

@craigloewen-msft
Copy link
Member

Hi folks, we have put out a new update that aims to address networking issues in WSL. In your .wslconfig file you can set experimental.networkingMode=mirrored, as well as some other key settings that should improve your network compatibility!
This feature should allow you to set a static IP as you will get the IP address of the Windows host. Please try them out and let us know what you think.

More info on this release and the changes can be found here in the blog post.

Please note: You need to be on a Windows Insiders version to use the new networking settings (Any channel of Windows Insiders will do, including release preview). If you see the "These are not supported" messages it means that your current Windows version doesn't have support, and you will need to upgrade. These features will eventually be coming to Windows 11 22H2.

@FansinZhao

This comment was marked as off-topic.

@edwindijas
Copy link

edwindijas commented Sep 19, 2023

@craigloewen-msft, thanks for the update, waiting for 22H2

@zkorhone
Copy link

Hi folks, we have put out a new update that aims to address networking issues in WSL. In your .wslconfig file you can set experimental.networkingMode=mirrored, as well as some other key settings that should improve your network compatibility! This feature should allow you to set a static IP as you will get the IP address of the Windows host. Please try them out and let us know what you think.

More info on this release and the changes can be found here in the blog post.

Please note: You need to be on a Windows Insiders version to use the new networking settings (Any channel of Windows Insiders will do, including release preview). If you see the "These are not supported" messages it means that your current Windows version doesn't have support, and you will need to upgrade. These features will eventually be coming to Windows 11 22H2.

Mirrored mode does seem quite interesting. However I couldn't find any hints, if Windows firewall policies are applied and how they are applied when using mirrored mode.

In many enterprise organizations firewall must be applied at host level. Copying rules to linux packet filter wouldn't be sufficient in such environment. Filtering at host level is required to provide observability to network traffic.

That said, some documentation would be appreciated.

@desarrollonenas2
Copy link

uwu

@FansinZhao
Copy link

FansinZhao commented Oct 12, 2023 via email

@keith-horton
Copy link
Member

Hi there. Please see https://devblogs.microsoft.com/commandline/windows-subsystem-for-linux-september-2023-update/#:~:text=Hyper-V%20Firewall%20allows%20you%20to%20specify%20firewall%20settings,will%20be%20automatically%20applied%20to%20your%20WSL%20distros. for more information about the new features coming like Mirrored Networking, DNS Tunneling, and Hyper-V Firewall

@craigloewen-msft
Copy link
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!)

@luxzg
Copy link

luxzg commented Oct 25, 2023

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 @craigloewen-msft and rest of the team for making this finally happen!

@Lucyfurnice
Copy link

Lucyfurnice commented Dec 1, 2023

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.

@FansinZhao
Copy link

FansinZhao commented Dec 1, 2023 via email

@AzureZeng
Copy link

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

@micheldiemer
Copy link

See also WSLAttachSwitch

Script : #4799 (comment)

Text : #4799 (comment)

@FansinZhao
Copy link

FansinZhao commented Apr 19, 2024 via email

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

No branches or pull requests