-
描述:修改/etc/resolv.conf,每次WSL重启之后,自己配置的DNS就会丢失,重置为默认的
根据/etc/resolv.conf中的内容可知,/etc/resolv.conf是WSL自动生成的,如果要停止自动生成,在/etc/wsl.conf添加下面的内容。
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf: # [network] # generateResolvConf = false nameserver 172.27.96.1
就像这样子:
#sudo vim /etc/wsl.conf -----------添加的内容---------- [network] generateResolvConf = false
之后重启WSL【wsl --shutdown】,再vim /etc/resolv.conf,添加:
nameserver 8.8.8.8 nameserver 114.114.114.114
恭喜这样子改完之后,但根本不能编辑/etc/resolv.conf的兄弟们。
编辑/etc/resolv.conf文件会提示下面这类信息,并且/etc/resolv.conf就算是root用户也没办法修改的。Using swap file "/var/tmp/resolv.conf.swp" "/etc/resolv.conf" [New File] Recovery completed. You should check if everything is OK. (You might want to write out this file under another name and run diff with the original file to check for changes) You may want to delete the .swp file now.
-
解决:
#vim ~/.bashrc
添加下面的内容:
echo "nameserver 114.114.114.114 nameserver 8.8.8.8" > /etc/resolv.conf
重启一下Ubuntu,就好了。
-
结果:
# vim ~/.bashrc --------重启-------- # cat /etc/resolv.conf nameserver 114.114.114.114 nameserver 8.8.8.8 # ping www.baidu.com PING www.a.shifen.com (180.101.49.11) 56(84) bytes of data. 64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=1 ttl=52 time=13.3 ms 64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=2 ttl=52 time=11.2 ms 64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=3 ttl=52 time=10.6 ms 64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=4 ttl=52 time=13.0 ms 64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=5 ttl=52 time=16.6 ms 64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=6 ttl=52 time=11.9 ms
WSL2 Ubuntu20.04 /etc/resolv.conf重启丢失
最新推荐文章于 2025-03-18 09:36:06 发布