最近发现新装的Ubuntu 12.04 每次重启后,/etc/resolv.conf里面设置的nameserver就被清空。
这跟以前用RHT系的 情况是完全不一样的:
- root@cobbler-cli-ubuntu:/# 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
在google上搜了一下,发现这里面还真有些奥妙:
1 /etc/resolv.conf 其实是一个Link
它其实指向的是 /run/resolvconf/resolv.conf
- root@cobbler-cli-ubuntu:/# ll /etc/resolv.conf
- lrwxrwxrwx 1 root root 29 Oct 9 18:08 /etc/resolv.conf -> ../run/resolvconf/resolv.conf
2 Ubuntu 有一个 resolvconf 服务,如果重启它,那么 /etc/resolv.conf 的内容会被修改成默认的样子
所以 不能直接修改 /etc/resolv.conf
3 如果想自己定义 nameserver,那么有以下2个办法:
A 在网卡的配置文件里面加:
iface eth0 inet static
address 192.168.3.3
netmask 255.255.255.0
gateway 192.168.3.1
dns-nameservers 192.168.3.45 192.168.8.10
dns-search foo.org bar.com
B 修改 resolvconf服务的配置文件: /etc/resolvconf/resolv.conf.d/base
- echo "search test.com" >> /etc/resolvconf/resolv.conf.d/base
- echo "nameserver 202.106.0.20 " >> /etc/resolvconf/resolv.conf.d/base
- 有时候,需要添加tail文件,将DNS写到tail里边,而有时,直接写入base, 或者original即可,具体什么机制还不清楚!!
这样,在机器重启或 resolvconf 服务重启 都可以保证配置会写到/etc/resolv.conf 里面
其实 /etc/resolv.conf 里面一直都有提示,只是自己没注意看:
"DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN"
关于 resolvconf 服务更多信息,可以用man查看:man resolvconf
暂时我还没有找到能直接 刷新 /etc/resolv.conf 内容的命令,只能是重启 resolvconf 服务