WSL
WSL copied to clipboard
WSL2 Set static ip?
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.
There is a work around. You can port forward to host ip. Check the below link. https://github.com/microsoft/WSL/issues/4150
I agree with the author of the question, please, Wsl2 must have option "static IP". Maybe cmd: wsl --ip <Distro> <IP address>?
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.
@alekseymvt
You could execute any command using wsl like this
wsl -- ifconfig eth0
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
Looking for a solution too :)
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
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
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.
@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 check out this https://github.com/shayne/go-wsl2-host you can do it automatically with this service
@MiklerGM Thanks, I've seen it. I'm OK with my own self-written script.
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

@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?
maybe build 18945 https://devblogs.microsoft.com/commandline/whats-new-for-wsl-in-insiders-preview-build-18945/
@yanyan33333 I'm already on build 18945 but still no joy

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
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 it works ,I start nginx on wsl and I can visit the website in windows brower by using localhost
@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
@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
@alekseymvt it is working for me

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