[Ubuntu 18.04]公共DNS设置[/etc/resolv.conf]

为什么要使用公共DNS

参考:为什么要使用 Public DNS ?

  • 速度快
  • 更稳定
  • 无劫持

国内国外都有很多的厂商提供了免费的公共DNS,比如:

  1. DNSPod Public DNS119.29.29.29
  2. 阿里云公共DNS223.5.5.5/223.6.6.6
  3. Google Public DNS8.8.8.8/8.8.4.4

系统通过读取/etc/resolv.conf中的DNS地址进行查询,默认DNS服务器IP127.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
  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ubuntu 18.04 上,可以使用 BIND(Berkeley Internet Name Domain)软件包来安装 DNS 服务器。下面是安装步骤: 1. 打开终端,更新软件包列表: ``` sudo apt update ``` 2. 安装 BIND 软件包: ``` sudo apt install bind9 ``` 3. 安装完成后,进入 BIND 配置目录: ``` cd /etc/bind/ ``` 4. 备份原始配置文件: ``` sudo cp named.conf.options named.conf.options.bak sudo cp named.conf.local named.conf.local.bak ``` 5. 编辑 named.conf.options 文件,添加以下内容: ``` options { directory "/var/cache/bind"; recursion yes; allow-recursion { any; }; forwarders { 8.8.8.8; 8.8.4.4; }; dnssec-validation auto; auth-nxdomain no; # conform to RFC1035 listen-on-v6 { any; }; }; ``` 这里的配置项含义如下: - directory:指定缓存文件的存放目录; - recursion:允许递归查询; - allow-recursion:允许任何人进行递归查询; - forwarders:指定转发器,这里使用 Google 的公共 DNS 服务器; - dnssec-validation:启用 DNSSEC 验证; - auth-nxdomain:如果查询的域名不存在,则不返回“不可知的域名”错误; - listen-on-v6:监听 IPv6 地址。 6. 编辑 named.conf.local 文件,添加以下内容: ``` zone "example.com" { type master; file "/etc/bind/db.example.com"; }; ``` 这里的配置项含义如下: - zone:指定域名; - type:指定 DNS 服务器的类型,这里是主服务器; - file:指定域名的资源记录文件。 7. 创建资源记录文件 db.example.com,添加以下内容: ``` $TTL 604800 @ IN SOA ns1.example.com. admin.example.com. ( 3 ; Serial 604800 ; Refresh 86400 ; Retry 2419200 ; Expire 604800 ) ; Negative Cache TTL ; @ IN NS ns1.example.com. @ IN A 192.168.1.10 ns1 IN A 192.168.1.10 www IN CNAME example.com. ``` 这里的配置项含义如下: - $TTL:指定缓存时间(以秒为单位); - @:表示域名本身; - SOA:Start of Authority,指定域名服务器的授权信息; - NS:Name Server,指定主 DNS 服务器; - A:Address Record,指定域名对应的 IP 地址; - CNAME:Canonical Name,指定域名的别名。 8. 重新启动 BIND 服务: ``` sudo systemctl restart bind9 ``` 至此,DNS 服务器的安装和配置就完成了。你可以使用 nslookup 命令测试域名解析是否正常: ``` nslookup example.com ``` 输出应该包含以下信息: ``` Server: 127.0.0.53 Address: 127.0.0.53#53 Name: example.com Address: 192.168.1.10 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值