问题描述#
我在 WSL 中执行 ping google.com
时失败,并提示:
Temporary failure in name resolution
解决方法#
在 WSL2 中创建或修改
/etc/wsl.conf
文件添加以下内容,以防止 DNS 修改被系统覆盖:
sudo tee /etc/wsl.conf << EOF [network] generateResolvConf = false EOF
在 Windows CMD 中运行:
wsl --shutdown
重启 WSL2
在 WSL2 中执行以下命令(
search
行可选):sudo rm -rf /etc/resolv.conf sudo tee /etc/resolv.conf << EOF search yourbase.domain.local nameserver 8.8.8.8 nameserver 1.1.1.1 EOF
如果删除
/etc/resolv.conf
时出现:rm: cannot remove '/etc/resolv.conf': Operation not permitted
则先运行以下命令解锁文件:
sudo chattr -a -i /etc/resolv.conf
参考链接#
https://askubuntu.com/questions/1192347/temporary-failure-in-name-resolution-on-wsl
https://askubuntu.com/questions/125847/un-removable-etc-resolv-conf
转载本文请注明作者与出处,禁止商业用途。欢迎关注公众号「DevOps攻城狮」。