彻底解决 Ubuntu 14.04 重启后 DNS 配置丢失的问题

Posted by Azrael on September 30, 2016

Ubuntu系统下,添加到DNS配置文件 /etc/resolv.conf 中的 DNS 地址,重启就会消失!

从网上得知 /etc/resolv.conf 中的DNS配置是从 /etc/resolvconf/resolv.conf.d/head 中加载而来,所以每回改 resolv.conf 重启都会失效,在此文件里面已经有说明。

打开/etc/resolv.conf内容如下:

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

内容是一段警告:说这个文件是resolvconf程序动态创建的,不要直接手动编辑,修改将被覆盖。

所以我们不能直接修改这个文件了

查资料得到两种添加 DNS 地址的方法:

方法1:修改 /etc/network/interfaces

sudo vim /etc/network/interfaces

dns-nameservers 8.8.8.8

方法2:修改 /etc/resolvconf/resolv.conf.d/base(这个文件默认是空的)

sudo vim /etc/resolvconf/resolv.conf.d/base

nameserver 8.8.8.8
nameserver 8.8.4.4

如果有多个DNS就一行一个

修改好保存,然后执行

resolvconf -u

再看/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 8.8.4.4

现在此DNS已经生效了,无需重启,当然重启也不会丢失。

此外还有一种方法是修改/etc/resolvconf/resolv.conf.d/head

sudo vim /etc/resolvconf/resolv.conf.d/head

# 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

这个文件里提示不要手动修改。。。

按照系统提示的说法,不建议改此文件