Hostnames Resolved Incorrect IPs WSL: Troubleshooting Guide
When using the Windows Subsystem for Linux (WSL), you might encounter issues where hostnames are resolved to incorrect IP addresses. This can cause connectivity problems and make it difficult to access network services. This guide will help you diagnose and resolve these issues.
Understanding the Issue
The cause of this issue can be a misconfiguration in the WSL's networking stack or a DNS resolution issue. To identify the root cause, you need to understand how hostnames are resolved in WSL and the components involved in this process.
- WSL uses a custom networking stack that runs on top of the Windows network stack.
- When you access a hostname in WSL, the system first checks its local hosts file.
- If the hostname is not found in the local hosts file, it queries the configured DNS servers for resolution.
Troubleshooting Steps
Follow these steps to diagnose and resolve hostname resolution issues in WSL:
Step 1: Check the Hosts File
Check if the incorrect IP address is present in the WSL hosts file located at /etc/hosts:
$ cat /etc/hosts
...
If you find an incorrect entry, either remove or update it with the correct IP address.
Step 2: Verify DNS Configuration
Check the DNS configuration in WSL by querying the configured DNS servers:
$ nslookup example.com
Server: 127.0.0.53
Address: 127.0.0.53#53
Non-authoritative answer:
Name: example.com
Address: 192.0.2.1
If the IP address returned is incorrect, check your DNS server configuration by running:
$ cat /etc/resolv.conf
nameserver 1.1.1.1
search example.com
Update the nameserver to a valid public DNS server, like Cloudflare's 1.1.1.1 or Google's 8.8.8.8, or use your organization's DNS server if required.
Step 3: Restart WSL
After making any changes, restart the WSL distribution to apply the modifications:
$ exit
# From the Windows command prompt, run:
wsl --shutdown
# Then, start the WSL distribution again.
- Hostnames are resolved in WSL using the local hosts file and DNS servers.
- Check the hosts file for incorrect entries and update or remove them as necessary.
- Verify DNS server configuration and update it to a valid public DNS server or your organization's DNS server.
- Restart WSL after making any changes.