The DNS address in
/etc/resolv.conf within WSL2 is a dynamically generated file that mirrors the host Windows network settings. When you switch networks, this file does not automatically update because it is mounted as a read-only symlink to a temporary file managed by the Hyper-V virtual switch. To resolve this issue and ensure your Linux environment uses the correct DNS servers for the new network, you must either configure WSL2 to use a static DNS server or manually intervene with network adapter settings on the Windows host.
Understanding the Dynamic Nature of resolv.conf
In WSL2, the file
/etc/resolv.conf is not a persistent configuration file like it is in traditional Linux distributions. Instead, it is a symbolic link pointing to a temporary file located at
/run/wslg/resolv.conf or similar paths depending on the version. This temporary file is populated by the WSL daemon based on the DNS servers assigned to the Windows host's active network interface.
When you connect to a new Wi-Fi network or switch from Ethernet to Wi-Fi, Windows updates its own DNS configuration. However, the WSL2 virtual machine does not receive an immediate notification to refresh its internal DNS cache or remount the
resolv.conf file. Consequently, your Linux distribution continues to query the old DNS servers, leading to resolution failures or timeouts until the WSL instance is restarted or the underlying network state is forcibly refreshed. This behavior is by design to maintain network isolation and stability between the host and the guest OS.
Why Restarting WSL Helps
Restarting the WSL service forces the daemon to re-evaluate the host's network state. By killing all running instances and restarting the service, you trigger a fresh generation of the
resolv.conf content. While effective, this approach disrupts any running processes in your Linux environment, making it unsuitable for production workflows or long-running services.
Configuring Static DNS via wsl.conf
A more robust solution is to override the dynamic DNS assignment by configuring a static DNS server in the WSL2 configuration file. This method ensures that regardless of the host network change, your Linux VM always uses a reliable DNS resolver, such as Google's public DNS or your corporate internal DNS.
To implement this, create or edit the
wsl.conf file in the root directory of your Linux distribution. You can do this by running the following command in your WSL terminal:
sudo nano /etc/wsl.conf
Add the following section to the file:
[network]
generateResolvConf = false
After saving the file, you must manually create a standard
/etc/resolv.conf file. Since
generateResolvConf is set to false, WSL will no longer manage this file. You can create it with your preferred DNS servers:
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'
sudo bash -c 'echo "nameserver 8.8.4.4" >> /etc/resolv.conf'
Ensure the permissions are set correctly so that the file is readable:
sudo chmod 644 /etc/resolv.conf
Finally, restart WSL to apply the changes. Open PowerShell or Command Prompt on Windows and run:
wsl --shutdown
Then relaunch your Linux distribution. Your DNS queries will now bypass the dynamic host mapping and go directly to the specified servers. Note that if you need to change DNS servers later, you must edit
/etc/resolv.conf directly again.
Managing Network Adapter Settings on Windows
If you prefer to keep the dynamic DNS behavior but want to force an update without restarting WSL, you can manipulate the Windows network adapter settings. The WSL2 virtual switch relies on the host's primary network interface. Sometimes, disabling and re-enabling the adapter triggers a refresh of the IP and DNS configurations that WSL detects.
1. Open the
Network Connections panel in Windows (press
Win + R, type
ncpa.cpl, and hit Enter).
2. Identify the active network adapter (Wi-Fi or Ethernet).
3. Right-click the adapter and select
Disable.
4. Wait approximately 10 seconds.
5. Right-click the adapter again and select
Enable.
This action forces Windows to renew its DHCP lease and update its DNS cache. In many cases, WSL2 will detect this change and update its internal DNS records upon the next network request. However, this method is not guaranteed to work in all scenarios, particularly if the WSL daemon has cached the previous state aggressively.
Advanced Troubleshooting with Custom DNS Routes
For users requiring complex network setups, such as those using VPNs or custom routing tables, managing DNS in WSL2 can become intricate. If you are using tools like Twingate or other enterprise VPN solutions, you may need to configure specific DNS routes to ensure connectivity. For detailed guidance on automating DNS configuration in these advanced scenarios, refer to our article on
Automatically Configure DNS Server Routes for WSL2 Ubuntu on Windows with Twingate VPN.
Additionally, if you are experiencing broader networking issues alongside DNS problems, such as mirrored network mode compatibility, it is worth reviewing how WSL2 handles network interfaces. See
WSL2 and Mirrored Network Mode: A Compatibility Overview for Dev Projects for insights into network topology impacts.
Conclusion
The inability of
/etc/resolv.conf to update automatically on network change is a known limitation of WSL2's architecture. The most reliable fix for developers is to disable automatic generation in
wsl.conf and specify static DNS servers. This eliminates dependency on the host's transient network state and provides consistent name resolution across different environments.
Frequently Asked Questions
Can I make WSL2 automatically detect DNS changes without restarting?
No, WSL2 does not support live hot-reloading of
/etc/resolv.conf when the host network changes. The file is regenerated only during the startup of the WSL service or when explicitly triggered by shutdown commands. You must restart WSL or use a static DNS configuration to avoid manual intervention.
Does setting generateResolvConf to false break internet access?
No, it does not break internet access as long as you provide valid DNS servers in
/etc/resolv.conf. In fact, it often improves reliability because your Linux environment is no longer dependent on potentially misconfigured or slow host DNS servers. Ensure you use public resolvers like 8.8.8.8 or your local network's DNS gateway.
How do I check which DNS servers WSL2 is currently using?
You can view the current DNS configuration by opening your WSL terminal and running the command
cat /etc/resolv.conf. This will display the nameserver entries currently active in your Linux distribution. If it shows dynamic entries, automatic generation is still enabled.
What happens to my existing processes if I use wsl --shutdown?
Running
wsl --shutdown terminates all running Linux processes and closes all connected terminals. Any unsaved work in background services or long-running scripts will be lost. Always save your work and stop critical services before shutting down the WSL instance to prevent data loss.
Is this issue specific to Windows 11?
No, this behavior exists in both Windows 10 and Windows 11 WSL2 implementations. It is inherent to how the Hyper-V virtual switch manages network interfaces and DNS forwarding. The troubleshooting steps provided are applicable to all versions of WSL2 supported on Windows 10 and 11.