When I tried git clone
in WSL2, it failed to resolve the host because the name server is not set correctly. I write down the way for the solution here. Go to the last chapter if you only want to know the solution.
So I executed the following command to set the correct IP addresses.
echo -e "\n[network]\ngenerateResolvConf = false" | sudo tee -a /etc/wsl.conf
echo -e "nameserver <name server 1>\nnameserver <name server 2>" | sudo tee /etc/resolv.conf
The configs look like this after the command above.
$ cat /etc/wsl.conf
[boot]
systemd=true
[network]
generateResolvConf = false
$ cat /etc/resolv.conf
nameserver <name server 1>
nameserver <name server 2>
It perfectly works until WSL is shut down. Whenever I rebooted WSL or the Windows machine, the setting for /etc/resolv.conf
was initialized… I needed to run the command again but it’s tedious.
When I checked the file content, it looked like the following.
$ cat /etc/resolv.conf
# This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8).
# Do not edit.
#
# This file might be symlinked as /etc/resolv.conf. If you're looking at
# /etc/resolv.conf and seeing this text, you have followed the symlink.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "resolvectl status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs should typically not access this file directly, but only
# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a
# different way, replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.
nameserver 127.0.0.53
options edns0 trust-ad
search .
I found that it could resolve the issue when executing chattr
command. However, the command was not supported for the file. By the way, chattr +i file_path
makes the file immutable.
$ sudo chattr +i /etc/resolv.conf
chattr: Operation not supported while reading flags on /etc/resolv.conf
The force option makes the command successful but it doesn’t work because the operation is not supported in the first place.
sudo chattr -f +i /etc/resolv.conf
This is because the file /etc/resolve.conf
is a symlink to /run/systemd/resolve/stub-resolv.conf
.
$ ls -la /etc/resolv.conf
lrwxrwxrwx 1 root root 39 Dec 5 12:35 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
Update resolve.conf every boot
I found this article.
This would be a nice way because the file is updated every boot. However, it might not work if you need to move the place and use a different Wifi and DNS server for whatever reason.
In this case, you need to set all possible DNS servers by hand in the next way.
Delete and Recreate the file
The original file is used as a symlink, and thus it’s impossible to make it immutable. So what we should do here is as follows.
- [WSL]: Delete the file
sudo rm /etc/resolv.conf
- [WSL]: Add name servers
echo -e "nameserver <name server 1>\nnameserver <name server 2>" | sudo tee /etc/resolv.conf
- [WSL]: Make it immutable if you want. It’s not mandatory.
sudo chattr +i /etc/resolv.conf
- [WSL]: Exit from WSL
exit
- [PowerShell]: Shutdown WSL
wsl --shutdown
- [PowerShell]: Start WSL
wsl
- [WSL]: Check the file content
cat /etc/resolv.conv
Then, the file content should have the setting that we added on step 2.
There are some cases where resolve.conf
is not used as a symlink depending on the version of Ubuntu or WSL. The file doesn’t have to be deleted in this case.
Comments