ubuntu 怎么设置静态ip及dns

首先不推荐修改/etc/resolv.conf或者修改/etc/resolvconf/resolv.conf.d/目录下的head,base文件,添加tail文件的方法也不赞成。

因为resolv.conf和head文件中都有提醒文字:

[plain]  view plain copy print ?
  1. cat /etc/resolv.conf   
  2. # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)  
  3. #     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN  

[plain]  view plain copy print ?
  1. cat /etc/resolvconf/resolv.conf.d/head  
  2. # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)  
  3. #     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN  

不要对这个视若无睹。以为即便你当时改对了,网络服务一重启,又不行了。


用man resolvconf命令查看文档,可以看到一段提醒注意的文字:

[plain]  view plain copy print ?
  1. N.B.: On a machine where resolvconf has just been or is about to be installed and which previously relied on a static /etc/resolv.conf file, the nameserver  informa‐  
  2.        tion in that static file should be migrated to the appropriate iface stanza(s) in interfaces(5).  
证明了的确不推荐修改上面的文件,而建议修改/etc/network/interfaces文件。下面是个正确的例子:

[plain]  view plain copy print ?
  1. # This file describes the network interfaces available on your system  
  2. # and how to activate them. For more information, see interfaces(5).  
  3.   
  4. # The loopback network interface  
  5. auto lo  
  6. iface lo inet loopback  
  7.   
  8. # The primary network interface  
  9. auto eth0  
  10. iface eth0 inet static  
  11.       address 10.112.18.106  
  12.       network 10.112.18.0  
  13.       netmask 255.255.255.0  
  14.       broadcast 10.112.18.255  
  15.       gateway 10.112.18.254  
  16.       dns-nameservers 10.112.18.1  

就是最后一行dns-nameservers,可以添加多个,用空格分开


上面我用的是静态IP地址,很多时候需要使用DHCP动态分配IP, 这个时候dns-nameservers仍然能够使用,不过优先级比DHCP提供的dns要低。可以改变这个优先级,通过修改/etc/resolvconf/interface-order配置文件。


现在用nslookup来查找dns,一切正常:

[plain]  view plain copy print ?
  1. nslookup google.com  
  2. Server:     10.112.18.1  
  3. Address:    10.112.18.1#53  
  4.   
  5. Non-authoritative answer:  
  6. Name:   google.com  
  7. Address: 74.125.224.229  
  8. Name:   google.com  
  9. Address: 74.125.224.224  
  10. Name:   google.com  
  11. Address: 74.125.224.238  
  12. Name:   google.com  
  13. Address: 74.125.224.230  
  14. Name:   google.com  
  15. Address: 74.125.224.232  
  16. Name:   google.com  
  17. Address: 74.125.224.227  
  18. Name:   google.com  
  19. Address: 74.125.224.233  
  20. Name:   google.com  
  21. Address: 74.125.224.231  
  22. Name:   google.com  
  23. Address: 74.125.224.225  
  24. Name:   google.com  
  25. Address: 74.125.224.226  
  26. Name:   google.com  
  27. Address: 74.125.224.228  

但是,我的一台服务器用上面的方法仍然有故障。

用下面的命令没有问题,interfaces文件中的dns配置会被自动复制到/etc/resolv.conf文件中

[plain]  view plain copy print ?
  1. ifdown eth0; ifup eth0  

但是如果用service networking restart就不行了。

解决方法是:

1.确定安装了resolvconf

[plain]  view plain copy print ?
  1. apt-get install resolvconf  
2. 重新配置resolvconf

[plain]  view plain copy print ?
  1. dpkg-reconfigure resolvconf  
确认框中都选择yes.

3.然后重新启动,问题解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值