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
: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
-
Update
~/.zshrc
or~/.bashrc
:# WSL networking 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