Fixing DNS Resolution Problems in WSL2
Hello!
Windows Subsystem for Linux (WSL2) is a wonderful feature that lets you run a Linux environment on Windows, but some users run into DNS resolution problems. This article covers the symptoms and an effective way to fix them.
Test Environment
The method described in this article was verified with the following versions
WSL version: 2.4.13.0
Kernel version: 5.15.167.4-1
WSLg version: 1.0.65
MSRDC version: 1.2.5716
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.26100.1-240331-1435.ge-release
Windows version: 10.0.22631.3880
Symptoms
If you see error messages like the following, there is a good chance you are hitting a DNS resolution problem in WSL2:
- When connecting from the command line:
Could not resolve hostname ... - When connecting from Python code:
[Errno -2] Name or service not known
These errors indicate that the DNS server configuration inside WSL2 is not set up correctly.
Cause
On startup, WSL2 automatically generates the /etc/resolv.conf file and tries to use the Windows-side DNS settings. However, there are cases where this auto-generated DNS configuration does not work correctly. As the version information above shows, this problem is still not fully resolved even in the latest WSL2 as of March 2025 (version 2.4.13.0).
Solution
The problem can be fixed in the following 3 steps
STEP 1: Disable WSL2's auto-generated DNS configuration
Run the following one-liner in WSL bash to prevent resolv.conf from being auto-generated.
Note: if you already have a wsl.conf, edit it with vim or another editor instead.
sudo sh -c 'cat > /etc/wsl.conf << EOF
[network]
generateResolvConf = false
EOF'
This command creates the WSL2 configuration file /etc/wsl.conf and disables automatic generation of the DNS configuration.
STEP 2: Restart WSL from the Windows side
Run the following command in Windows PowerShell or Command Prompt to fully restart WSL
wsl --shutdown
STEP 3: Configure the DNS servers manually
Start WSL again and run the following command in the shell to manually configure Google's public DNS servers
sudo sh -c 'cat > /etc/resolv.conf << EOF
nameserver 8.8.8.8
EOF'
This configures WSL to use Google's DNS servers (8.8.8.8 and 8.8.4.4).
Verification
To check that the settings have been applied correctly, try running the following command
ping google.com
Or run a connection test with the following Python code
import socket
socket.gethostbyname('google.com')
To check the WSL kernel version, you can use the following commands
# Show the kernel version (short form)
uname -r
# Show detailed kernel version information
cat /proc/version
# Check overall system information (if systemd is available)
hostnamectl
To check the WSL version information from Windows Command Prompt or PowerShell
wsl --version
If these work correctly, the DNS resolution problem has been fixed.
Caveats
- These settings may be reset each time WSL restarts. If that happens, run STEP 3 again.
- Behavior may differ depending on your WSL version. For the latest information, we recommend consulting Microsoft's official documentation.
Conclusion
DNS resolution problems in WSL2 can be fixed by disabling the auto-generated DNS configuration and setting the DNS servers manually. This method works even on the latest WSL2 versions.