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~
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/
@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.
@WSLUser yes that's a good idea! I'll look into making some of those docs.
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
@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 ?
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.
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!
There's a workaround posted, but I haven't tried it.
https://github.com/microsoft/WSL/issues/4150#issuecomment-504209723
@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.
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.
@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...
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.
@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
@mscraigloewen thx
the last build version 18945 solaved
https://devblogs.microsoft.com/commandline/whats-new-for-wsl-in-insiders-preview-build-18945/
@mscraigloewen Is there any solution to start wsl when windows booting?
@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!
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.
protang -- Ths works "temporarily". Sometimes, it persists after a reboot. Sometimes after 3-4 days the IP vanishes.
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'
Still impossible to set a static IP?
Most helpful comment
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.0In 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.