How to Fix WSL2 DNS Issues

 

Windows Subsystem for Linux 2 (WSL2) provides a powerful and convenient Linux environment on Windows. However, users often encounter DNS resolution issues that can impact connectivity. Fortunately, a one-liner shell script has been developed by epomatti to streamline the setup process, using Google's custom nameserver 8.8.8.8. In this article, we'll explore the easy-to-follow steps to enhance your WSL2 experience.

 

Usage

Clone and run the shell:

git clone https://github.com/epomatti/wsl2-dns-fix-config.git cd wsl2-dns-fix-config sudo sh ./run.sh

Restart WSL:

wsl --shutdown

Is GitHub DNS not resolving?

You can try curl --resolve with one of GitHub's IP addresses to force DNS resolution:

curl --resolve 'codeload.github.com:443:20.201.28.149' 'https://codeload.github.com/epomatti/wsl2-dns-fix-config/tar.gz/refs/tags/v1.0.1' -o wsl2-dnsfix.tar.gz tar -xf wsl2-dnsfix.tar.gz cd wsl2-dns-fix-config-1.0.1 sudo sh ./run.sh

This will work in distros like Ubuntu 22.04, which are not recognizing out of the box. Yet another way is to get the archive from the releases:

curl -L 'https://github.com/epomatti/wsl2-dns-fix-config/archive/refs/tags/v1.0.1.tar.gz' -o wsl2-dnsfix.tar.gz

What it does

The run.sh script will perform these tasks:

  1. Delete the following files: /etc/wsl.conf and /etc/resolv.conf

  2. Create the new ".conf" files (pre-created in the dist folder) setting Google DNS for name resolution and preventing WSL from overriding it:

    # /etc/wsl.conf [network] generateResolvConf = false

    # /etc/resolv.conf nameserver 8.8.8.8

  3. Make /etc/resolv.conf immutable

    1. This means running the command sudo chattr +i /etc/resolv.conf

    2. If the above command give the error seen in the image below,

       

      image

       

      chattr: Operation not supported while reading flags on /etc/resolv.conf

      run

      sudo chattr -f +i /etc/resolv.conf instead

Reference: wsl2-dns-fix-config

 

Post a Comment

Previous Post Next Post