服务器 > 服务器 > dns服务器

使用Unbound配置DNS缓存服务器的实现步骤

76人参与 2024-05-12 dns服务器

简单介绍

dns 缓存服务器 将 dns查询结果存储在本地缓存中,并在它们的 ttl过期时从缓存中删除资源记录。

在本地网络中设置缓存名称服务器,它通过在本地缓存中存储最近访问的域名和 ip 地址的映射关系,减少跨internet的dns流量,这极大地提高了dns名称解析的效率。随着本地缓存数量增加,缓存名称服务器回答越来越多的客户端查询,dns性能将得到改善。

当用户在浏览器中输入一个网址时,浏览器会向缓存名称服务器发送一个 dns 查询请求,如果该请求的域名和 ip 地址映射已经存在于缓存中,则缓存名称服务器可以立即返回该映射关系,而无需再向 dns 服务器发送请求,从而加快了网页的加载速度。

有几个软件包可用于配置缓存名称服务器,包括bind,dnsmasq(虚拟化用的多) 和 unbound。这里使用 unbound 安装、配置和管理缓存名称服务器。

环境准备

安装 unbound

[root@servera ]# yum -y install unbound
last metadata expiration check: 0:53:24 ago on sun 19 feb 2023 02:19:20 pm cst.
installed:
  unbound-1.7.3-8.el8.x86_64

查看相关的配置文件

[root@servera ]# rpm -qlc unbound
/etc/sysconfig/unbound
/etc/unbound/conf.d/example.com.conf
/etc/unbound/keys.d/example.com.key
/etc/unbound/local.d/block-example.com.conf
/etc/unbound/unbound.conf
[root@servera ]#

编辑配置文件

编辑配置文件 /etc/unbound/unbound.conf

帮助文档查看

[root@servera ]# man unbound.conf

常见的配置信息

定义网络监听

在 server 子句中,定义网络监听,下面为不同的监听配置,监听ipv4和ipv6

interface:172.25.250.10 
interface:2001:db8:1001:f0
interface-automatic:no

网络监听配置demo

interface: 0.0.0.0
# interface: ::0
# interface: 192.0.2.153
# interface: 192.0.2.154
# interface: 192.0.2.154@5003
# interface: 2001:db8::5
#
# for dns over tls and raw dns over port 80
# interface: 0.0.0.0@443
# interface: ::0@443
# interface: 0.0.0.0@80
# interface: ::0@80
# enable this feature to copy the source address of queries to reply.
# socket options are not supported on all platforms. experimental.
# interface-automatic: yes
#
# note: enable this option when specifying interface 0.0.0.0 or ::0
# note: disabled per fedora policy not to listen to * on default install
# note: if deploying on non-default port, eg 80/443, this needs to be disabled
interface-automatic: yes

访问控制列表

在 server 子句中,定义访问控制列表。

使用access-control选项指定哪些客户端可以进行递归查询。可以指定网络或ip地址,控制类型:

示例:

# access-control: 0.0.0.0/0 refuse
# access-control: 127.0.0.0/8 allow
# access-control: ::0/0 refuse
# access-control: ::1 allow
# access-control: ::ffff:127.0.0.1 allow
access-control: 172.25.250.0/24 allow

配置访问控制, 禁止除预期客户端之外的主机使用递归缓存名称服务器 。

如果允许internet上的任何主机递归查询您的服务器,则攻击者可以使用它对第三方执行dns放大分布式拒绝服务攻击。详情参考https://blog.cloudflare.com/deep-inside-a-dns-amplification-ddos-attack/。

转发请求到其他缓冲名称服务器

转发请求到其他缓冲名称服务器:

如果此名称服务器无法访问internet,但可以访问另外一个连接internet的dns服务器。创建一个 forward-zone 子句以指定要转发的域以及将查询转发到的dns服务器。

将名称值设置为. 转发所有查询

forward-zone:
      name:"."
      forward-addr:172.25.250.254

对内部域的查询直接发送到对该域具有权威性的名称服务器。

使用forward-addr选项通过ip地址。

# forward-zone:
#       name: "example.com"
#       forward-addr: 192.0.2.68
#       forward-addr: 192.0.2.73@5355  # forward to port 5355.
#       forward-first: no
#       forward-tls-upstream: no
# forward-zone:
#       name: "example.org"
#       forward-host: fwd.example.com

domain-insecure 用于指定不需要验证 dnssec 的域名。在这个选项中 example.com 表示不需要验证 example.com 域名的 dnssec 安全性。这意味着,当 unbound 接收到来自 example.com 的 dns 响应时,它不会对响应进行 dnssec 验证。如果 example.com 的 dns 响应被篡改或伪造,那么 unbound 将不会检测到这种攻击

domain-insecure: "example.com"

证书相关生成

unbound-control-setup 在 /etc/unbound 目录下生成了 unbound_server.key 和 unbound_control.key 两个 rsa 私钥文件,并生成了 unbound_server.pem 和 unbound_control.pem 两个证书文件。

其中,unbound_server.pem 是自签名的服务器证书,unbound_control.pem 是客户端证书,由服务器证书签名。这个命令还输出了证书的签名信息和主题信息。最后,命令提示您在 unbound.conf 配置文件中启用证书以使用它们。

[root@servera ]# unbound-control-setup
setup in directory /etc/unbound
generating unbound_server.key
generating rsa private key, 3072 bit long modulus (2 primes)
..............................++++
......................................................++++
e is 65537 (0x010001)
generating unbound_control.key
generating rsa private key, 3072 bit long modulus (2 primes)
............................................++++
.............................................................++++
e is 65537 (0x010001)
create unbound_server.pem (self signed certificate)
create unbound_control.pem (signed client certificate)
signature ok
subject=cn = unbound-control
getting ca private key
setup success. certificates created. enable in unbound.conf file to use
[root@servera ]#

配置文件语法检查

[root@servera ]# unbound-checkconf
unbound-checkconf: no errors in /etc/unbound/unbound.conf
[root@servera ]#

启动dns缓存服务器

开启防火墙

[root@servera ]# firewall-cmd --add-service=dns --permanent
success
[root@servera ]# firewall-cmd --reload
success

配置开启自启动,并启动

[root@servera ]# systemctl enable unbound --now
created symlink /etc/systemd/system/multi-user.target.wants/unbound.service → /usr/lib/systemd/system/unbound.service.

测试

[root@servera ]# dig  @servera.lab.example.com workstation.lab.example.com
; <<>> dig 9.11.4-p2-redhat-9.11.4-26.p2.el8 <<>> @servera.lab.example.com workstation.lab.example.com
; (1 server found)
;; global options: +cmd
;; got answer:
;; ->>header<<- opcode: query, status: noerror, id: 24427
;; flags: qr rd ra; query: 1, answer: 1, authority: 0, additional: 1
;; opt pseudosection:
; edns: version: 0, flags:; udp: 4096
;; question section:
;workstation.lab.example.com.   in      a
;; answer section:
workstation.lab.example.com. 600 in     a       172.25.250.9
;; query time: 2 msec
;; server: 172.25.250.10#53(172.25.250.10)
;; when: sun feb 19 19:21:49 cst 2023
;; msg size  rcvd: 72
[root@servera ]#

查询的是 lab.example.com 域名的 ns 记录,使用的 dns 服务器是 servera.lab.example.com

[root@servera ]# dig  @servera.lab.example.com ns lab.example.com
; <<>> dig 9.11.4-p2-redhat-9.11.4-26.p2.el8 <<>> @servera.lab.example.com ns lab.example.com
; (1 server found)
;; global options: +cmd
;; got answer:
;; ->>header<<- opcode: query, status: noerror, id: 51183
;; flags: qr rd ra; query: 1, answer: 1, authority: 0, additional: 1
;; opt pseudosection:
; edns: version: 0, flags:; udp: 4096
;; question section:
;lab.example.com.               in      ns
;; answer section:
lab.example.com.        86400   in      ns      bastion.lab.example.com.
;; query time: 2 msec
;; server: 172.25.250.10#53(172.25.250.10)
;; when: sun feb 19 19:29:04 cst 2023
;; msg size  rcvd: 66
[root@servera ]#

查询结果显示该域名的 ns 记录为 bastion.lab.example.com。在查询结果中,

unbound dns 服务器的状态输出结果

[root@servera ]# unbound-control status
version: 1.7.3
verbosity: 1
threads: 4
modules: 3 [ ipsecmod validator iterator ]
uptime: 349 seconds
options: reuseport control(ssl)
unbound (pid 28457) is running...

unbound dns 服务器的缓存输出结果

[root@servera ]# unbound-control dump_cache
start_rrset_cache
;rrset 504 1 0 3 3
bastion.lab.example.com.        504     in      a       172.25.250.254
;rrset 86304 1 0 8 3
lab.example.com.        86304   in      ns      bastion.lab.example.com.
end_rrset_cache
start_msg_cache
msg lab.example.com. in ns 33152 1 504 3 1 0 1
lab.example.com. in ns 0
bastion.lab.example.com. in a 0
end_msg_cache
eof
[root@servera ]# unbound-control dump_cache > dns_dump
[root@servera ]#

start_rrset_cache 和 end_rrset_cache 之间的部分是存储在 unbound 缓存中的 rrset 记录,包括了 bastion.lab.example.com 域名的 a 记录和 lab.example.com 域名的 ns 记录。每个 rrset 记录包含了多个 rrsig 记录,用于验证该 rrset 记录的真实性。

start_msg_cache 和 end_msg_cache 之间的部分则是存储在 unbound 缓存中的 dns 消息记录,包括了查询 lab.example.com 域名的 ns 记录的 dns 消息记录。

[root@servera ]# dig  @servera.lab.example.com servera.lab.example.com
[root@servera ]# dig  @servera.lab.example.com serverb.lab.example.com
[root@servera ]# dig  @servera.lab.example.com serverc.lab.example.com
[root@servera ]#

查看缓存信息

[root@servera ]# unbound-control dump_cache
start_rrset_cache
;rrset 502 1 0 8 3
serverb.lab.example.com.        502     in      a       172.25.250.11
;rrset 280 1 0 3 3
bastion.lab.example.com.        280     in      a       172.25.250.254
;rrset 490 1 0 8 3
servera.lab.example.com.        490     in      a       172.25.250.10
;rrset 86080 1 0 8 3
lab.example.com.        86080   in      ns      bastion.lab.example.com.
;rrset 511 1 0 8 3
serverc.lab.example.com.        511     in      a       172.25.250.12
end_rrset_cache
start_msg_cache
msg serverb.lab.example.com. in a 33152 1 502 3 1 1 1
serverb.lab.example.com. in a 0
lab.example.com. in ns 0
bastion.lab.example.com. in a 0
msg lab.example.com. in ns 33152 1 280 3 1 0 1
lab.example.com. in ns 0
bastion.lab.example.com. in a 0
msg servera.lab.example.com. in a 33152 1 490 3 1 1 1
servera.lab.example.com. in a 0
lab.example.com. in ns 0
bastion.lab.example.com. in a 0
msg serverc.lab.example.com. in a 33152 1 511 3 1 1 1
serverc.lab.example.com. in a 0
lab.example.com. in ns 0
bastion.lab.example.com. in a 0
end_msg_cache
eof

清除缓存

[root@servera ]# unbound-control flush_zone lab.example.com.
ok removed 5 rrsets, 4 messages and 0 key entries
[root@servera ]# unbound-control dump_cache
start_rrset_cache
end_rrset_cache
start_msg_cache
end_msg_cache
eof
[root@servera ]#

缓存导出和导入

[root@servera ]# unbound-control load_cache < dns_dump
ok
[root@servera ]# unbound-control dump_cache
start_rrset_cache
;rrset 457 1 0 3 3
bastion.lab.example.com.        457     in      a       172.25.250.254
;rrset 86257 1 0 8 3
lab.example.com.        86257   in      ns      bastion.lab.example.com.
end_rrset_cache
start_msg_cache
msg lab.example.com. in ns 33152 1 457 3 1 0 1
lab.example.com. in ns 0
bastion.lab.example.com. in a 0
end_msg_cache
eof
[root@servera ]#

到此这篇关于使用unbound配置dns缓存服务器的实现步骤的文章就介绍到这了,更多相关unbound配置dns缓存服务器内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)
打赏 微信扫一扫 微信扫一扫

您想发表意见!!点此发布评论

推荐阅读

非常好用的DNS服务器(推荐)

05-12

ansible部署DNS缓存服务器的实现步骤

05-12

DNS服务器未响应的原因及解决方法

05-12

DNS信息查询工具dig用法详解

05-12

如何查看本机DNS服务器地址

05-12

DNS服务器的安装与配置步骤

05-12

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论