microsoft / WSL

Issues found on WSL
https://docs.microsoft.com/windows/wsl
MIT License
17.37k stars 816 forks source link

WSL2 Set static ip? #4210

Open scrossan-crown opened 5 years ago

scrossan-crown commented 5 years ago

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.

abolfazleslami1990 commented 4 years ago

i don't know it is a right solution for everyone, but believe me i could solve this problem in a very weird way which is not compatible for everyone the way i solved this problem made the cost. i bought an extra wireless adapter for my pc and now my pc is making connection from two adapters one is connecting through the LAN connection and the second one is connected from wireless adapter so if i check my device manager, i have two adapter installed in my machine so as @https://github.com/microsoft/WSL/issues/4150#issuecomment-603294405 said we need to change WSL adapter from internal to external but in previous days i couldn't make connection both in my windows and Linux altogether but now i have two adapters which i can assign one adapter to WSL and another one can be used for my windows connection

after assigning your adapter to your WSL machine from Hyper-V manager, you just need to change wsl ip to the ip of your connection just write

sudo ifconfig eth0 netmask 255.255.255.0 up sudo route add default gw 192.168.xxx eth0 then remove resolv.conf file and make wsl.conf file and in wsl file write down [network] generateResolvConf = false

and after that, you should create resolv.conf once again, put your DNS inside this file like example below:

nameserver 8.8.8.8 nameserver 8.8.4.4

don't forget that each time you shutdown your machine or either your Linux, you have to assign ip once again because this method is not permanent (if you know the permanent way, i am happy to hear)

now I can ssh to another machine in local area i know this is not a great solution for everyone, but it's working like a charm for me

please Microsoft think more about this issue

luxzg commented 4 years ago

@illustrayking you could work around the non-permanent issue by making a startup script in WSL that changes the needed settings / files. It is still not a pretty solution... it's literally workaround upon workaround. New add-in card or USB adapter is the least of my worries, my desktop PC already has 2 LAN ports on it's motherboard.. but even though I like tinkering, and could make this work, it's actually MORE work than - simply installing Linux in a Hyper-V VM.

Icehunter commented 4 years ago

So; not sure if this is around just accessing it from localhost; maybe I need to make this comment elsewhere but I did notice that if docker desktop is installed and the WSL2 integration is turned on; that setting items such as these:

127.0.0.1 mysite.com

Do not work until you run wsl --shutdown and open a terminal tab again.

I haven't tested yet if it's docker related or the startup order, but simply restarting the WSL container after logging into my machine when everything else has started fixes issues access from 127.0.0.1, localhost and custom domains.

I have my docker setup in windows to be integrated with WSL2 (latest slow ring) so that I don't need to install it inside the WSL2 image. Previously I had docker in the image and had to start it manually each boot.

Icehunter commented 4 years ago

Okay, my issue was that "Fast Startup" was turned on; so it was not regenerating the host mapping correctly and used the previous IP address on startup.

Opened up power options in control panel > choose what to do when power buttons are pressed > show unavailable options > uncheck fast startup.

Done and fixed for me.

pavlovic11 commented 4 years ago

i don't know it is a right solution for everyone, but believe me i could solve this problem in a very weird way which is not compatible for everyone the way i solved this problem made the cost. i bought an extra wireless adapter for my pc and now my pc is making connection from two adapters one is connecting through the LAN connection and the second one is connected from wireless adapter so if i check my device manager, i have two adapter installed in my machine so as @#4150 (comment) said we need to change WSL adapter from internal to external but in previous days i couldn't make connection both in my windows and Linux altogether but now i have two adapters which i can assign one adapter to WSL and another one can be used for my windows connection

after assigning your adapter to your WSL machine from Hyper-V manager, you just need to change wsl ip to the ip of your connection just write

sudo ifconfig eth0 netmask 255.255.255.0 up sudo route add default gw 192.168.xxx eth0 then remove resolv.conf file and make wsl.conf file and in wsl file write down [network] generateResolvConf = false

and after that, you should create resolv.conf once again, put your DNS inside this file like example below:

nameserver 8.8.8.8 nameserver 8.8.4.4

don't forget that each time you shutdown your machine or either your Linux, you have to assign ip once again because this method is not permanent (if you know the permanent way, i am happy to hear)

now I can ssh to another machine in local area i know this is not a great solution for everyone, but it's working like a charm for me

please Microsoft think more about this issue

You can also just bridge vEthernet (WSL) and your primary network interface. After that process is same as you explained. I tested this on windows host running in wmvare, but it should be same for a non virtual host.

gwharton commented 4 years ago

<PCNAME>.mshome.net always resolves to the IP address of the host machine from both host and wsl side.

Pinging GW-DESKTOP.mshome.net [172.24.224.1] with 32 bytes of data:
Reply from 172.24.224.1: bytes=32 time<1ms TTL=128
PING GW-DESKTOP.mshome.net (172.24.224.1) 56(84) bytes of data.
64 bytes from GW-DESKTOP.mshome.net (172.24.224.1): icmp_seq=1 ttl=128 time=0.268 ms
jpsenior commented 4 years ago

This statement is not correct - pcname.mshome.net resolved to something incorrect. [jp@TITAN ~]$ ip -4 route | grep default default via 172.23.80.1 dev eth0

[jp@TITAN ~]$ ping titan.mshome.net PING TITAN.mshome.net (172.20.240.1): 56 data bytes ^C--- TITAN.mshome.net ping statistics --- 2 packets transmitted, 0 packets received, 100% packet loss

Doesn't seem this dns name is a hard and fast rule. My vswitch here is clearly configured with a different IP than this mshome resolution replie to.

gwharton commented 4 years ago

My apologies. Has worked 100% for me sofar. Not sure whats going on.

gwharton commented 4 years ago

On my ubuntu install

sudo apt-get install -y avahi-daemon
sudo /etc/init.d/dbus start
sudo /etc/init.d/avahi-daemon start

Then <PCNAME>.local resolves to your windows machine and <PCNAME>-2.local resolves to your ubuntu machine

You can also override the published hostname of your linux machine in /etc/avahi/avahi-daemon.conf for example

[server]
host-name=WSL

so WSL.local will now resolve to your wsl machine.

And this will apply to any lookups run on any mDNS enabled machines on your local LAN.

virgilioneto commented 4 years ago

You can access any port on WSL2 using the 127.0.1.1 IP 127.0.0.1 points to host and 127.0.1.1 to WSL.

Works well with ubuntu and docker :)

JacobDB commented 4 years ago

You can access any port on WSL2 using the 127.0.1.1 IP 127.0.0.1 points to host and 127.0.1.1 to WSL.

Works well with ubuntu and docker :)

This doesn't seem to be the case for me. Using the full external IP works fine, but when I change it to 127.0.1.1, it just doesn't work at all. Ubuntu 18.04.4 LTS, WSL 2, Windows 19041.208.

ueinohakono commented 4 years ago

I don't know why Microsoft makes this simple question be sooooooo complete. Why wsl2 have dynamic IP? Because wsl2 uses the stupid 'Default Hyper-V Switch' . This Switch will be deleted and recreated after reboot.

To solve this problem, just give us a way to change wsl2's switch from 'Default Hyper-V Switch' to other custom Hyper-V Switch that has static network segmentcustom .

It will simplely solve this problem.

All the methods above(dynamic hosts, port forward etc...) are OK. But they can't solve the fundamental problem: Because wsl2 has no static IP, any computer on LAN can't connect to wsl2's port or files.

For example, there is a Windows 10 virtual machine on my computer, I want to share wsl2's files to the Windows 10 virturl machine(or other computer on LAN) by samba. Is there a way to do it? No, there is no way to do it. Because wsl2's ip is random. Just give us a static IP can solve this problem.

1 year has passed, Microsoft still does not want to solve this problem Maybe Microsoft thinks if wsl2 has a static IP, a lot of web service will run on wsl2 (nginx, apache etc..) instead of IIS?

bytemain commented 4 years ago

I don't know why Microsoft makes this simple question be sooooooo complete. Why wsl2 have dynamic IP? Because wsl2 uses the stupid 'Default Hyper-V Switch' . This Switch will be deleted and recreated after reboot.

To solve this problem, just give us a way to change wsl2's switch from 'Default Hyper-V Switch' to other custom Hyper-V Switch that has static network segmentcustom .

It will simplely solve this problem.

All the methods above(dynamic hosts, port forward etc...) are OK. But they can't solve the fundamental problem: Because wsl2 has no static IP, any computer on LAN can't connect to wsl2's port or files.

For example, there is a Windows 10 virtual machine on my computer, I want to share wsl2's files to the Windows 10 virturl machine(or other computer on LAN) by samba. Is there a way to do it? No, there is no way to do it. Because wsl2's ip is random. Just give us a static IP can solve this problem.

1 year has passed, Microsoft still does not want to solve this problem Maybe Microsoft thinks if wsl2 has a static IP, a lot of web service will run on wsl2 (nginx, apache etc..) instead of IIS?

@ueinohakono https://github.com/microsoft/WSL/issues/4150#issuecomment-504209723

kangzj commented 4 years ago

You can access any port on WSL2 using the 127.0.1.1 IP 127.0.0.1 points to host and 127.0.1.1 to WSL. Works well with ubuntu and docker :)

This doesn't seem to be the case for me. Using the full external IP works fine, but when I change it to 127.0.1.1, it just doesn't work at all. Ubuntu 18.04.4 LTS, WSL 2, Windows 19041.208.

It doesn't work for me all the time either. It works only for some applications, while the others don't. So frustrating... Build 19577 200228-1439

Celimar commented 4 years ago

wsl -- ifconfig eth0

@alekseymvt You could execute any command using wsl like this

wsl -- ifconfig eth0

Thank´s, it is worked for me

widersky commented 4 years ago

I wrote powershell script to automate WSL IP's changing in Windows hosts file: WSLSync

Currently i'ts in Beta stage but I think it's close to completion. Feel free to test, contribute if anyone want to :)

kppomzh commented 4 years ago

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

4150

Since a normal hyper-v host can set up a virtual switch to customize the network, Microsoft should also provide a tool to set up WSL instead of letting users write scripts themselves.

therealkenc commented 4 years ago

Scribbling your Windows hosts file elevated is not particularly recomended (but free country, natch). The suggestion by gwharton to start up avahi-daemon(8) is sound (thanks Grahm). It also has the amiable property of being both the correct and standard solution to the problem of LAN IPs that come and go (like ours). [n.b. You'll get better eyeballs on configuring avahi in a forum dedicated to that sort of thing rather than in this WSL github issue.]

MichaelBelgium commented 4 years ago

I wrote powershell script to automate WSL IP's changing in Windows hosts file: WSLSync

Currently i'ts in Beta stage but I think it's close to completion. Feel free to test, contribute if anyone want to :)

Cool! I created something the same but more simplier: https://gist.github.com/MichaelBelgium/2243e5713833ba44b5675d844eeb1c85

kangzj commented 4 years ago

I wrote powershell script to automate WSL IP's changing in Windows hosts file: WSLSync Currently i'ts in Beta stage but I think it's close to completion. Feel free to test, contribute if anyone want to :)

Cool! I created something the same but more simplier: https://gist.github.com/MichaelBelgium/2243e5713833ba44b5675d844eeb1c85

Loving it. thanks dude

JurrianFahner commented 4 years ago

Cool! I created something the same but more simplier: https://gist.github.com/MichaelBelgium/2243e5713833ba44b5675d844eeb1c85

It is a nice work around! However it would be better if there would be either a fixed host name or a fixed ip address to ease development on a windows and wsl2 system simultaneously.

arsdever commented 4 years ago

Any update about having a static IP?

goosechaser commented 4 years ago

Any update about having a static IP?

The situation remains dynamic.

kangzj commented 4 years ago

I don't know why I'm still sticking to WSL2 while VSCode could do with ssh remote developing with a real VM... seriously considering switching back to a HyperV VM which has a fix ip and more.

Genda1ph commented 4 years ago

I was looking forward to WSL2, but I held back on updating to 2004 for various reasons until last weekend. I then updated my main environment to WSL2 and it broke my rsync backups, because now I can't easily ssh in from outside. I can't switch the WSL switch to bridged mode, as I already have a bridge for Hyper-V and only one network interface.

Clearly, the solution here is telling WSL, on a per-instance basis, which switch to use and let the VM manage its network, so why a year into development we don't have that?

MichaelBelgium commented 4 years ago

switching back to a HyperV VM which has a fix ip and more.

HyperV changes ip every restart too, WSL 2 and HyperV are using the same adapter afaik

Genda1ph commented 4 years ago

@MichaelBelgium depends on how you set it up. I have mine set up in bridged mode and VM manages its virtual NIC like any normal Linux box would.

protang commented 4 years ago

I give you a new idea: Instead of changing the IP, add a designated IP.

In Windows 10, run CMD or Powershell with administrator privilege, and then execute the following two commands:

:: Add an IP address in Ubuntu, 192.168.50.16, named eth0:1 wsl -d Ubuntu -u root ip addr add 192.168.50.16/24 broadcast 192.168.50.255 dev eth0 label eth0:1

:: Add an IP address in Win10, 192.168.50.88 netsh interface ip add address "vEthernet (WSL)" 192.168.50.88 255.255.255.0

In the future, you will use 192.168.50.16 when you access Ubuntu, and 192.168.50.88 when you access Win10. You can save the above two lines of commands as a .bat file, and then put it into the boot area, and let it execute automatically every time.

Genda1ph commented 4 years ago

Except this does nothing in cases where you need to have SSH connections to WSL from other device in LAN (which worked perfectly fine in WSL1). The solution here would be to allow selecting a bridged interface and let the VM handle its own network configuration.

justinkb commented 4 years ago

It's absurd to me this isn't fixed yet after a year. I mean, I find it inconceivable hyper-v itself wouldn't have the functionality of being able to specify the ip address of a virtual network interface to be created, so the issue must simply exist in how wsl2 implements this. Can't a person on the wsl team just ask the hyper-v team how to proceed here? Seems like a major failing of internal communication here to have this issue remain for a year

offero commented 4 years ago

Yes, it should work

Does not work for me. curl localhost:<port> works but curl local.custom.com:<port> does not

JurrianFahner commented 4 years ago

On my computer I needed to windows from scratch. I discovered that on the most recent windows 10 version the problems with wsl2 are fixed; I'm now able to access a webserver (running from wsl2) from my browser via localhost.

LunarLanding commented 4 years ago

I'm on Windows 2004 and still have this issue. PS: occurs after waking from sleep.

litecart-cs commented 4 years ago

More than a year reported issue and still no progress? Really?

DTCM86 commented 4 years ago

Please get this sorted Mr. Microsoft, we want true integration, and we want it now! Please :-)

AlexLu202 commented 4 years ago

@litecart-cs @DTCM86 You can actually set up the static ip address inside wsl. I used netplan to set up the static ip address(assuming your os is ubuntu). But some servers became unreachable after the setup. I'm still trying to figure out what's really going on. Maybe it's not a good idea to change it. Anyway, here's how to set static ip address. https://linuxize.com/post/how-to-configure-static-ip-address-on-ubuntu-20-04/

Genda1ph commented 4 years ago

@AlexLu202 I believe the issue lies with vSwitch used by WSL2, you'd need to connect it to a different network interface as well. Honestly, at this point I'd rather go with a full Hyper-V guest and expose a network share, that I could then use as a working environment. If anyone is interested, @ me and I'll see if I can come up with some simple instructions or maybe even an Ansible role.

ermshiperete commented 4 years ago

I give you a new idea: Instead of changing the IP, add a designated IP.

In Windows 10, run CMD or Powershell with administrator privilege, and then execute the following two commands:

:: Add an IP address in Ubuntu, 192.168.50.16, named eth0:1 wsl -d Ubuntu -u root ip addr add 192.168.50.16/24 broadcast 192.168.50.255 dev eth0 label eth0:1

:: Add an IP address in Win10, 192.168.50.88 netsh interface ip add address "vEthernet (WSL)" 192.168.50.88 255.255.255.0

In the future, you will use 192.168.50.16 when you access Ubuntu, and 192.168.50.88 when you access Win10. You can save the above two lines of commands as a .bat file, and then put it into the boot area, and let it execute automatically every time.

To be able to access from another device on the LAN I had to add another line to the batch file:

netsh interface portproxy add v4tov4 listenport=22 listenaddress=0.0.0.0 connectport=22 connectaddress=192.168.50.16

transtone commented 3 years ago

windows can use wsl2's service by localhost:port, but wsl2 can't use windows's service. so we can change the dynamic ip to a domain:

echo `cat /etc/resolv.conf|grep nameserver|awk '{print $2}'` win10.local >> /etc/hosts

then we can use win10.local as a static domain.

Genda1ph commented 3 years ago

@transtone please, stop with irrelevant workarounds.

We need a fix, for an issue that is over a year old.

transtone commented 3 years ago

@transtone please, stop with irrelevant workarounds.

We need a fix, for an issue that is over a year old.

Before it been fixed, may some one like me want to find a temporary workarounds? It may irrelevant you, so you can just fix it and close this issue.

gwharton commented 3 years ago

HyperV, virtual switch shared with pc, full Ubuntu, static networking, vscode with remote SSH module. Flush wsl down the crapper. Now stop your whining!!!!! Out.

aki-k commented 3 years ago

I was able to define a static IP address in WSL 2 VM like this on Windows 10 Home:

1) enable systemd in your WSL 2: https://github.com/DamionGans/ubuntu-wsl2-systemd-script

2) bridge the vEthernet (WSL) and your ethernet Local Area Connection in Windows' Network Connections

3) /etc/resolv.conf is linked to ../run/resolvconf/resolv.conf but resolvconf was not installed. To fix:

cat > /etc/wsl.conf <<EOF
[network]
generateResolvConf = false
EOF

4) use systemd-networkd to configure a static IP address in your WSL 2 VM

/etc/systemd/network/lan.network:

[Match]
Name=eth0

[Network]
Description=lan
DHCP=no
Address=192.168.1.20/24
Gateway=192.168.1.1
DNS=192.168.1.1
LLDP=true
EmitLLDP=true

(shutdown the VM on Windows side with wsl --shutdown and start it again)

rm -f /etc/resolv.conf

echo "nameserver 192.168.1.1" > /etc/resolv.conf

To test:

1) install/configure/enable/start ssh.service in WSL 2 VM

apt-get install openssh-server
dpkg-reconfigure openssh-server # this creates the ssh host keys in /etc/ssh
systemctl enable ssh
systemctl start ssh

2) connect to port 22 on your WSL 2 VM IP 192.168.1.20

bigben932 commented 3 years ago

I was able to define a static IP address in WSL 2 VM like this on Windows 10 Home:

1. enable systemd in your WSL 2: https://github.com/DamionGans/ubuntu-wsl2-systemd-script

2. bridge the vEthernet (WSL) and your ethernet Local Area Connection in Windows' Network Connections

3. /etc/resolv.conf is linked to ../run/resolvconf/resolv.conf but resolvconf was not installed. To fix:
cat > /etc/wsl.conf <<EOF
[network]
generateResolvConf = false
EOF
1. use systemd-networkd to configure a static IP address in your WSL 2 VM

/etc/systemd/network/lan.network:

......

Do not do this. It will break your Hyper networking and cause other yet unreasonable problems. While it technically works, but the WSL network adapter is recreated after every reboot, which means you have to first start wsl and then you have to re-bridge the network adapters. I'm not sure if there is a good way to undo these changes so I would strongly not recommend doing this to a WSL which is already setup and you are using.

aki-k commented 3 years ago

It will break your Hyper networking and cause other yet unreasonable problems.

What's the problem you experienced using this solution?

bigben932 commented 3 years ago

It will break your Hyper networking and cause other yet unreasonable problems.

What's the problem you experienced using this solution?

Like I said it breaks hyper-v networking. And in addition, everytime you reboot the machine, you have to manually add the WSL virtual network adapter back to the network bridge. There is no way to script creation of network bridges nor are there reg keys which you can change.

and there is no clear way (that I see at the moment) to remove the changes from the systemd script hack.

dmokel commented 3 years ago

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.

hello, I have the same problem. I use docker in windows10, and I found that the ip address on vEthernet (WSL) change everytime when I restart windows. It leads to I can't access Windows resources from Linux. For example, I run an application using docker to access the mySQL running on windows host, I can't connect to mysql-server running on windows when I use the orignal config. because ip address on the vEthernet (WSL) had changed, but the mysql-host ip in my original config as same as last time. What can I do to keep the IP address of vEthernet (WSL) unchanged when I restart windows? thanks~

kangaroonote1972 commented 3 years ago

Add ip address when wsl starts For example wsl -d kali-linux -- ifconfig eth0 add 172.31.155.100 netmask 255.255.240.0 broadcast 172.31.159.255

Genda1ph commented 3 years ago

wsl -d kali-linux -- ifconfig eth0 add 172.31.155.100 netmask 255.255.240.0 broadcast 172.31.159.255

...which creates an interface in WSL-managed V-Switch, i.e. useless.

Again, the proper fix would be to allow us to assign the WSL distro to a different V-Switch.

justinkb commented 3 years ago

FWIW, I migrated my setup to hyper-v, where admittedly this still isn't that easy to set up, but at least you get it working properly.