1基本知识点 DNS服务
DNS:Domain Name System 应用层协议 C/S,53/udp, 53/tcp BIND:Bekerley Internat Name Domain ISC (www.isc.org) 本地名称解析配置文件:hosts
### DNS域名
根域 一级域名:Top Level Domain: tld com, edu, mil, gov, net, org, int,arpa 三类:组织域、国家域(.cn, .ca, .hk, .tw)、反向域 二级域名 三级域名 最多127级域名 ICANN(The Internet Corporation for Assigned Names and Numbers) 互联网名称与数字地址分配机构,负责在全球范围内对互联网通用顶级域名 (gTLD)以及国家和地区顶级域名(ccTLD)系统的管理、以及根服务器系统 的管理
DNS域名结构
DNS解析
DNS查询类型: 递归查询 迭代查询 名称服务器:域内负责解析本域内的名称的主机 根服务器:13组服务器 解析类型: FQDN –> IP IP –> FQDN 注意:正反向解析是两个不同的名称空间,是两棵不同的解析树
DNS服务器类型
DNS服务器的类型: 主DNS服务器 从DNS服务器 缓存DNS服务器(转发器) * 主DNS服务器:管理和维护所负责解析的域内解析库的服务器 * 从DNS服务器:从主服务器或从服务器“复制”(区域传输)解析库副本 序列号:解析库版本号,主服务器解析库变化时,其序列递增 刷新时间间隔:从服务器从主服务器请求同步解析的时间间隔 重试时间间隔:从服务器请求同步失败时,再次尝试时间间隔 过期时长:从服务器联系不到主服务器时,多久后停止服务 * “通知”机制:主服务器解析库发生变化时,会主动通知从服务器
资源记录
区域解析库:由众多RR组成: 资源记录:Resource Record, RR 记录类型:A, AAAA, PTR, SOA, NS, CNAME, MX SOA:Start Of Authority,起始授权记录;一个区域解析库有且仅能有一个 SOA记录,必须位于解析库的第一条记录 A:internet Address,作用,FQDN –> IP AAAA:FQDN –> IPv6 PTR:PoinTeR,IP –> FQDN NS:Name Server,专用于标明当前区域的DNS服务器 CNAME : Canonical Name,别名记录 MX:Mail eXchanger,邮件交换器 TXT:对域名进行标识和说明的一种方式,一般做验证记录时会使用此项,如: SPF(反垃圾邮件)记录,https验证等 示例:_dnsauth TXT 2012011200000051qgs69bwoh4h6nht4n1h0lr038x
2基本环境安装 1 2 root: ~ # yum install -y bind root: ~ # vim /etc/named.conf
修改这两行,让所有主机都可访问 >listen-on port 53 { localhost; }; allow-query { any; }
3主从服务器搭建 搭建主服务器
1 2 3 4 5 6 7 8 9 10 11 vim /etc/ named.conf // listen-on port 53 { 127.0 .0.1 ; };// allow-query { localhost; }; allow-transfer {从服务器IP;}; vim /etc/ named.rfc1912.zones 添加 zone "qh.com" IN { type master; file "qh.com.zone" ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 root:/etc/ named # cp -p /var/ named/named.localhost / var/named/ qh.com.zone root:/var/ named $ vi /var/ named/qh.com.zone $TTL 1 D @ IN SOA master qh.com. ( 2019042210 ; serial 1 D ; refresh 1 H ; retry 1 W ; expire 3 H ) ; minimum NS master master A 192.168 .64.151 dbserver1 A 1.1 .1.1 dbserver2 A 2.2 .2.2
root:~ # systemctl restart named.service #重启服务
1 root: ~ $ dig dbserver1.qh.com @192 .168 .64 .151
搭建从服务器 1 2 3 4 5 6 7 8 9 10 11 12 yum install bind vim /etc/ named.conf // listen-on port 53 { 127.0 .0.1 ; };// allow-query { localhost; }; allow-transfer {none;}; vim /etc/ named.rfc1912.zones zone "qh.com" { type slave; masters {主服务器IP;}; file "slaves/qh.com.slave" ; }; root:~
1 2 3 4 5 6 7 8 root:/var/ named/slaves # cp -p / var/named/ named.localhost /var/ named/slave/ qh.com.slave root:/var/ named root:/var/ named/slaves root:/var/ named/slaves server reload successful root:/var/ named/slaves total 4 -rw-rw---- 1 root named 152 Jun 21 2007 qh.com.slave
1 root:~ $ vi /etc/ sysconfig/network-scripts/i fcfg-ens33
1 2 3 4 5 6 7 root:~ $ service network restart Restarting network (via systemctl): [ OK ] root:~ $ cat /etc/resolv.conf ; generated by /usr/sbin/dhclient-script search localdomainnameserver 192.168 .64 .151 nameserver 192.168 .64 .152
断开151网卡
在客户机测试
说明是从从服务器解析地址的,主从服务器已成功
4搭建反向主服务器
1 2 3 4 zone "64.168.192.in-addr.arpa" IN { type master file "192.168.64.zone" }
1 2 3 4 5 6 7 8 9 10 11 root:/var/named $ cp -p qh.com .zone 192.168 .64 .zone root:/var/named $ vim 192.168 .64 .zone $TTL 1 D@ IN SOA master qh.com . ( 1 1 H 10 M 12 H 1 D ) NS master master A 192.168 .64.151 7 PTR websrv.qh .com .6 PTR websrv.qh .com .100 PTR mail.qh .com .root:~ $ rndc reload root:~ $ systemctl restart named.service
测试
5父域委派子域到另一台服务器上
1 2 3 4 yum install bind vim /etc/ named.conf // listen-on port 53 { 127.0 .0.1 ; };// allow-query { localhost; };
在子域的DNS服务器上192.168.64.153
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 root:/var/named zone "beijing.qh.com" IN { type master; file "beijing.qh.com.zone"; }; root :/var/named # cp -p named.empty beijing.qh .com .zone root:/var/named # chmod g+w beijing.qh .com .zone root:~ $ vim /var/named/beijing.qh .com .zone $TTL 1D @ IN SOA master beijing.qh .com . ( 2019042214 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS master master A 192.168.64.153 websrv A 3.3.3.3 www CNAME websrv #此处可使用别名 root :~ $ systemctl start named.service
测试成功
6实现智能DNS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 root:~ acl beijingnet{ 192.168.64.0/24; }; acl shanghainet{ 192.168.65.0/24; }; acl othernet{ any; }; view beijingview{ match-clients {beijingnet;}; include "/etc/named/named.rfc1912.zones.bj" ; }; view shanghaiview{ match-clients {shanghainet;}; include "/etc/named/named.rfc1912.zones.sh" ; }; view otherview{ match-clients {othernet;}; include "/etc/named.rfc1912.zones" ; };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 root:/var/named root:/var/named root:/var/named $TTL 1 D @ IN SOA master qh .com ( 1 1 D 1H 1 W 3H ) NS master master A 192.168 .64.151 www A 6.6 .6.6 root:/var/named $TTL 1 D @ IN SOA master qh .com ( 1 1 D 1H 1 W 3H ) NS master master A 192.168 .64.151 www A 7.7 .7.7 root:/var/named cp: overwrite ‘qh.com.zone’? y root:/var/named $TTL 1 D @ IN SOA master qh .com ( 1 1 D 1H 1 W 3H ) NS master master A 192.168 .64.151 www A 8.8 .8.8
共三个数据库,对应不同地区
1 2 root:~ $ cp -p /etc/named.rfc1912 .zones /etc/named/named.rfc1912 .zones .bj root:~ $ cp -p /etc/named.rfc1912 .zones /etc/named/named.rfc1912 .zones .sh
共三个区域数据库文件
开始关联数据库
原有named.rfc1912.zones配置不变 * 语法检查
* 重启服务
1 2 3 4 #报错 root: /var/ named # rndc reload rndc: connect failed: 127.0 .0 .1 #953: connection refused # 尝试先重启服务在加载
* 测试 必须把DNS指向所配服务器 * DNS服务器 有两个ip段 * 以64网段
以65网段
其他网段
1 2 3 临时加一个网段 服务器:ip a a 192.168 .63.1 /24 dev ens33 客户端:ip a a 192.168 .63.2 /24 dev ens33
测试均已成功 ![](https://s1.51cto.com /images/blog/201904/23/0c181b664c6773ed2c32d1d336ae799a.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
7互联网DNS架构实验
架构图
共7台主机,联合实现互联网dns架构
1将客户端dns服务器指向本地dns服务器
2将网站搭建好
1 2 3 4 5 root: ~ # yum install httpd root: ~ # cd /var/www/html/ root: /var/ www/html # echo 192.168.64.57,hello >index.html root: /var/ www/html # chmod a+r index.html root: /var/ www/html # service httpd restart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 root: ~ # yum install bind root: ~ # vi /etc/named.conf allow-transfer { 192.168 .64 .47 ; }; root: ~ # vi /etc/named.rfc1912.zones zone "qh.com" IN { type master ; file "qh.com.zone" ; }; root: ~ # cd /var/named/ root: /var/ named # vi qh.com.zone $TTL 1 D @ IN SOA ns1 qh.mail.com. ( 1 1 H 10 M 1 D 3 H ) NS ns1 NS ns2 ns1 A 192.168 .64 .37 ns2 A 192.168 .64 .47 www A 192.168 .64 .57 root: /var/ named # chgrp named qh.com.zone root: /var/ named # chmod 640 qh.com.zone #### 语法检查 root: /var/ named # named-checkconf #### 启动服务 root: /var/ named # systemctl start named.service
5客户端测试master服务器
6搭建从服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 root: ~ # yum install bind root: ~ # vi /etc/named.conf allow-transfer { none ; }; root: ~ # vi /etc/named.rfc1912.zones zone "qh.com" { type slave ; masters { 192.168 .64 .37 ; }; file "slaves/qh.com.slave" ; }; root: /var/ named/slaves # systemctl start named.service root: /var/ named/slaves # rndc reload root: /var/ named/slaves # ll total #已同步 -rw-r--r-- 1 named named 269 Apr 23 16 :34 qh.com.slave
7测试从服务器
8配置com域服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 root: ~ # yum install bind root: ~ # vi /etc/named.conf allow-transfer { none ; }; ------------------------------------------------ root: ~ # vi /etc/named.rfc1912.zones zone "com" IN { type master ; file "com.zone" ; }; --------------------------------------------------------- root: ~ # cd /var/named/ root: /var/ named # vim com.zone $TTL 1 D @ IN SOA NS1 qh.mail.com. (1 1 D 1 H 1 W 3 D ) NS ns1 qh NS qhns1 qh NS qhns2 ns1 A 192.168 .64 .27 qhns1 A 192.168 .64 .37 #主服务器 qhns2 A 192.168 .64 .47 #从服务器 root: /var/ named # chgrp named com.zone root: /var/ named # chmod g+w com.zone root: /var/ named # systemctl start named.service root: /var/ named # rndc reload server reload successful
9测试 (通过父域192.168.64.27测试)
10搭建根DNS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 root: ~ # yum install bind root: ~ # vi /etc/named.conf zone "." IN { type master ; #改为master自己做根 file "root.zone" ; }; root: ~ # cd /var/named/ root: /var/ named # vim root.zone $TTL 1 D @ IN SOA ns1 qh.mail.com. (1 1 D 1 H 1 W 3 D ) NS ns1 com NS comns1 ns1 A 192.168 .64 .17 comns1 A 192.168 .64 .27 root: /var/ named # chgrp named com.zone root: /var/ named # chmod g+w com.zone root: /var/ named # systemctl start named.service
11测试(通过主根服务器测试)
12配置本地dns服务器
1 2 3 4 5 6 7 8 9 root:~ root:~ // listen-on port 53 { 127.0 .0.1 ; };// allow-query { localhost; };root:/etc/ sysconfig/network-scripts # vi / var/named/ named.ca . 518400 IN NS a.root-servers.net. a.root-servers.net. 3600000 IN A 192.168 .64.17
1 2 3 root:~ # vi /etc/named.conf dnssec-enable no ; dnssec-validation no ;
15测试
一些过程中可能遇到的错误 1 2 root:/var/named Job for named.service failed because the control process exited with error code. See "systemctl status named.service" and "journalctl -xe" for details.
一般可以通过systemctl status named.service -l 命令可以查看当前错误类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #dig A example.com ... SERVFAIL:The nameserver encountered a problem while processing the query. • 可使用dig +trace排错,可能是网络和防火墙导致 NXDOMAIN:The queried name does not exist in the zone. • 可能是CNAME 对应的A 记录不存在导致 REFUSED:The nameserver refused the client's DNS request due to policy restrictions. • 可能是DNS策略导致 • NOERROR不代表没有问题,也可以是过时的记录 •查看是否为权威记录,flags:aa标记判断 •被删除的记录仍能返回结果,可能是因为*记录存在 •如:*.example.com. IN A 172.25.254.254 •注意“.”的使用 •避免CNAME 指向CNAME 记录,可能产生回环 •est.example.com. IN CNAME lab.example.com. •lab.example.com. IN CNAME test.example.com. •正确配置PTR 记录,许多服务依赖PTR ,如sshd,MTA •正确配置轮询round-robin记录