microsoft / WSL

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

Provide static IP option in .wslconfig (or elsewhere) #6858

Open kevinyhong opened 3 years ago

kevinyhong commented 3 years ago

Is your feature request related to a problem? Please describe. Currently, I'm attempting to host my own webserver on my machine using WSL2, but because of the changing IP address on VM restart, the domain I registered keeps losing the webserver and I would have to update it on the domain manager's site. I know the simple fix would be to use a CSP, but I'm trying to limit any issues/unexpected costs from occurring during development.

Describe the solution you'd like The ability to configure a static IP for the VM through something as simple as the .wslconfig file

Describe alternatives you've considered I've read over

okolvik-avento commented 3 years ago

While waiting for such a feature, i've been using https://github.com/shayne/go-wsl2-host in local development. It runs as a Windows service and updates the Windows hosts file automatically based on values in a simple text file in the $HOME directory in the wsl distro.

giovannicandido commented 3 years ago

The go-wsl2-host solution is not working in my current windows setup because it has no permissions to write to the hosts file

luxzg commented 1 year ago

Since I just stumbled on this issue, I'd like to point everyone to the fact that vSwitch can now be used to directly connect WSL2 distro, officially, no hacks, using networkingMode=bridged option, and then setting static IP address. You do need W11 Pro, which shouldn't be an issue. I've compiled a nice big guide / tutorial couple of weeks ago as a comment in the main networking thread of issue 4150.

Likewise, I've added it to my GitHub, with some more polish, comments and tweaks added over time: https://github.com/luxzg/WSL2-fixes

I went through W10 to W11 upgrade (linked instructions for those with "unsupported" hardware), WSL2 Preview (now stable 1.0.1), creation of vSwitch, configuration of the network bridge, and as a bonus systemd configuration on Ubuntu with complete systemd based networking setup (networkd, resolved). I even threw in tests with web server and GUI apps. It should be doable for both newbies and experienced users alike. Btw, I can now tear down and setup new WSL2 instance with proper networking and full apt upgrades and all in just a few minutes, easy-peasy. I've even added two shorter guides / references for those that (re)install distros all the time - all in that same GitHub repo linked above.

Hope this information helps someone!