-
Open a WSL terminal
-
Copy the contents of
configure-wsl-dns.sh
to/opt/configure-wsl-dns.sh
:sudo nano /opt/configure-wsl-dns.sh
-
Make it executable:
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
:sudo sh -c "dbus-uuidgen > /etc/machine-id"
-
Update
~/.zshrc
or~/.bashrc
:# WSL networking sudo /opt/configure-wsl-dns.sh export $(</etc/wsl-host.env)
-
Exit and then go back into WSL terminal
Last active
February 27, 2025 10:21
-
-
Save djfdyuruiry/8ec94d6454fbacccafb092022fe041a3 to your computer and use it in GitHub Desktop.
WSL 2 - Fix DNS Issues
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment