Skip to content

Instantly share code, notes, and snippets.

@ps2goat
Last active August 9, 2023 18:40
Show Gist options
  • Save ps2goat/f885ad790178ed9e8012b0681a0ef61d to your computer and use it in GitHub Desktop.
Save ps2goat/f885ad790178ed9e8012b0681a0ef61d to your computer and use it in GitHub Desktop.
Fix wsl2 dns issues
[boot]
command="printf \"nameserver 1.1.1.1\n$(cat /etc/resolv.conf)\nnameserver 8.8.8.8\nnameserver 1.0.0.1\n\" > /etc/resolv.conf"
# Where does this go?
# In each WSL2 distribution, add this to your `/etc/wsl.conf` file.
#
# What this does:
# Writes 3 nameservers to your WSL2 distribution, and inserts the dynamic nameserver (the WSL2 -> Windows bridge) from
# /etc/resolv.conf at level 2, and re-writes all that to the same /etc/resolv.conf file.
#
# 1. A subprocess reads the contents of the newly created file at boot, which has the bridge IP in it
# by default. `$(cat /etc/resolv.conf)`
# 2. The `printf` command injects the result of that after the first nameserver, and before the other two (configure as needed)
# 3. The value to print is surrounded with double quotes, so that variable substitution can happen.
# These are already in double quotes due to `command = "<full command>"`, so they are escaped: `\"`
# 4. The results of what is printed are written back to `/etc/resolv.conf`.
#
# Why?
# I had several issues with DNS inside the Linux distribution (several Ubuntu versions). For example,
# terraform provider lookup failed after a few tries and it only tries twice (thus I put the public nameserver
# before the Windows bridge).
# I don't remember where I saw that specific number of DNS attempts, but Possible Cause #3 at this link seems to be the reason.
# https://support.hashicorp.com/hc/en-us/articles/5511799316243-Error-Failed-to-query-available-provider-packages
#
# Comments were added to this file only to keep them at the start-- the original thread I responded to only loads later comments
# and you have to manually click "Load earlier comments" to see the author's initial comment!
# Original post was made here:
# https://gist.github.com/coltenkrauter/608cfe02319ce60facd76373249b8ca6?permalink_comment_id=4263955#gistcomment-4263955
@ps2goat
Copy link
Author

ps2goat commented Aug 9, 2023

I think I also had the same issue with Pypi when on a work VPN, but I did not test this out. In that case, I just disconnected from the work VPN and pip would work. Terraform was the first time I actually had a problem where NOTHING would download, but I also experienced faster homebrew downloads and installs after fixing the DNS this way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment