There are tons of gists available for this problem:
However even solution is there - I was not satisfied that this problem always persists and I had to do same steps again from time to time. Also using a static DNS like 8.8.8.8 was not the option for me because we use pool of enterprise DNS servers and those are controlled by our IT on our Windows machines. So I wanted to find the way to get those DNS servers dynamically from Windows.
This guide describes the permanent solution using brand-new WSL boot command.
This gist removes nameservers every time your WSL starts and adds newly from powershell results. If it doesn't work for you - please feel free to adjust the script according to your needs.
- login to your wsl and run
sudo bash
. It's important that you do this with a root user because it's a user which executes this command during boot - change to ~ folder ( or use any folder you like )
- create a new file for example
boot.sh
and update it with a content from boot.sh gist - make it executable with
chmod +x boot.sh
- create a symbolic link in one of PATH folders ( keep in mind, that for root user number of path folders is very limited by default )
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
- i have used
ln -s ~/boot.sh /usr/local/bin/boot.sh
- having such a limited list of folders makes root not aware of powershell.exe and rest of window utilities. I have preferred to use an absolute path like
/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe
in my script - but you may think of creating a symlink for powershell too. - now let's add boot command to
/etc/wsl.conf
file. In my case it looks like this:
[network]
generateResolvConf=false
[boot]
systemd=true
command=boot.sh
- now just
exit
from bash, from wsl as well. Terminate your wsl with a command likewsl -t Ubuntu
and start again - if everything went well
cat /etc/resolv.conf
should have nameservers from your Windows
Sometimes an interface can have a DNS value on it, but not be "Up", this takes care of that (User used as username for reference to next step update):
file (Windows): C:\Users\User\bootDNS.ps1
then update as such (fix your "User" reference for the -File path, and update example.com to your environment):
file (WSL): /root/boot.sh