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

Static IP on WSL 2 #418

Closed
yanyan33333 opened this issue Jun 14, 2019 · 26 comments
Closed

Static IP on WSL 2 #418

yanyan33333 opened this issue Jun 14, 2019 · 26 comments

Comments

@yanyan33333
Copy link

yanyan33333 commented Jun 14, 2019

hello, I use WSL 2 in windows 10 insider
and I found that the ip address on WSL 2 change everytime when I restart windows
can I use the static ip on WSL 2?

also how can I start ssh service on boot?
thx~

@WSLUser
Copy link
Contributor

WSLUser commented Jun 25, 2019

You'll need to create an init script for ssh to auto start when you launch WSL (doesn't matter WSL1 or WSL2). Pengwin will automatically do this for you if you ask it from pengwin-setup. As far as IP address goes, you can make the IP address static just like on Linux. Here's a quick look from a recent article: https://danielmiessler.com/study/manually-set-ip-linux/

@WSLUser
Copy link
Contributor

WSLUser commented Jun 25, 2019

@mscraigloewen It may be a good idea to create a new doc going over common configurations such as creating the init script for ssh and others that are common. Anyone could go to the Pengwin repo to get the ssh script, I linked to a few others in the FedoraRemix repo (such as memcached and nginx) but I'm sure it would ease a lot of developers time and effort to just have an authoritative doc that goes over standard practices and procedures. Init scripts are just a part of it. Linking to the dev-box-setup repo would also be a good idea for that doc.

@craigloewen-msft
Copy link
Collaborator

@WSLUser yes that's a good idea! I'll look into making some of those docs.

@yanyan33333
Copy link
Author

You'll need to create an init script for ssh to auto start when you launch WSL (doesn't matter WSL1 or WSL2). Pengwin will automatically do this for you if you ask it from pengwin-setup. As far as IP address goes, you can make the IP address static just like on Linux. Here's a quick look from a recent article: https://danielmiessler.com/study/manually-set-ip-linux/

It`s not working, the Hyper-V Switch IP change everytime when the windows reboot, so the gateway in wsl 2 also need to change

@yanyan33333
Copy link
Author

@mscraigloewen Is it no way to make the IP in Hyper-V Switch on windows and linux on WSL 2 not to change every reboot right now ?

@WSLUser
Copy link
Contributor

WSLUser commented Jul 13, 2019

There's a technical issue with WSL2 in that regard. I saw there was an issue about it on https://github.com/Microsoft/WSL/issues/ but I don't recall the issue number unfortunately. From what I remember, you'll need something like a script right now that automatically runs when you launch a WSL2 instance.

@craigloewen-msft
Copy link
Collaborator

craigloewen-msft commented Jul 15, 2019

As of right now I'm not aware of a way to make the IP of the Hyper-V switch static.

We are working on improving our networking story so you can connect to Linux apps using 'localhost' instead of a remote IP address which should address your issue!

@kpeters-cbsi
Copy link

There's a workaround posted, but I haven't tried it.

microsoft/WSL#4150 (comment)

@peidaqi
Copy link

peidaqi commented Jul 24, 2019

@mscraigloewen it doesn't work when you need to access Windows resources from Linux, say, you want to run a Linux app to access the SQL Server running on Windows host.

@WSLUser
Copy link
Contributor

WSLUser commented Jul 24, 2019

say, you want to run a Linux app to access the SQL Server running on Windows host.

I think the 9P server would probably be able to work for it. Granted perf will likely not be great but assuming 9P works as intended in WSL2, it should provide you an workaround option. Also keep in mind AF_Unix. Though I think the interop story was broken, so that use-case may not be entirely workable even with 9P.

@peidaqi
Copy link

peidaqi commented Jul 24, 2019

@WSLUser Not sure what you mean by 9P server. If you're refering to the MSSQL Server for Linux, yes it works but I also hope to be able to use the Management Studio to easily explore my data...

@WSLUser
Copy link
Contributor

WSLUser commented Jul 24, 2019

No, 9P is a network share feature for allowing Windows to access Linux files that’s built into init. A separate feature from the networking story discussed in this issue. You can find out more about it in a MS blog.

@craigloewen-msft
Copy link
Collaborator

@peidaqi what do you mean by it doesn't work? If you follow the instructions here (make sure to read the 'Other networking considerations section as well as that might be where the error comes from!) does it still not work?

If it still doesn't work please consider opening an issue on our WSL Github repo: https://github.com/microsoft/wsl

@yanyan33333
Copy link
Author

@mscraigloewen thx
the last build version 18945 solaved
https://devblogs.microsoft.com/commandline/whats-new-for-wsl-in-insiders-preview-build-18945/

@yanyan33333
Copy link
Author

@mscraigloewen Is there any solution to start wsl when windows booting?

@craigloewen-msft
Copy link
Collaborator

@yanyan33333 there's a program called 'Task Scheduler' on Windows that can start programs on log on. You can set this up to run wsl.exe on login. I'd encourage you to take a look online on how to do so, if you aren't able to do get it working please let me know!

@protang
Copy link

protang commented Jun 24, 2020

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.

@samgithub
Copy link

protang -- Ths works "temporarily". Sometimes, it persists after a reboot. Sometimes after 3-4 days the IP vanishes.

@akhiljalagam
Copy link

akhiljalagam commented Sep 5, 2020

Hyper-V bridged mode

wsl --shutdown
# assign statis ip address
wsl -d Ubuntu -u root bash -c "sudo ip a flush dev eth0; sudo ip r del default; ip addr add 192.168.1.50/24 broadcast 192.168.1.255 dev eth0; sudo ip r add default via 192.168.1.1"

# update resolv.conf
wsl -d Ubuntu -u root bash -c 'echo -e "options single-request\nnameserver 8.8.8.8\nnameserver 8.8.4.4" ^> /etc/resolv.conf'

@litecart-cs
Copy link

Still impossible to set a static IP?

@emandret
Copy link

Hey @craigloewen-msft do you have any news on that?

I recently tried to assign a static IP to my wsl instance and I noticed the IP get randomized at each wsl reboot, this is mainly because the wsl vSwitch is deleted when wsl is not running. It would be really, really great if the wsl vSwitch could be permanently configured, along with the NAT settings associated with the Windows host and the hyper-v wsl instance.

Thanks in advance.

@craigloewen-msft
Copy link
Collaborator

We don't yet have any concrete news here. I can tell you that we are working on improving WSL's networking story and are taking in static IP as a consideration, but we don't yet have anything to share in this area. Thanks for being patient!

@Falseclock
Copy link

  1. create any executable script on wsl2
#!/bin/bash

/sbin/ip addr add 192.168.101.161/24 broadcast 192.168.101.255 dev eth0 label eth0:1
/mnt/c/Windows/System32/netsh.exe interface ipv4 add address "vEthernet (WSL)" 192.168.101.162 255.255.255.0
  1. edit crontab and add your script
@reboot /path/to/the/script
  1. add to /etc/sudoers
%sudo ALL=NOPASSWD: /etc/init.d/cron *
  1. on windows side create vbs script
Set oShell = CreateObject ("Wscript.Shell") 

Dim strArgs
strArgs = "C:\Windows\System32\wsl.exe -d Debian sudo /etc/init.d/cron start"
oShell.Run strArgs, 0, false
  1. put this script to windows autostart

@wanderleihuttel
Copy link

Hello

Is there any update regarding this? It's awful have to look for the IP every time you need to access the WSL.
This should be something trivial.

@tomrus88
Copy link

tomrus88 commented Jun 4, 2022

@craigloewen-msft Its been years since users are asking for this feature. Why does it take so long to implement? I'm like many others out there want to have static IP (and also static MAC address) in WSL2...

Why can't we just have an access to those virtual Hyper-V network adapters used by WSL2 guests (i've tried to find them without success) and be able to configure them the way we want it (assign ip, assign mac etc)?

@nmydt
Copy link

nmydt commented Jul 16, 2023

@Falseclock If I copy a Ubuntu 18.04, modify one of the static IP will lead to another Ubuntu18.04 IP will also change, I want to make these two have static IP, what is a good way

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

No branches or pull requests