ubuntu每次重启后都修改DNS配置文件 /etc/resolv.conf 重启就会失效的问题困扰了近一个小时,从网上得知 /etc/resolv.conf中的DNS配置是从/etc/resolvconf/resolv.conf.d/head中加载而来,所以每次修改resolv.conf后重启必然失效,同时在此文件中也有明确说明。
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)# DO NOT EDIT THIS FILE BY HAND — YOUR CHANGES WILL BE OVERWRITTEN
内容是一段警告:说这个文件是resolvconf程序动态创建的,不要直接手动编辑,修改将被覆盖。
sudo vim /etc/resolvconf/resolv.conf.d/base
nameserver 8.8.8.8
nameserver 8.8.4.4
每个DNS独立一行
修改好保存,然后执行:sudoresolvconf -u,同时重启网络服务:/etc/init.d/networking restart
sudo vim /etc/resolvconf/resolv.conf.d/head
打开里面显示如下
再看/etc/resolv.conf,最下面就多了2行:
cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 8.8.8.8
nameserver 4.4
现在DNS已经生效,无需重启,重启后不会被重写。