WSL问题:Temporary failure resolving ''archive.ubuntu.com''

今天在WSL进行操作的时候,发现从网络上下载东西无法成功,包括wget 、apt-get 等命令都无法正常使用,比如执行sudo apt-get update 的时候会提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Err:1 http://security.ubuntu.com/ubuntu bionic-security InRelease
Temporary failure resolving 'security.ubuntu.com'
Err:2 http://archive.ubuntu.com/ubuntu bionic InRelease
Temporary failure resolving 'archive.ubuntu.com'
Err:3 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
Temporary failure resolving 'archive.ubuntu.com'
Err:4 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
Temporary failure resolving 'archive.ubuntu.com'
Reading package lists... Done
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic-updates/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic-backports/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/bionic-security/InRelease Temporary failure resolving 'security.ubuntu.com'
W: Some index files failed to download. They have been ignored, or old ones used instead.

经过查询和搜索(解决bug真的痛苦😫),发现通过一下几行命令可以解决

1
2
3
4
5
sudo rm /etc/resolv.conf
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'
sudo bash -c 'echo "[network]" > /etc/wsl.conf'
sudo bash -c 'echo "generateResolvConf = false" >> /etc/wsl.conf'
sudo chattr +i /etc/resolv.conf

接下来下载东西就都没有问题了。

2021.11.20 补充:今天又碰到这个问题了,这次是ssh的时候无法解析hostname,

又get到了一种新的解决办法:

直接在/etc/resolv.conf中加入dns服务器地址

1
2
nameserver 202.96.134.133
nameserver 8.8.8.8

然后重新启动wsl就可以了

参考资料:

Github上的一个issue

ubuntu apt-get 错误 Temporary failure resolving 'us.archive.ubuntu.com' 解决