Skip to content

Instantly share code, notes, and snippets.

@djfdyuruiry
Last active February 27, 2025 10:21

Revisions

  1. djfdyuruiry revised this gist May 28, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -35,4 +35,4 @@ This guide will help overcome the DNS issues present in WSL 2 while still making
    # it for XServer setup etc.
    ```

    - Exit and then go back into WSL terminal
    - Exit and then go back into your WSL terminal
  2. djfdyuruiry revised this gist May 28, 2020. 1 changed file with 0 additions and 6 deletions.
    6 changes: 0 additions & 6 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -22,12 +22,6 @@ This guide will help overcome the DNS issues present in WSL 2 while still making
    %admin ALL = (root) NOPASSWD: /opt/configure-wsl-dns.sh
    ```

    - Fix an issue with `dbus`:

    ```bash
    sudo sh -c "dbus-uuidgen > /etc/machine-id"
    ```

    - Update `~/.zshrc` or `~/.bashrc`:

    ```bash
  3. djfdyuruiry revised this gist May 28, 2020. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    # Fix DNS Issues in WSL 2

    This guide will help overcome the DNS issues present in WSL 2 while still making the Windows host IP autogenerated by WSL available for use. Note: this was tested on Windows 10 Build 2004, running Ubuntu 20.04 LTS in WSL 2.

    - Open a WSL terminal

    - Copy the contents of `configure-wsl-dns.sh` to `/opt/configure-wsl-dns.sh`:
    @@ -38,5 +42,3 @@
    ```

    - Exit and then go back into WSL terminal

    *Tested on Ubuntu 20.04 LTS and Windows 10 2004*
  4. djfdyuruiry revised this gist May 28, 2020. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,10 @@
    sudo /opt/configure-wsl-dns.sh

    export $(</etc/wsl-host.env)

    # The env var 'WSL_HOST' is now available which
    # contains the IP of the host machine if you need
    # it for XServer setup etc.
    ```

    - Exit and then go back into WSL terminal
  5. djfdyuruiry revised this gist May 28, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -34,3 +34,5 @@
    ```

    - Exit and then go back into WSL terminal

    *Tested on Ubuntu 20.04 LTS and Windows 10 2004*
  6. djfdyuruiry created this gist May 28, 2020.
    36 changes: 36 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    - Open a WSL terminal

    - Copy the contents of `configure-wsl-dns.sh` to `/opt/configure-wsl-dns.sh`:

    ```bash
    sudo nano /opt/configure-wsl-dns.sh
    ```

    - Make it executable:

    ```bash
    sudo chmod +x /opt/configure-wsl-dns.sh
    ```

    - Add a passwordless sudo entry in the file `/etc/sudoers.d/configure-wsl-dns`:

    ```
    %admin ALL = (root) NOPASSWD: /opt/configure-wsl-dns.sh
    ```

    - Fix an issue with `dbus`:

    ```bash
    sudo sh -c "dbus-uuidgen > /etc/machine-id"
    ```

    - Update `~/.zshrc` or `~/.bashrc`:

    ```bash
    # WSL networking
    sudo /opt/configure-wsl-dns.sh

    export $(</etc/wsl-host.env)
    ```

    - Exit and then go back into WSL terminal
    45 changes: 45 additions & 0 deletions configure-wsl-dns.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    #! /usr/bin/env bash
    set -e

    resolvConfPath="/etc/resolv.conf"
    resolvConfDirPath="/etc/resolvconf/resolv.conf.d"
    resolvHeadConfPath="${resolvConfDirPath}/head"
    wslHostEnvPath="/etc/wsl-host.env"

    function writeResolvConf() {
    cat > "$1" <<- EOM
    ## Change the below as you see fit
    # cloudflare
    nameserver 1.1.1.1
    # google
    nameserver 8.8.8.8
    # local
    nameserver 192.168.0.1
    EOM
    }

    function saveWslHostConfig() {
    rm -f "${wslHostEnvPath}"

    local wslHost=$(cat "${resolvConfPath}" | grep -v '#' | cut -d ' ' -f 2)

    printf "WSL_HOST=${wslHost}" > "${wslHostEnvPath}"
    }

    function main() {
    # exit if resolv.conf has already been patched
    grep 'generated by WSL' /etc/resolv.conf > /dev/null || exit 0

    saveWslHostConfig

    writeResolvConf "${resolvConfPath}"

    # if systemd is enabled - you will need this
    mkdir -p "${resolvConfDirPath}"
    writeResolvConf "${resolvHeadConfPath}"
    }

    main