翼度科技»论坛 云主机 LINUX 查看内容

【DNS】域名服务 Bind实现

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
一、域名解析过程


 

 DNS域名完整解析过程
1、查询本地 hosts文件 解析记录
2、查询客户端本地DNS缓存记录
3、访问DNS转发(缓存)服务器本地缓存记录
4、转发到权威服务器查询本地缓存记录
5、访问权威服务器解析记录
6、权威服务器迭代查询
  6.1、访问子域权威服务器查询本地缓存记录
  6.2、访问子域解析记录
7、访问根服务器解析记录
8、访问一级域名服务器解析记录
.......
二、DNS解析记录类型

 1、DNS服务器类型

主DNS服务器

管理和维护所负责解析的域内解析库的服务器
从DNS服务器

从主服务器或从服务器复制(区域传输)解析库副本
缓存DNS服务器(转发器)

将客户端请求转发到指定的DNS服务器上,并将指定DNS服务器返回结果缓存到本地DNS缓存记录中,缓存DNS服务器自身不保存解析库数据,不使用自身进行域名解析。
 2、解析结果类型



  • 肯定答案:存在对应的查询结果
  • 否定答案:请求的条目不存在等原因导致无法返回结果
  • 权威答案:直接由存在此查询结果的DNS服务器(权威服务器)返回的结果
  • 非权威答案:有其他非权威服务器返回的查询结果
 3、资源记录RR(Resource Record)

区域解析库
  有众多资源记录RR(Resource Record)组成
记录类型:SOA、A、AAAA、NS、CNAME、MX、TXT、PTR

  • SOA:Start Of Authority,起始授权记录;一个区域解析库有且仅能有一个SOA记录,且必须位于解析库的第一条
  • A:IPv4 正向解析资源记录
  • AAAA:IPv6正向解析资源记录
  • NS:用于标注当前区域的DNS服务器
  • CNAME:别名记录
  • MX:邮件交换器
  • TXT:对域名进行标识说明的一种方式,一般做验证记录会使用此项,如SPF(反垃圾邮件)记录,https验证 登
  • PTR:
3.1、资源记录定义格式
  1. name    [TTL]    IN      rr_type      value
复制代码
使用@符号可引用当前区域名字
TTL 可以从全局继承
IN值可以通过继承上一条记录忽略不写
同一个名字可以通过多条记录定义多个值,此时DNS服务器会以轮询方式响应
同一个值也可能有多个不同定义的名字,通过多个名字指向同一个值进行定义,此时表示通过多个不同的名字找到同一台主机
 
3.2、SOA 记录

name:当前区域的名字
value:多个内容组成
  当前主DNS服务器的FQDN,也可以使用当前区域的名字
  当前区域的管理员邮箱,由于无法使用@符合,通常使用.替换,例如:root.janzen.com (root@janzen.com)
  主从服务区域传输相关定义以及否定答案的统一TTL设置
  1. @       IN      SOA     dns1.janzen.com  root.janzen.com (
  2.                          1          ; 序列号
  3.                          604800      ; 刷新时间
  4.                          86400       ; 重试时间
  5.                          2419200     ; 过期时间
  6.                          604800 )    ; 否定答案的TTL值
复制代码
 
3.3、A 记录

name:主机的FQDN,可以.结尾代表完整名称,也可以简写末尾不加.
value:对应的IPv4地址
  1. dns1            A       10.0.0.20
  2. dns2            A       10.0.0.21
  3. gitlab.janzen.com.          A       10.0.0.13
  4. harbor          A       10.0.0.9
  5. harbor          A       10.0.0.10
复制代码
 
3.4、AAAA 记录

name:主机的FQDN,可以.结尾代表完整名称,也可以简写末尾不加.
value:对应的IPv6地址
 
3.5、NS 记录

name:当前区域的名字
value:当前区域某DNS服务器的名字
  相邻的两个资源记录name相同时,后续的可以省略
  对于NS记录而言,每一条NS记录后面的名字,后续都应该有一条对应的A记录
  一个区域可以有多条NS记录
  1. @       IN      NS      dns1
  2.                 NS      dns2
复制代码
 
3.6、CNAME 记录

name:别名FQDN
value:真实的FQDN
  1. dns     IN      CNAME   dns1
复制代码
 
3.7、MX 记录

name:当前区域的名字
value:当前区域某邮件服务器(smtp服务器)的名字
  一个区域内,MX记录可以有多个,但每个记录的value后面都应该有一个(0-99)数字,表示此服务器的优先级
  对于MX记录而言,每一条NS记录后面的名字,后续都应该有一条对应的A记录
  1. @     IN      MX  12  mail1<br>    IN      MX  10  mail2<br>mail1  IN  A  10.0.0.31<br>mail2  IN  A  10.0.0.32
复制代码
 
3.8、TXT 记录

name:文本描述头
value:文本内容
  1. _dnstxt        TXT     this is @ name server
复制代码
 
 
3.9、PTR 记录

name:IP
value:FQDN
  name的IP拥有固定写法,需要将IP反向书写,并添加特殊后缀 in-addr.arpa.
  完整写法为:20.0.0.10.in-addr.arpa.
  网络地址及后缀可以省略,主机地址依旧要反写
  1. 20.0.0.10.in-addr.arpa.    IN    PTR    dns.janzen.com.
  2. #由于 10.0.0 为网络地址,可以省略
  3. 9    IN    PTR    harbor.janzen.com.
复制代码
 
三、DNS工具介绍 

dig 工具介绍

用于测试DNS解析结果
  1. Usage:  dig [@global-server] [domain] [q-type] [q-class] {q-opt}
  2. Where:  domain      is in the Domain Name System
  3.         q-class  is one of (in,hs,ch,...) [default: in]
  4.         q-type   is one of (a,any,mx,ns,soa,hinfo,axfr,txt,...) [default:a]
  5.                  (Use ixfr=version for type ixfr)<br>     q-opt  +[no]trace (Trace delegation down from root [+dnssec]) <br>          +[no]recurse (Recursive mode (+[no]rdflag))
复制代码
  1. #获取目标dns全部解析记录
  2. dig -tAXFR janzen.com @10.0.0.21
  3. #跟踪域名解析路径
  4. dig +trace app3.janzen.com @10.0.0.21
  5. #查询PTR记录
  6. dig -x 10.0.0.21
  7. #直接显示域名查询结果
  8. dig +short app3.janzen.com @10.0.0.71
复制代码
nslookup工具
  1. nslookup [-option] [name | -] [server]
复制代码
 
rndc DNS管理工具 
  1. Usage: rndc [-b address] [-c config] [-s server] [-p port]
  2.     [-k key-file ] [-y key] [-r] [-V] command
  3. command is one of the following:
  4.   addzone zone [class [view]] { zone-options }
  5.         Add zone to given view. Requires allow-new-zones option.
  6.   delzone [-clean] zone [class [view]]
  7.         Removes zone from given view.
  8.   dnstap -reopen
  9.         Close, truncate and re-open the DNSTAP output file.
  10.   dnstap -roll count
  11.         Close, rename and re-open the DNSTAP output file(s).
  12.   dumpdb [-all|-cache|-zones|-adb|-bad|-fail] [view ...]
  13.         Dump cache(s) to the dump file (named_dump.db).
  14.   flush     Flushes all of the server's caches.
  15.   flush [view]    Flushes the server's cache for a view.
  16.   flushname name [view]
  17.         Flush the given name from the server's cache(s)
  18.   flushtree name [view]
  19.         Flush all names under the given name from the server's cache(s)
  20.   freeze    Suspend updates to all dynamic zones.
  21.   freeze zone [class [view]]
  22.         Suspend updates to a dynamic zone.
  23.   halt        Stop the server without saving pending updates.
  24.   halt -p    Stop the server without saving pending updates reporting
  25.         process id.
  26.   loadkeys zone [class [view]]
  27.         Update keys without signing immediately.
  28.   managed-keys refresh [class [view]]
  29.         Check trust anchor for RFC 5011 key changes
  30.   managed-keys status [class [view]]
  31.         Display RFC 5011 managed keys information
  32.   managed-keys sync [class [view]]
  33.         Write RFC 5011 managed keys to disk
  34.   modzone zone [class [view]] { zone-options }
  35.         Modify a zone's configuration.
  36.         Requires allow-new-zones option.
  37.   notify zone [class [view]]
  38.         Resend NOTIFY messages for the zone.
  39.   notrace    Set debugging level to 0.
  40.   nta -dump
  41.         List all negative trust anchors.
  42.   nta [-lifetime duration] [-force] domain [view]
  43.         Set a negative trust anchor, disabling DNSSEC validation
  44.         for the given domain.
  45.         Using -lifetime specifies the duration of the NTA, up
  46.         to one week.
  47.         Using -force prevents the NTA from expiring before its
  48.         full lifetime, even if the domain can validate sooner.
  49.   nta -remove domain [view]
  50.         Remove a negative trust anchor, re-enabling validation
  51.         for the given domain.
  52.   querylog [ on | off ]
  53.         Enable / disable query logging.
  54.   reconfig    Reload configuration file and new zones only.
  55.   recursing    Dump the queries that are currently recursing (named.recursing)
  56.   refresh zone [class [view]]
  57.         Schedule immediate maintenance for a zone.
  58.   reload    Reload configuration file and zones.
  59.   reload zone [class [view]]
  60.         Reload a single zone.
  61.   retransfer zone [class [view]]
  62.         Retransfer a single zone without checking serial number.
  63.   scan        Scan available network interfaces for changes.
  64.   secroots [view ...]
  65.         Write security roots to the secroots file.
  66.   showzone zone [class [view]]
  67.         Print a zone's configuration.
  68.   sign zone [class [view]]
  69.         Update zone keys, and sign as needed.
  70.   signing -clear all zone [class [view]]
  71.         Remove the private records for all keys that have
  72.         finished signing the given zone.
  73.   signing -clear <keyid>/<algorithm> zone [class [view]]
  74.         Remove the private record that indicating the given key
  75.         has finished signing the given zone.
  76.   signing -list zone [class [view]]
  77.         List the private records showing the state of DNSSEC
  78.         signing in the given zone.
  79.   signing -nsec3param hash flags iterations salt zone [class [view]]
  80.         Add NSEC3 chain to zone if already signed.
  81.         Prime zone with NSEC3 chain if not yet signed.
  82.   signing -nsec3param none zone [class [view]]
  83.         Remove NSEC3 chains from zone.
  84.   signing -serial <value> zone [class [view]]
  85.         Set the zones's serial to <value>.
  86.   stats        Write server statistics to the statistics file.
  87.   status    Display status of the server.
  88.   stop        Save pending updates to master files and stop the server.
  89.   stop -p    Save pending updates to master files and stop the server
  90.         reporting process id.
  91.   sync [-clean]    Dump changes to all dynamic zones to disk, and optionally
  92.         remove their journal files.
  93.   sync [-clean] zone [class [view]]
  94.         Dump a single zone's changes to disk, and optionally
  95.         remove its journal file.
  96.   thaw        Enable updates to all dynamic zones and reload them.
  97.   thaw zone [class [view]]
  98.         Enable updates to a frozen dynamic zone and reload it.
  99.   trace        Increment debugging level by one.
  100.   trace level    Change the debugging level.
  101.   tsig-delete keyname [view]
  102.         Delete a TKEY-negotiated TSIG key.
  103.   tsig-list    List all currently active TSIG keys, including both statically
  104.         configured and TKEY-negotiated keys.
  105.   validation [ yes | no | status ] [view]
  106.         Enable / disable DNSSEC validation.
  107.   zonestatus zone [class [view]]
  108.         Display the current status of a zone.
  109. Version: 9.11.3-1ubuntu1.18-Ubuntu
复制代码
  
四、DNS安装部署

1、Centos7 配置域名正向解析主服务器

1.1、yum安装bind服务,及DNS工具 bind-utils
  1. yum install -y bind bind-utils
复制代码
  
1.2、修改 named.conf 配置文件,禁用服务限制,引入区域配置文件
  1. [root@node-centos7-70 ~]# vim /etc/named.conf
  2. options {
  3.     # listen-on port 53 { 127.0.0.1; };
  4.     listen-on-v6 port 53 { ::1; };
  5.     directory     "/var/named";
  6.     dump-file     "/var/named/data/cache_dump.db";
  7.     statistics-file "/var/named/data/named_stats.txt";
  8.     memstatistics-file "/var/named/data/named_mem_stats.txt";
  9.     recursing-file  "/var/named/data/named.recursing";
  10.     secroots-file   "/var/named/data/named.secroots";
  11.     # allow-query     { localhost; };
  12.     recursion yes;
  13.     dnssec-enable yes;
  14.     dnssec-validation yes;
  15.     /* Path to ISC DLV key */
  16.     bindkeys-file "/etc/named.root.key";
  17.     managed-keys-directory "/var/named/dynamic";
  18.     pid-file "/run/named/named.pid";
  19.     session-keyfile "/run/named/session.key";
  20. };
  21. logging {
  22.         channel default_debug {
  23.                 file "data/named.run";
  24.                 severity dynamic;
  25.         };
  26. };
  27. zone "." IN {
  28.     type hint;
  29.     file "named.ca";
  30. };
  31. include "/etc/named.rfc1912.zones";
  32. include "/etc/named.root.key";
  33. include "/etc/named.zones"
复制代码
  
1.3、创建 named.zones 区域配置文件
  1. [root@node-centos7-70 ~]# vim /etc/named.zones<br><br>zone "janzen.com" IN { type master; file "named.janzen.com"; allow-update { none; }; };
复制代码
  
1.4、创建 named.janzen.com 区域解析库文件
  1. [root@node-centos7-70 etc]# vim /var/named/named.janzen.com
  2. ;
  3. ; BIND reverse data file for broadcast zone
  4. ;
  5. $TTL    604800
  6. @       IN      SOA     janzen.com. root.localhost. (
  7.                               1         ; Serial
  8.                          604800         ; Refresh
  9.                           86400         ; Retry
  10.                         2419200         ; Expire
  11.                          604800 )       ; Negative Cache TTL
  12. ;
  13. @       IN      NS      dns1
  14.         IN      NS      dns2
  15.         IN      MX  12  mail1
  16.         IN      MX  10  mail2
  17. dns     IN      CNAME   dns1
  18. dns1    IN      A       10.0.0.20
  19. dns2    IN      A       10.0.0.21
  20. gitlab  IN      A       10.0.0.13
  21. harbor  IN      A       10.0.0.9
  22. harbor  IN      A       10.0.0.10
  23. www     IN      A       10.0.0.11
  24. mail1   IN      A       10.0.0.31
  25. mail2   IN      A       10.0.0.32
  26. _dnstext IN     TXT     this is @ name server
复制代码
  
1.5、修改文件权限
  1. [root@node-centos7-70 etc]# chmod 640 {/etc/named.zones,/var/named/named.janzen.com}
  2. [root@node-centos7-70 etc]# chgrp named {/etc/named.zones,/var/named/named.janzen.com}
复制代码
  
 1.6、设置named服务开机自启动
  1. [root@node-centos7-70 etc]# systemctl enable --now named
复制代码
  
1.7、使用dig测试DNS服务

[code][root@node-centos7-70 etc]# dig dns.janzen.com @10.0.0.70;  DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.13  dns.janzen.com @10.0.0.70;; global options: +cmd;; Got answer:;; ->>HEADERHEADERHEADERHEADERHEADERHEADERHEADERHEADERHEADERHEADERHEADERHEADER

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具