Ubuntu怎么修改DNS

有时候会出现配置好网络之后,可以ping通网关却ping不通www.baidu.com

orangepi@orangepi3:~$ ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=94.1 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=6.14 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=6.02 ms
^C
--- 192.168.1.1 ping statistics ---
4 packets transmitted, 3 received, 25% packet loss, time 3025ms
rtt min/avg/max/mdev = 6.026/35.455/94.192/41.533 ms
orangepi@orangepi3:~$ ping www.baidu.com
ping: www.baidu.com: Name or service not known

出现上面所显示的情况,多数原因是因为没有正确的配置DNS(域名解析服务)。

解决方法

修改文件/etc/resolv.conf

sudo vim /etc/resolv.conf

修改为

# Generated by NetworkManager
nameserver 8.8.8.8
nameserver 114.114.114.114

执行sudo resolvconf -u更新resolv.conf文件。
如果有更多的NDS,可以继续添加nameserver x.x.x.x,不出意外,修改完过一会儿,应该就能成功ping通。

orangepi@orangepi3:~$ ping www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=56 time=8.49 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=3 ttl=56 time=9.15 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=4 ttl=56 time=13.1 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=5 ttl=56 time=7.44 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=6 ttl=56 time=12.7 ms
^C
--- www.a.shifen.com ping statistics ---
6 packets transmitted, 5 received, 16% packet loss, time 331074581ms
rtt min/avg/max/mdev = 7.440/10.198/13.169/2.317 ms

但是这个方式重启之后就会失去效果,想要永久生效,可以采用第二个

修改文件/etc/resolvconf/resolv.conf.d/base

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

这里我修改的是/etc/resolvconf/resolv.conf.d/head文件,我发现效果是一样的,因为我修改base没有生效。

增加DNS

nameserver 8.8.8.8
nameserver 114.114.114.114

执行sudo resolvconf -u更新resolv.conf文件,如果执行命令出现问题:

sudo rm /etc/resolv.conf
sudo ln -s ../run/resolvconf/resolv.conf /etc/resolv.conf
sudo resolvconf -u

然后就和上面效果效果相同了,之后重启也能生效了。

posted @ 2021-01-12 10:59  hokori  阅读(758)  评论(0编辑  收藏  举报