为什么要使用公共DNS
- 速度快
- 更稳定
- 无劫持
国内国外都有很多的厂商提供了免费的公共DNS
,比如:
- DNSPod Public DNS:
119.29.29.29
- 阿里云公共DNS:
223.5.5.5/223.6.6.6
- Google Public DNS:
8.8.8.8/8.8.4.4
系统通过读取/etc/resolv.conf
中的DNS
地址进行查询,默认DNS
服务器IP
是127.0.0.53
# 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
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "systemd-resolve --status" to see details about the actual nameservers.
nameserver 127.0.0.53
该文件是一个软链接,其指向/run/systemd/resolve/resolv.conf
如何设置公共DNS¶
由于/etc/resolv.conf
会被重写,所以关键问题在于如何在/etc/resolv.conf
文件中添加新的DNS
服务器地址
第一步:调整/etc/resolv.conf
的软链接,使其指向文件/run/systemd/resolve/resolv.conf
$ cd /etc
$ rm resolv.conf
$ ln -s /run/systemd/resolve/resolv.conf resolv.conf
第二步:修改配置文件/etc/systemd/resolved.conf
,添加新的DNS
服务器IP
$ cat resolved.conf
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See resolved.conf(5) for details
[Resolve]
DNS=119.29.29.29 223.5.5.5 223.6.6.6
#DNS=
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes
第三步:重启系统
完成后,可以查看新的/etc/resolv.conf
配置
$ cat /etc/resolv.conf
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients directly to
# all known uplink DNS servers. This file lists all configured search domains.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.
nameserver 119.29.29.29
nameserver 223.5.5.5
nameserver 223.6.6.6
# Too many DNS servers configured, the following entries may be ignored.
nameserver 113.214.230.25
nameserver 113.215.2.222
使用命令systemd-resolve --status
查询
$ systemd-resolve --status
Global
DNS Servers: 119.29.29.29
223.5.5.5
223.6.6.6
...
...
使用命令nslookup
解析网址:
$ nslookup www.baidu.com
Server: 119.29.29.29
Address: 119.29.29.29#53
Non-authoritative answer:
www.baidu.com canonical name = www.a.shifen.com.
Name: www.a.shifen.com
Address: 182.61.200.7
Name: www.a.shifen.com
Address: 182.61.200.6