WSL2 Ubuntu20.04 /etc/resolv.conf重启丢失

  • 描述:修改/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
    
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值