zoukankan      html  css  js  c++  java
  • 使用 unbound 在 RHEL7 上搭建 DNS 服务

    转载自:LinuxProbe http://www.linuxprobe.com/use-unbound-rhel7-setup-dns/ 作者: 陶武杰
    本文地址:https://linux.cn/article-7169-1.html
       

    本文导航

    1.概念

    DNS (),使用 TCP&UDP 的53号端口(主从 DNS 之间用 TCP,客户端查询使用 UDP)。它可以完成域名与 IP 地址的互换,可以通过 IP 地址解析到域名,也可以通过域名解析到 IP 地址。

    FQDN(),层次化树形结构。通常表现为:主机名.子域.二级域.顶级域.根域. 。例如我们平时访问的网站:"www.linuxprobe.com"就是 FQDN。

    DNS的查询方式:

    • 迭代查询:服务器与服务器之间的查询。本地域名服务器向根域名服务器的查询通常是采用迭代查询(反复查询)。当根域名服务器收到本地域名服务器的迭代查询请求报文时,要么给出所要查询的IP地址,要么告诉本地域名服务器下一步应向那个域名服务器进行查询。然后让本地域名服务器进行后续的查询;
    • 递归查询:客户端与服务器之间的查询。主机向本地域名服务器的查询一般都是采用递归查询。如果主机所询问的本地域名服务器不知道被查询域名的 IP 地址,那么本地域名服务器就以 DNS 客户的身份,向其他根域名服务器继续发出查询请求报文。最后会给客户端一个准确的返回结果,无论是成功与否。

    DNS解析类型:

    • 正向解析:由 FQDN 解析到 IP 地址;
    • 反向解析:由 IP 地址解析到 FQDN;

    名称解析方式:

    • hosts文件(etc/hosts)
    • dns
    • 广播
    • 解析缓存
    • wins(windows 中)等

    2.DNS 安装配置

    在 RHEL5、6 中 DNS 都是用的是 bind 软件包,而在 RHEL/CentOS 7 用的是 unbound 安装包,配置文件也有了改变。我们来看一下:

    2.1.安装:

    1. [root@linuxprobe ~]# yum -y install unbound
    2. Loaded plugins: langpacks, product-id, subscription-manager
    3. This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
    4. Resolving Dependencies
    5. ---> Running transaction check
    6. ---> Package unbound.x86_64 0:1.4.20-19.el7 will be installed
    7. ---> Finished Dependency Resolution
    8. ·····
    9. ---------------------------启动服务-----------------------------
    10. [root@linuxprobe ~]# systemctl restart unbound //启动DNS服务
    11. [root@linuxprobe ~]# systemctl enable unbound
    12. ln -s '/usr/lib/systemd/system/unbound.service' '/etc/systemd/system/multi-user.target.wants/unbound.service'
    13. //下次系统重启自动启动DNS服务

    2.2.修改配置文件

    unbound 安装好之后,缺省配置文件在 /etc/unbound/unbound.conf。

    2.2.1.修改端口监听地址

    相当于 RHEL6 配置文件中的:listen-on port 53 { any; };

    1. -----------------------查看默认监听地址--------------------------
    2. [root@linuxprobe ~]# netstat -tunlp |grep unbound
    3. tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN 3333/unbound
    4. tcp 0 0 127.0.0.1:8953 0.0.0.0:* LISTEN 3333/unbound
    5. tcp6 0 0 ::1:53 :::* LISTEN 3333/unbound
    6. tcp6 0 0 ::1:8953 :::* LISTEN 3333/unbound
    7. udp 0 0 127.0.0.1:53 0.0.0.0:* 3333/unbound
    8. udp6 0 0 ::1:53 :::* 3333/unbound
    9. //默认监听本地回环地址,也就是现在只有自己能访问DNS服务,其它主机不能访问本机的DNS服务
    10. -------------------------修改监听地址----------------------------
    11. [root@linuxprobe ~]# vim /etc/unbound/unbound.conf
    12. ……
    13. 38 # interface: 0.0.0.0
    14. 39 interface: 0.0.0.0
    15. ……
    16. //找到38行,复制去掉注释行,打开监听全网功能。
    17. --------------------------重启服务查看--------------------------------
    18. [root@linuxprobe ~]# systemctl restart unbound
    19. [root@linuxprobe ~]# netstat -tunlp |grep unbound
    20. tcp 0 0 0.0.0.0:53 0.0.0.0:* LISTEN 3461/unbound
    21. tcp 0 0 127.0.0.1:8953 0.0.0.0:* LISTEN 3461/unbound
    22. tcp6 0 0 ::1:8953 :::* LISTEN 3461/unbound
    23. udp 0 0 0.0.0.0:53 0.0.0.0:* 3461/unbound
    24. //现在53号端口监听的是0.0.0.0,即所有网段都监听。

    2.2.2.修改允许查询的范围

    在 RHEL6 中,DNS 配置文件中有这样一句:allow-query { localhost; };。此句定义的是允许向本机查询(迭代 & 递归)的主机范围,localhost 代表只有本机可以向本机查询。而在配置中,经常改 localhost 为 any,让所有主机能够向本机查询 DNS。所以,在 RHEL7 中,也要做这样的修改,只不过修改内容不同而已,如下:

    1. [root@linuxprobe ~]# vim /etc/unbound/unbound.conf
    2. ……
    3. 177 # access-control: 0.0.0.0/0 refuse
    4. 178 access-control: 0.0.0.0/0 allow
    5. 179 # access-control: 127.0.0.0/8 allow
    6. ……
    7. 找到配置文件/etc/unbound/unbound.conf的第177行,缺省为注释行,把内容改为允许访问,然后保存退出,重启服务即可。

    2.2.3.创建解析文件

    RHEL/CentOS 5、6系统中,DNS 的解析文件分正向和反向两个解析文件,并且有解析文件的模板文件。但是在 RHEL7中,正反向解析文件合并为一个,并且无模板文件,需自己创建,路径可以在主配置文件中查看:

    1. [root@linuxprobe ~]# vim /etc/unbound/unbound.conf
    2. ……
    3. 453 # You can add locally served data with
    4. 454 # local-zone: "local." static
    5. 455 # local-data: "mycomputer.local. IN A 192.0.2.51"
    6. //正向解析可参考语法
    7. 456 # local-data: 'mytext.local TXT "content of text record"'
    8. 457 #
    9. 458 # You can override certain queries with
    10. 459 # local-data: "adserver.example.com A 127.0.0.1"
    11. 460 #
    12. 461 # You can redirect a domain to a fixed address with
    13. 462 # (this makes example.com, www.example.com, etc, all go to 192.0.2.3)
    14. 463 # local-zone: "example.com" redirect
    15. 464 # local-data: "example.com A 192.0.2.3"
    16. 465 #
    17. # Shorthand to make PTR records, "IPv4 name" or "IPv6 name".
    18. 467 # You can also add PTR records using local-data directly, but then
    19. 468 # you need to do the reverse notation yourself.
    20. 469 # local-data-ptr: "192.0.2.3 www.example.com"
    21. //反向解析参考语法
    22. 470
    23. 471 include: /etc/unbound/local.d/*.conf
    24. 472
    25. 473 # service clients over SSL (on the TCP sockets), with plain DNS inside
    26. ……
    27.  
    28. ---------------------------------查看本机FQDN---------------------------
    29. [root@linuxprobe ~]# hostname
    30. linuxprobe.example.com
    31. //由此可知,域名为example.com
    32. --------------------------------创建解析文件-----------------------------
    33. [root@linuxprobe ~]# vim /etc/unbound/local.d/example.conf
    34. local-zone: "example.com." static
    35. local-data: "example.com. 86400 IN SOA ns.example.com. root 1 1D 1H 1W 1H"
    36. local-data: "ns.example.com. IN A 192.168.10.10"
    37. local-data: "linuxprobe.example.com. IN A 192.168.10.10"
    38. local-data-ptr: "192.168.10.10 ns.example.com."
    39. local-data-ptr: "192.168.10.10 linuxprobe.example.com."
    40. ------------------------查看RHEL6上解析文件以作对比--------------------
    41. [root@linuxprobe ~]# vim /var/named/named.localhost
    42. $TTL 1D
    43. @ IN SOA @ rname.invalid. (
    44. 0 ; serial
    45. 1D ; refresh
    46. 1H ; retry
    47. 1W ; expire
    48. 3H ) ; minimum
    49. NS @
    50. A 127.0.0.1
    51. AAAA ::1

    2.3.禁用服务用户

    每个服务都是有其专用的服务用户,DNS 的服务用户为 unbound,实际情况下服务用户的启用有可能有安全隐患,这里要禁用服务用户。

    1. [root@linuxprobe ~]# vim /etc/unbound/unbound.conf
    2. ······
    3. 211 # if given, user privileges are dropped (after binding port),
    4. 212 # and the given username is assumed. Default is user "unbound".
    5. 213 # If you give "" no privileges are dropped.
    6. 214 #username: "unbound"
    7. 215 username: " "
    8. 216
    9. 217 # the working directory. The relative files in this config
    10. ······
    11. 如上,找到配置文件的第214行,删除unbound即可,删除后为:username " "。

    2.4.验证

    1. [root@linuxprobe ~]# unbound-checkconf
    2. unbound-checkconf: no errors in /etc/unbound/unbound.conf
    3. 验证无配置问题,即可重启服务
    4.  
    5. [root@linuxprobe ~]# systemctl restart unbound
    6.  
    7. dns验证:
    8.  
    9. -------------------------修改本机DNS------------------------
    10. [root@linuxprobe ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
    11. HWADDR=00:0C:29:70:····
    12. TYPE=Ethernet
    13. ····
    14. IPADDR="192.168.10.10"
    15. PREFIX="24"
    16. ···
    17. DNS1=192.168.10.10
    18. NAME=eth0
    19. ONBOOT=no
    20. [root@linuxprobe ~]# systemctl restart network
    21.  
    22. ----------------------------------------------------nslookup验证--------------------------------------------
    23. [root@linuxprobe ~]# nslookup
    24. linuxprobe.example.com.
    25. 192.168.10.10
    26.  
    27. ok dns设置成功

    PS:关闭防火墙

    在本次实验中我们关闭了 linux 的3大防火墙。当没有关闭防火墙时,远程主机验证可能出现故障,这时需要在 DNS 服务器防火墙上开放 DNS 服务。我们以 firewall 防火墙为例,修改一下:

    1. [root@linuxprobe ~]# systemctl stop iptables
    2. [root@linuxprobe ~]# systemctl stop ebtables
    3. [root@linuxprobe ~]# systemctl disable iptables
    4. [root@linuxprobe ~]# systemctl disable ebtables
    5. [root@linuxprobe ~]# firewall-cmd --add-service=dns --permanent
    6. success
    7. [root@linuxprobe ~]# firewall-cmd --reload
    8. success
    9. [root@linuxprobe ~]# firewall-cmd --list-all
    10. public (default, active)
    11. interfaces: eth0
    12. sources:
    13. services: dhcpv6-client dns ssh
    14. ports:
    15. masquerade: no
    16. forward-ports:
    17. icmp-blocks:
    18. rich rules:
    19. //DNS服务器上Firewall开放DNS访问ok

    转载自:LinuxProbe http://www.linuxprobe.com/use-unbound-rhel7-setup-dns/ 作者: 陶武杰

     

  • 相关阅读:
    Context都没弄明白,还怎么做Android开发?
    Android中Drawable分类汇总
    查找首个非重复字符
    七个对我最好的职业建议(译文)
    Android:最全面的 Webview 详解
    Android开发之微信底部菜单栏实现的几种方法汇总
    android 底部菜单栏实现(转)
    Android实现顶部底部双导航界面功能
    Android BottomNavigationBar底部导航控制器的使用
    Android底部导航栏的四种实现
  • 原文地址:https://www.cnblogs.com/jonathanyue/p/9301258.html
Copyright © 2011-2022 走看看