WSL icon indicating copy to clipboard operation
WSL copied to clipboard

WSL2 Set static ip?

Open scrossan-crown opened this issue 6 years ago • 219 comments

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.

scrossan-crown avatar Jun 21 '19 08:06 scrossan-crown

There is a work around. You can port forward to host ip. Check the below link. https://github.com/microsoft/WSL/issues/4150

edwindijas avatar Jun 22 '19 07:06 edwindijas

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

zippaaa avatar Jun 22 '19 21:06 zippaaa

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.

scrossan-crown avatar Jun 24 '19 07:06 scrossan-crown

@alekseymvt You could execute any command using wsl like this

wsl -- ifconfig eth0

MiklerGM avatar Jul 03 '19 10:07 MiklerGM

Any solution? also see https://github.com/MicrosoftDocs/WSL/issues/418#issuecomment-511104330

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

yanyan33333 avatar Jul 13 '19 09:07 yanyan33333

Looking for a solution too :)

GhostvOne avatar Aug 01 '19 06:08 GhostvOne

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

MiklerGM avatar Aug 01 '19 06:08 MiklerGM

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?

zippaaa avatar Aug 01 '19 07:08 zippaaa

Yes, it should work

MiklerGM avatar Aug 01 '19 07:08 MiklerGM

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.

scrossan-crown avatar Aug 01 '19 07:08 scrossan-crown

@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

moigagoo avatar Aug 01 '19 07:08 moigagoo

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

MiklerGM avatar Aug 01 '19 09:08 MiklerGM

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

moigagoo avatar Aug 01 '19 09:08 moigagoo

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

MiklerGM avatar Aug 01 '19 09:08 MiklerGM

@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?

moigagoo avatar Aug 01 '19 10:08 moigagoo

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

yanyan33333 avatar Aug 01 '19 11:08 yanyan33333

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

image

scrossan-crown avatar Aug 01 '19 11:08 scrossan-crown

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

GhostvOne avatar Aug 01 '19 11:08 GhostvOne

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.

scrossan-crown avatar Aug 01 '19 14:08 scrossan-crown

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

yanyan33333 avatar Aug 02 '19 06:08 yanyan33333

@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

scrossan-crown avatar Aug 02 '19 07:08 scrossan-crown

@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

zippaaa avatar Aug 02 '19 09:08 zippaaa

@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

MiklerGM avatar Aug 02 '19 11:08 MiklerGM

@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 :)

zippaaa avatar Aug 02 '19 13:08 zippaaa

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

craigloewen-msft avatar Aug 07 '19 00:08 craigloewen-msft

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

webfaqtory avatar Aug 19 '19 08:08 webfaqtory

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.

sth4me avatar Aug 29 '19 06:08 sth4me

Rider that really what is needed is a static MAC address, on which a static IP can follow as a side effect if desired. Ref #4454, use case being that some software uses the MAC address to implement intellectual property control.

Really the IP address is neither here nor there; the address needs a stable name. Depending on some memorized IP addresses goes against best practice. Acknowledging that this is what a lot of people do, and it is a valid ask. Noting the strategy doesn't work so good with ipv6.

therealkenc avatar Sep 04 '19 18:09 therealkenc

Someone was looking at a $WSL_HOST_IP approach in July ref #4212 (message) although the repo appears empty now.

You can get the WSL2 instance IP address easy enough:

C:>wsl.exe -d Ubuntu-18.04 /bin/bash -c "hostname -I | awk '{print $1}'"
172.21.141.235

And the other direction:

$ ip route | grep default | awk '{print $3}'
172.21.128.1

Those can be assigned to environment variables to taste. But that's not really the ask(s).

therealkenc avatar Sep 04 '19 20:09 therealkenc

Changing the guest VM static IP address here doesn't satisfy the behavior that the WSL-side bridge /16 will change on every subsequent reboot. The problem needs to be resolved on both Host side and Guest side, and we cannot control Host.

You can be deterministic about the IP address you get within 172.21.0.0/16 -- but we cannot even be sure you are in 172.21.0.0/16 in the first place.

jpsenior avatar Sep 08 '19 17:09 jpsenior