Can I set a static ip address for WSL 2?

Viewed 3782

I'm doing some Ruby on Rails development on a WSL 2. The rails app connects to a MySQL server running on my Windows host. When I used WSL 1 it as easy to set the host to 127.0.0.1 but on WSL 2 I have to use the nameserver written in /etc/resolv.conf

But I found out that IP address written in resolv.conf changes on reboots.

Is there a way to set a static IP address for WSL 2 ?

2 Answers

You can consider ocroz/wsl2-boot (see INSTALL for installation instructions)

After running wsl-boot command:

  • The WSL network is configured as per its predefined definition always (Windows side),
  • All WSL hosts are configured with their predefined static IP always (Linux side),
  • All WSL hosts and connected Hyper-V VMs can talk to each other always,
  • The DNS resolution works however you are connected to Internet or VPN,
  • You can SSH to WSL host without any delay.

If you don't want /etc/hosts, /etc/resolv.conf to change after restart, you can create /etc/wsl.conf

[network]
generateHosts = false
generateResolvConf = false
Related