目录
dns介绍
dns客户端工具
dns服务端
DNS配置文件
客户端测试工具host
dns测试工具
dig
nslookup
实验1正向解析dns
资源记录
SOA记录
NS记录
反向解析dns服务
recursion
总结
实验:实现正向解析的主DNS服务
语法检查
实验:实现反向解析DNS服务
dns介绍
作用:把网址解析成ip地址
DNS:Domain Name Service 应用层协议
C/S,53/udp, 53/tcp 客户端默认安装,服务器端需要手动安装
BIND:Bekerley Internat Name Domain
ISC (www.isc.org)此公司研发了DNS和dhcp,其官网地址
本地名称解析配置文件:hosts
/etc/hosts
%WINDIR%/system32/drivers/etc/hosts 如C:\Windows\System32\drivers\etc\hosts
122.10.117.2 www.baidu.com
93.46.8.89 www.google.com
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)系统的管理、以及根服务器系统的管理
如图
https://wanwang.aliyun.com. 阿里云的dns域名注册
.根域,一般会隐藏起来;.com顶级域;aliyun二级域;wanwang主机;
https://www.baidu.com./
从右到左: .根域;.com顶级域;.baidu二级域;www主机
如本地电脑访问百度,小区的电信网,电信dns:114.114.114.114转向根域,在转向.com域,在转向baidu二级域,在到主机。解析ip后,访问百度。之后,电信dns在一段时间内缓存百度域名对应的ip地址,此时间内小区其它人访问百度,则电信dns直接解析。而本地电脑也会在一段时间内缓存百度域名对应的ip,再次打开百度,则不会访问根域解析dns,本地缓存直接解析ip,打开网页
linux命令:whois baidu.com 可查看baidu.com域名的注册信息等
13个根dns服务器,10个在美国,1个在荷兰,1个在瑞典,1个在日本。13个是由dns报文长度决定的。ipv4的dns服务器。Ipv6推广,共25个,中国有一个ipv6的dns服务器
DNS解析
一次完整的查询请求经过的流程:
Client -->hosts文件 -->DNS Service Local Cache --> DNS Server (recursion) --> Server Cache --> iteration(迭代) --> 根--> 顶级域名DNS--> 二级域名DNS…
解析答案:
肯定答案:
否定答案:请求的条目不存在等原因导致无法返回结果
权威答案:
非权威答案:
————————————
windows有本地缓存,linux客户端没有缓存
windows:ipconfig/displaydns ipconfig/flushdns
资源记录
区域解析库:由众多RR组成:
资源记录:Resource Record, RR
记录类型:A, AAAA, PTR, SOA, NS, CNAME, MX
互联网域名
域名注册:
代理商:万网, 新网, godaddy
注册完成以后,想自己用专用服务来解析
管理后台:把NS记录指向的服务器名称,和A记录指向的服务器地址
默认本机的dns解析文件/etc/hosts比互联网上的dns解析优先级高,可在文件/etc/nsswitch.conf中更改
[root@cos7:~ ]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
示例:
[root@centos7:etc]# ping www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=128 time=4.12 ms
[root@cos7:~ ]# vim /etc/hosts
1.1.1.1 www.baidu.com
[root@centos7:etc]# ping www.baidu.com
PING www.baidu.com (1.1.1.1) 56(84) bytes of data.
调整优先级
[root@centos7:~]# vim /etc/nsswitch.conf
#hosts: db files nisplus nis dns
hosts: files dns myhostname
#files代表本地的/etc/hosts文件优先级,dns表示互联网上的dns解析服务
#hosts: dns files myhostname 即表示dns优先级高于files,一般不调整
/etc/hosts可以在局域网中使用,如搭建集群服务,建议把主机名加入到/etc/hosts文件中,如下
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 centos7 #主机名centos7
192.168.31.100 www.my.com #网址对应的域名
当企业中有几百台服务器时,可以集中的把网址和ip的对应关系放到NIS服务器上。互联网的机器太多,用dns解决
www.baidu.com. 全域名或者FQDN (Fully Qualified Domain Name)正式域名
www主机名
baidu.com域名
.根域,一般默认不写
www.baidu.com. 简单解析流程为,先找根域. 根域帮助迭代到.com ,顶级域在帮助迭代到.baidu 最后到www代表的服务器。实际解析比此流程复杂
[root@centos7:~]# whois baidu.com
Domain Name: BAIDU.COM
Registry Domain ID: 11181110_DOMAIN_COM-VRSN
Registrar WHOIS Server: whois.markmonitor.com
递归查询,负责到底
迭代查询,自己没有,转给别的dns服务器
dns客户端工具
默认安装
[root@centos7:~]# rpm -qa "bind*"
bind-utils-9.9.4-61.el7.x86_64
bind-libs-9.9.4-61.el7.x86_64
bind-license-9.9.4-61.el7.noarch
bind-9.9.4-61.el7.x86_64
bind-libs-lite-9.9.4-61.el7.x86_64
[root@centos7:~]# rpm -ql bind-utils
/etc/trusted-key.key
/usr/bin/dig #dns测试工具dig
/usr/bin/host #dns测试工具host
/usr/bin/nslookup #dns测试工具nslookup,windows中也可以使用
/usr/bin/nsupdate
/usr/share/man/man1/dig.1.gz
/usr/share/man/man1/host.1.gz
/usr/share/man/man1/nslookup.1.gz
/usr/share/man/man1/nsupdate.1.gz
[root@centos7:~]#
www.isc.org 开发了dhcp和dns
dns服务端
[root@centos7:~]# yum info bind
[root@centos7:~]# yum install bind
[root@centos7:~]# rpm -ql bind
/var/named 存放dns解析数据库
/etc/named.conf 配置文件
/var/named/named.ca 13个根域存放文件
/usr/sbin/named 主程序
/usr/lib/systemd/system/named.service 服务
/usr/sbin/rndc
/var/log/named.log 日志
/var/named/named.ca
[root@centos7:~]# getenforce
Disabled
[root@centos7:~]# iptables -vnL
[root@centos7:~]# systemctl start named
[root@centos7:~]# ss -ntul
udp UNCONN 0 0 127.0.0.1:53 *:*
tcp LISTEN 0 10 127.0.0.1:53 *:*
#只能为本机提供服务,不能对外提供服务,需要修改配置文件
DNS配置文件
#centos7可以访问外网,有一个nat模式的网卡;还有一个仅主机模式的网卡(1921.168.31.7)
[root@centos7:~]# vim /etc/named.conf
#listen-on port 53 { 127.0.0.1; };改为如下
#dns服务自身的acl localhost ,所有ip
listen-on port 53 { localhost; };
一个数据库文件对应一个dns域解析
filename <--> baidu.com zone区域
[root@centos7:~]# cat /etc/named.conf
options { #选项
listen-on port 53 { localhost; };
listen-on-v6 port 53 { ::1; };
directory "/var/named"; #include包含文件等的路径
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
allow-query { localhost; }; #允许本机查询;{ any; };任何主机都生效
recursion yes; #递归查询
dnssec-enable yes;
dnssec-validation yes;
/* Path to ISC DLV key */
bindkeys-file "/etc/named.iscdlv.key";
managed-keys-directory "/var/named/dynamic";
pid-file "/run/named/named.pid";
session-keyfile "/run/named/session.key";
};
logging { #日志
channel default_debug {
file "data/named.run";
severity dynamic;
};
};
zone "." IN { #区域 .根
type hint;
file "named.ca"; # 13个根域的文件名
};
include "/etc/named.rfc1912.zones"; #包含zone文件,zone可以写到/etc/named.rfc1912.zones中
include "/etc/named.root.key";
[root@centos7:~]# systemctl restart named
[root@centos7:~]# ss -ntul
udp UNCONN 0 0 192.168.31.7:53 *:*
udp UNCONN 0 0 127.0.0.1:53 *:*
tcp LISTEN 0 10 192.168.31.7:53 *:*
tcp LISTEN 0 10 127.0.0.1:53 *:*
------------------------------
#把客户端centos7(192.168.31.27)添加一个dns1
[root@client:/etc/sysconfig/network-scripts ]# vim ifcfg-ens33
TYPE=Ethernet
NAME=ens33
BOOTPROTO=none
IPADDR=192.168.31.27
DNS1=192.168.31.7 #添加dns1服务主机
#查看是否生效
[root@client:~ ]# cat /etc/resolv.conf
# Generated by NetworkManager
search magedu.com localdomain
#nameserver 223.5.5.5
#nameserver 223.6.6.6
nameserver 192.168.31.7
客户端测试工具host
[root@client:~ ]# host www.baidu.com
Host www.baidu.com.localdomain not found: 5(REFUSED)
[root@centos7:~]# cat /etc/named.conf
allow-query { localhost; }; #只允许本机查询
#在dns本机服务上查询如下
[root@centos7:~]# host www.baidu.com 127.0.0.1 #问自己,没有,递归找根域
Using domain server:
Name: 127.0.0.1
Address: 127.0.0.1#53
Aliases:
www.baidu.com is an alias for www.a.shifen.com.
www.a.shifen.com has address 61.135.169.125
www.a.shifen.com has address 61.135.169.121
[root@centos7:~]# vim /etc/named.conf
allow-query { any; }; #允许任何主机查询
#生效,重启服务,或者如下命令
[root@centos7:~]# rndc reload
server reload successful
#在客户端再次访问,可以访问,并且此时dns服务主机是有第一次缓存的,比较快
[root@client:~ ]# host www.baidu.com
www.baidu.com is an alias for www.a.shifen.com.
www.a.shifen.com has address 61.135.169.125
www.a.shifen.com has address 61.135.169.121
[root@client:~ ]# host www.taobao.com
www.taobao.com is an alias for www.taobao.com.danuoyi.tbcache.com. #别名解析,cdn分布式dns,每个地区的dns
www.taobao.com.danuoyi.tbcache.com has address 123.129.215.178
www.taobao.com.danuoyi.tbcache.com has address 123.129.215.222
dns只负责解析ip,至于网络通不通,和dns没有关系。并且linux客户端是没有缓存功能的,windows有
这里搭建的dns起到的只是缓存功能,可以成为非权威dns。只缓存服务器,没有缓存就转发到根域(13个根服务器)
[root@centos7:etc]# cat /var/named/named.ca
; <<>> DiG 9.9.4-RedHat-9.9.4-38.el7_3.2 <<>> +bufsize=1200 +norec @a.root-servers.net
; (2 servers found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 17380
;; flags: qr aa; QUERY: 1, ANSWER: 13, AUTHORITY: 0, ADDITIONAL: 27
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1472
;; QUESTION SECTION:
;. IN NS
;; ANSWER SECTION: #13个根域
. 518400 IN NS a.root-servers.net.
. 518400 IN NS b.root-servers.net.
. 518400 IN NS c.root-servers.net.
. 518400 IN NS d.root-servers.net.
. 518400 IN NS e.root-servers.net.
. 518400 IN NS f.root-servers.net.
. 518400 IN NS g.root-servers.net.
. 518400 IN NS h.root-servers.net.
. 518400 IN NS i.root-servers.net.
. 518400 IN NS j.root-servers.net.
. 518400 IN NS k.root-servers.net.
. 518400 IN NS l.root-servers.net.
. 518400 IN NS m.root-servers.net.
***省略***
;; ANSWER SECTION:
. 518400 IN NS a.root-servers.net.
. 518400 IN NS b.root-servers.net.
***省略***
;; ADDITIONAL SECTION:
a.root-servers.net. 3600000 IN A 198.41.0.4 #A记录为ipv4
a.root-servers.net. 3600000 IN AAAA 2001:503:ba3e::2:30 #AAAA记录为ipv6
路由器就带有只缓存dns服务功能
-----------------
dns测试工具
dig
"/etc/resolv.conf" 5L, 128C
# Generated by NetworkManager
search magedu.com localdomain
nameserver 223.5.5.5
nameserver 223.6.6.6
nameserver 192.168.31.7
~
[root@client:~ ]# dig www.baidu.com
; <<>> DiG 9.9.4-RedHat-9.9.4-50.el7 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 5364 #status:NOERROR没有错误
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION: #我查询的问题
;www.baidu.com. IN A
;; ANSWER SECTION: #回答结果
www.baidu.com. 71 IN CNAME www.a.shifen.com.
www.a.shifen.com. 71 IN A 61.135.169.125
www.a.shifen.com. 71 IN A 61.135.169.121
;; Query time: 39 msec #查询所花费时间
;; SERVER: 223.5.5.5#53(223.5.5.5)
;; WHEN: Sun Jul 22 18:41:36 CST 2018
;; MSG SIZE rcvd: 90
nslookup
[root@client:~ ]# nslookup
> server 192.168.31.7
Default server: 192.168.31.7
Address: 192.168.31.7#53
> www.qq.com
Server: 192.168.31.7
Address: 192.168.31.7#53
Non-authoritative answer:
www.qq.com canonical name = https.qq.com.
Name: https.qq.com
Address: 125.39.52.26
> exit
windows------
C:\Users\29256>nslookup
默认服务器: public1.alidns.com
Address: 223.5.5.5
> www.taobao.com
服务器: public1.alidns.com
Address: 223.5.5.5
非权威应答:
名称: www.taobao.com.danuoyi.tbcache.com
Addresses: 123.129.215.222
123.129.215.178
Aliases: www.taobao.com
#查询域baidu.com对应的NS(name server)服务器名称
[root@client:~ ]# dig -t NS baidu.com @192.168.31.7
; <<>> DiG 9.9.4-RedHat-9.9.4-50.el7 <<>> -t NS baidu.com @192.168.31.7
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 59030
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 6
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION: #询问
;baidu.com. IN NS
;; ANSWER SECTION: #回答
baidu.com. 86400 IN NS ns3.baidu.com.
baidu.com. 86400 IN NS dns.baidu.com.
baidu.com. 86400 IN NS ns2.baidu.com.
baidu.com. 86400 IN NS ns4.baidu.com.
baidu.com. 86400 IN NS ns7.baidu.com.
;; ADDITIONAL SECTION:
dns.baidu.com. 170251 IN A 202.108.22.220
ns7.baidu.com. 170251 IN A 119.75.219.82
ns3.baidu.com. 170251 IN A 220.181.37.10
ns4.baidu.com. 170251 IN A 220.181.38.10
ns2.baidu.com. 170251 IN A 61.135.165.235
;; Query time: 6 msec
;; SERVER: 192.168.31.7#53(192.168.31.7)
;; WHEN: Sun Jul 22 18:47:35 CST 2018
;; MSG SIZE rcvd: 208
----------
[root@client:~ ]# dig -t NS baidu.com #查询域名对应的主机名
[root@client:~ ]# dig dns.baidu.com. #由主机名查询ip地址
---------------
[root@client:~ ]# dig www.baidu.com @61.135.165.235
; <<>> DiG 9.9.4-RedHat-9.9.4-50.el7 <<>> www.baidu.com @61.135.165.235
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 39536
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 5, ADDITIONAL: 6 #aa权威应答,即不是从别处问的
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;www.baidu.com. IN A
;; ANSWER SECTION:
www.baidu.com. 1200 IN CNAME www.a.shifen.com.
;; AUTHORITY SECTION:
a.shifen.com. 1200 IN NS ns4.a.shifen.com.
a.shifen.com. 1200 IN NS ns2.a.shifen.com.
a.shifen.com. 1200 IN NS ns1.a.shifen.com.
a.shifen.com. 1200 IN NS ns5.a.shifen.com.
a.shifen.com. 1200 IN NS ns3.a.shifen.com.
;; ADDITIONAL SECTION:
ns1.a.shifen.com. 1200 IN A 61.135.165.224
ns2.a.shifen.com. 1200 IN A 180.149.133.241
ns3.a.shifen.com. 1200 IN A 61.135.162.215
ns4.a.shifen.com. 1200 IN A 115.239.210.176
ns5.a.shifen.com. 1200 IN A 180.76.76.95
;; Query time: 6 msec
;; SERVER: 61.135.165.235#53(61.135.165.235)
;; WHEN: Sun Jul 22 18:52:57 CST 2018
;; MSG SIZE rcvd: 239
==========================
============================
实验1正向解析dns
[root@centos7:etc]# vim named.conf
include "/etc/named.rfc1912.zones"; 另一个主配置文件
#,参考已有格式,添加如下内容
[root@centos7:etc]# vim /etc/named.rfc1912.zones
zone "baidu.com" IN {
type master;
file "baidu.com.zone";
};
[root@centos7:etc]# cat named.conf
directory "/var/named";
[root@centos7:etc]# cd /var/named
[root@centos7:named]# ll
total 20
-rw-r----- 1 root named 152 Jun 21 2007 baidu.com.zone
drwxrwx--- 2 named named 23 Jul 21 09:41 data
drwxrwx--- 2 named named 60 Jul 22 17:55 dynamic
-rw-r----- 1 root named 2281 May 22 2017 named.ca
-rw-r----- 1 root named 152 Dec 15 2009 named.empty
-rw-r----- 1 root named 152 Jun 21 2007 named.localhost
-rw-r----- 1 root named 168 Dec 15 2009 named.loopback
drwxrwx--- 2 named named 6 Apr 13 02:48 slaves
[root@centos7:named]# cp -p named.localhost baidu.com.zone
baidu.com.zone 为区域文件,区域文件中有众多的资源记录
资源记录
语法:name [TTL] IN rr_type value
SOA记录
SOA必须放在文件的第一行,必有有SOA
baidu.com 86400 IN SOA 管理员邮箱 master dns (1 7d 1h expire 2w 1d)
Start Of Authority,起始授权记录
主服务器master
从服务器是主服务器的备份 slave
push 主服务器主动推给从服务器
pull 从服务器从主服务器拉dns数据
管理员邮箱:出问题了可以发邮件用
TTLdns数据更新时间
IN internat
master dns (1 7d 1h expire 2w 1d)
1 版本号:版本号变化时,从服务器会同步主服务器的dns数据,即push
7d 7天:主服务器push失败,则7d为一周期,从服务器去主服务器拉数据,即pull
1h : pull失败,则每1h重试一次
2w: 主服务宕机,从服务器数据内容一直无法更新,可能是错误的,不能一直对外提供服务,2周失效,不再提供服务
1d: 如果有人一直访问一个错误的信息,如wwwwww.baidu.com,则会影响dns服务器,把此垃圾信息缓存到客户端时长为1天
NS记录
NS:name server;主从服务器记录,如下,有3个dns记录
baidu.com 86400 IN NS dns1.baidu.com
baidu.com 86400 IN NS dns1.baidu.com
baidu.com 86400 IN NS dns1.baidu.com
dns1.baidu.com 86400 IN A 1.1.1.1 # A记录
IN:internat
A:ipv4
[root@centos7:named]# cp -p named.localhost baidu.com.zone
[root@centos7:named]# vim baidu.com.zone
$TTL 1D #定义TTL
@ IN SOA @ rname.invalid. ( #@为本域,可写为:baidu.com.或者@ TTL省略,
#第二个@写为dns1或者dns1.baidu.com. admin.后的点表示邮箱中的@,admin.baidu.com.是一个邮箱地址
0 ; serial #分号表示注释
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @ #NS前面省略了@,从上面继承,
A 127.0.0.1 #dns1表示主服务器dns1 #A记录 ipv4
AAAA ::1 #此行表示从服务器dns2 #AAAA记录 ipv6
---------------
$TTL 1D
@ IN SOA dns1 admin.baidu.com. (
1807222001 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
@ NS dns1
dns1 A 192.168.31.7
web-nginx01 A 1.1.1.1
web-nginx01 A 1.1.1.2
app-tomcat02 A 2.2.2.2
www CNAME web-nginx01 #别名记录,可以实现负载均衡
mail-exchange01 A 3.3.3.3 #邮件发送目的地址
mail-exchange02 A 3.3.3.4 #两个邮件服务器,有优先级 10 ,20
@ MX 10 mail-exchange01 #MX Mail-eXchange #MX记录
@ MX 20 mail-exchange02
* A 191.168.31.7 # 如果输错网址,如wwwwww.baidu.com。则访问本地的httpd服务,泛域名解析
@ A 192.168.31.7 # 即表示baidu.com也可以访问
#$GENERATE 1-100 web-nginx0$ A 1.1.1.$ #此写法较为方便
#
#检查配置文件/etc/named.conf /etc/named.rfc1912.zones
[root@centos7:named]# named-checkconf
#检查数据库
[root@centos7:named]# named-checkzone baidu.com /var/named/baidu.com.zone
zone baidu.com/IN: loaded serial 1807222001
OK
#从新加载配置文件
[root@centos7:named]# rndc reload
server reload successful
---------------------------------------------------
[root@client:~ ]# vim /etc/resolv.conf
# Generated by NetworkManager
search magedu.com localdomain
#nameserver 223.5.5.5
#nameserver 223.6.6.6
nameserver 192.168.31.7
[root@client:~ ]# ping www.baidu.com
PING web-nginx01.baidu.com (1.1.1.1) 56(84) bytes of data.
[root@client:~ ]# ping www.baidu.com
PING web-nginx01.baidu.com (1.1.1.2) 56(84) bytes of data.
[root@client:~ ]# dig www.baidu.com
; <<>> DiG 9.9.4-RedHat-9.9.4-50.el7 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 3117
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 1, ADDITIONAL: 2
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;www.baidu.com. IN A
;; ANSWER SECTION:
www.baidu.com. 86400 IN CNAME web-nginx01.baidu.com.
web-nginx01.baidu.com. 86400 IN A 1.1.1.2
web-nginx01.baidu.com. 86400 IN A 1.1.1.1
;; AUTHORITY SECTION:
baidu.com. 86400 IN NS dns1.baidu.com.
;; ADDITIONAL SECTION:
dns1.baidu.com. 86400 IN A 192.168.31.7
;; Query time: 0 msec
;; SERVER: 192.168.31.7#53(192.168.31.7)
;; WHEN: Sun Jul 22 20:22:39 CST 2018
;; MSG SIZE rcvd: 135
[root@client:~ ]# dig wwwww.baidu.com
;; ANSWER SECTION:
wwwww.baidu.com. 86400 IN A 191.168.31.7
;; AUTHORITY SECTION:
baidu.com. 86400 IN NS dns1.baidu.com.
;; ADDITIONAL SECTION:
dns1.baidu.com. 86400 IN A 192.168.31.7
[root@client:~ ]# curl wwwww.baidu.com
www.google.com.
[root@client:~ ]# curl baidu.com
www.google.com.
=========================
=========================
=========================
=========================
=========================
=========================
=========================
=========================
=========================
反向解析dns服务
#参考1.00.127写反向解析
[root@centos7:etc]# vim /etc/named.rfc1912.zones
zone "1.0.0.127.in-addr.arpa" IN {
type master;
file "named.loopback";
allow-update { none; };
};
zone "31.168.192.in-addr.arpa" IN {
type master;
file "192.168.31.zone";
};
[root@centos7:etc]# cd /var/named
[root@centos7:named]# vim 192.168.31.zone
$TTL 86400
@ IN SOA dns1.baidu.com. admin.baidu.com. ( 1 1D 1H 3D 2D)
NS dns1.baidu.com.
7.31.168.192.in-addr.arpa. PTR dns1.baidu.com. #或者只写一个7
100 PTR www.qq.com.
#此文件的属组应为named,但此处other有r权限,不影响
[root@centos7:named]# ll
total 24
-rw-r--r-- 1 root root 153 Jul 22 21:14 192.168.31.zone
[root@centos7:named]# named-checkconf
[root@centos7:named]# named-checkzone 31.168.192.in-addr.arpa 192.168.31.zone
zone 31.168.192.in-addr.arpa/IN: loaded serial 1
OK
[root@centos7:named]# rndc reload
server reload successful
[root@client:~ ]# dig -x 192.168.31.7
; <<>> DiG 9.9.4-RedHat-9.9.4-50.el7 <<>> -x 192.168.31.7
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 53363
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;7.31.168.192.in-addr.arpa. IN PTR
;; ANSWER SECTION:
7.31.168.192.in-addr.arpa. 86400 IN PTR dns1.baidu.com.
;; AUTHORITY SECTION:
31.168.192.in-addr.arpa. 86400 IN NS dns1.baidu.com.
;; ADDITIONAL SECTION:
dns1.baidu.com. 86400 IN A 192.168.31.7
;; Query time: 0 msec
;; SERVER: 192.168.31.7#53(192.168.31.7)
;; WHEN: Sun Jul 22 21:25:01 CST 2018
;; MSG SIZE rcvd: 112
--------
[root@client:~ ]# dig 100.31.168.192.in-addr.arpa.
; <<>> DiG 9.9.4-RedHat-9.9.4-50.el7 <<>> 100.31.168.192.in-addr.arpa.
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 16192 #如果是status: NXDOMAIN,表示错误
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;100.31.168.192.in-addr.arpa. IN A
;; AUTHORITY SECTION:
31.168.192.in-addr.arpa. 86400 IN SOA dns1.baidu.com. admin.baidu.com. 1 86400 3600 259200 172800
;; Query time: 0 msec
;; SERVER: 192.168.31.7#53(192.168.31.7)
;; WHEN: Sun Jul 22 21:26:52 CST 2018
;; MSG SIZE rcvd: 112
-----------
recursion
[root@centos7:etc]# vim /etc/named.conf
把此行recursive yes;改为如下
recursion no;
#客户端ping www.qq.com;由于dns服务上没有此记录,故而通过递归查询去找别的dns
[root@client:~ ]# ping www.qq.com
PING https.qq.com (125.39.52.26) 56(84) bytes of data.
64 bytes from no-data (125.39.52.26): icmp_seq=2 ttl=53 time=21.0 ms
#dns服务端
[root@centos7:etc]# rndc reload
server reload successful
#客户端
[root@client:~ ]# ping www.qq.com
ping: www.qq.com: Name or service not known
总结
实验:实现正向解析的主DNS服务
1 vim /etc/named.conf
options {
listen-on port 53 { localhost; };
allow-query { any; };
2vim /etc/named.rfc1912.zones
zone "baidu.com" IN {
type master;
file "baidu.com.zone";
};
3vim /var/named/baidu.com.zone
$TTL 1D
@ IN SOA dns1 admin.baidu.com. (
1807211113 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
@ NS dns1
dns1 A 192.168.31.7
web-nginx01 A 1.1.1.1
web-nginx01 A 1.1.1.2
app-tomcat02 A 2.2.2.2
www CNAME web-nginx01
mail-exchange01 A 3.3.3.3
mail-exchange02 A 3.3.3.4
@ MX 10 mail-exchange01
@ MX 20 mail-exchange02
* A 192.168.31.7
@ A 192.168.31.7
$GENERATE 1-100 web-nginx0$ A 1.1.1.$
语法检查
[root@centos7:named]# named-checkconf
#检查数据库
[root@centos7:named]# named-checkzone baidu.com /var/named/baidu.com.zone
zone baidu.com/IN: loaded serial 1807222001
OK
实验:实现反向解析DNS服务
1cat /etc/named.rfc1912.zones
zone "31.168.192.in-addr.arpa" IN {
type master;
file "192.168.31.zone";
};
2[root@centos7 named]#cat /var/named/192.168.31.zone
$TTL 86400
@ IN soa dns1.baidu.com. admin.baidu.com. ( 1 1D 1H 3D 2D )
NS dns1.baidu.com.
7 PTR dns1.baidu.com.
100 PTR www.qq.com.
实验:主从DNS
1 主DNS
vim /etc/named.conf
allow-transfer {从DNSIP;};
vim /var/named/baidu.com.zone
@ IN SOA dns1.baidu.com. admin.baidu.com. (
1807211121 ; serial
86400 ; refresh (1 day)
3600 ; retry (1 hour)
604800 ; expire (1 week)
10800 ; minimum (3 hours)
)
NS dns1.baidu.com.
NS dns2.baidu.com.
NS dns3.baidu.com.
dns1 A 192.168.31.7
dns2 A 192.168.31.6
dns3 A 192.168.31.17
2 从DNS
vim /etc/named.conf
// listen-on port 53 { 127.0.0.1; };
// allow-query { localhost; };
allow-transfer {none;};
vim /etc/named.rfc1912.conf
zone "baidu.com" IN {
type slave;
masters {192.168.31.7;};
file "slaves/baidu.com.slave.zone";
};
baidu.com DNS1
shanghai.baidu.com DNS1