ubuntu 命令行设置dns_Ubuntu修改DNS配置(示例代码)

不要直接手动修改文件 /etc/resolv.conf

安装好Ubuntu之后设置了静态IP地址,再重启后就无法解析域名。想重新设置一下DNS,打开/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程序动态创建的,不要直接手动编辑,修改将被覆盖。

[email protected]:~#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 100.100.2.138nameserver100.100.2.136options timeout:2 attempts:3 rotate single-request-reopen

root@Ubuntu14:~#

ubuntu修改DNS有如下两种方法。

方法一:修改文件/etc/resolvconf/resolv.conf.d/base

[email protected]:~#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 100.100.2.138nameserver100.100.2.136options timeout:2 attempts:3 rotate single-request-reopen

root@Ubuntu14:~#vim /etc/resolvconf/resolv.conf.d/base

[email protected]:~#cat !$

cat /etc/resolvconf/resolv.conf.d/base

nameserver223.5.5.5nameserver223.6.6.6root@Ubuntu14:~#resolvconf -u

[email protected]:~#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 100.100.2.138nameserver100.100.2.136nameserver223.5.5.5options timeout:2 attempts:3 rotate single-request-reopen

root@Ubuntu14:~#nslookup www.baidu.com

Server: 100.100.2.138Address:100.100.2.138#53

Non-authoritative answer:

www.baidu.com canonical name=www.a.shifen.com.

Name: www.a.shifen.com

Address:220.181.112.244Name: www.a.shifen.com

Address:220.181.111.188root@Ubuntu14:~#

方法二:修改文件 /etc/network/interdaces

[email protected]:~#cat /etc/network/interfaces

auto lo

iface lo inet loopback

auto eth0

iface eth0 inet dhcp

dns-nameservers 223.5.5.5 # 在最后添加这一行

常用命令

linux刷新dns的缓存方法是:  sudo /etc/init.d/nscd restart

最暴力的方法刷dns,重启网络:  sudo /etc/init.d/networking restart

查看当前DNS解析情况:

nslookup www.baidu.com

dig | grep SERVER

[email protected]:~#/etc/init.d/nscd restart

*Restarting Name Service Cache Daemon nscd [ OK ]

root@iZbp1b66g5htibj2m7yyv3Z:~#/etc/init.d/networking restart

stop: Job failed whilestopping

start: Job is already running: networking

root@iZbp1b66g5htibj2m7yyv3Z:~#dig |grep SERVER

;; SERVER: 100.100.2.138#53(100.100.2.138)

[email protected]:~#nslookup www.baidu.com

Server: 100.100.2.138Address:100.100.2.138#53

Non-authoritative answer:

www.baidu.com canonical name=www.a.shifen.com.

Name: www.a.shifen.com

Address:220.181.111.188Name: www.a.shifen.com

Address:220.181.112.244root@iZbp1b66g5htibj2m7yyv3Z:~#

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值