DNS域名解析服务(Domain Name System)是用于解析域名与IP地址对应关系的服务,功能上可以实现正向解析与反向解析:
一、DNS服务器工作模式分类:
1、主服务器:在特定区域内具有唯一性、负责维护该区域内的域名与IP地址对应关系。
2、从服务器:从主服务器中获得域名与IP地址对应关系并维护,以防主服务器宕机等情况。
3、缓存服务器:通过向其他域名解析服务器查询获得域名与IP地址对应关系,提高重复查询时的效率.
二、DNS查询方式:
1、迭代查询:
2、递归查询:
三、解析方式分类
1、正向解析,既将FQDN解析为IP.
2、反向解析,将IP解析为FQDN.
四、Bind配置文件的结构:
主程序 | /usr/sbin/named |
主配置文件 | /etc/named.conf |
区域配置文件 | /etc/named.rfc1912.zones |
zone文件的默认路径 | /var/named |
/etc/named.conf : Bind的主配置文件,用于定义全局设置,DNS的zone等相关配置。
1.options部分:
options { //options段用于定义全局设置
listen-on port 53 { 127.0.0.1; };
//定义bind的监听IP地址(IPv4)
listen-on-v6 port 53 { ::1; };
//定义bind的监听IP地址(IPv6)
directory "/var/named";
//zone文件的默认路径
dump-file "/var/named/data/cache_dump.db";
//cache的备份
statistics-file "/var/named/data/named_stats.txt";
//静态文件
memstatistics-file "/var/named/data/named_mem_stats.txt";
//内存静态文件
allow-query { localhost; };
//允许谁向此DNS进行查询
recursion yes|no;
//允许递归查询
#安全相关部分:
dnssec-enable yes;
dnssec-validation yes;
dnssec-lookaside auto;
/* Path to ISC DLV key */
bindkeys-file "/etc/named.iscdlv.key";
managed-keys-directory "/var/named/dynamic";
};
2.日志系统部分:
logging { //定义日志
channel myfile {
//定义channel名称
file "data/named.run";
//以文件形式存储日志
severity dynamic;
//存储日志的级别,一共7个级别从高到低分别是:crit,error,warning,notice,info(前面5个属于syslog);debug[level],dynamic(后两个属于Bind8,9独有的级别)
};
category statistics { my_file; };
//定义bind系统中各子系统的日志 //将日志发给那个channel,可以发给多个channel,一个channel只能接受一个category。
};
3.定义zone
zone "." IN { //定义Dns的zone,"."代表根区域
type hint; //定义zone的类型,根区域的类型就为hint
file "named.ca"; //指定zone文件,默认已经生成
};
二、DNS中zone文件的放置/var/named/*.zone(与named.conf中的zone对应的文件)
zone文件的书写格式:
$TTL 1D //用宏定义一个TTL默认值为1天,下面数据直接引用此值.
@ [TTL] IN SOA 主DNS服务器FQDN 管理员邮箱 (
0 ; 序列号
1D ; 更新间隔
1H ; 更新失败后重试间隔
1W ; 过期时长
3H ) ; 否定记录保存时长
资源类型:A(IPv4), AAAA(IPv6):定义FQDN的IP
NS : 定义DNS服务器的FQDN
SOA : 起始授权(每个zone首先要定义此值)
MX: 定义邮件记录,有优先级概念(0-99),值越小优先级越高。
CNAME: 定义别名
PTR: 反向记录
单台DNS主服务器应用实验之正向解析:
查看bind版本:
[root@localhost ~]# rpm -q bind
bind-9.8.2-0.62.rc1.el6_9.4.x86_64
如果没有则安装:
#yum install -y bind bind-utils
实验环境
系统:CentOS release 6.8
软件:bind-9.8.2-0.62.rc1.el6_9.4.x86_64
服务器: IP 192.168.153.130;netmask 255.255.255.0 ;DNS 192.168.153.130;GW 192.168.153.2
我在192.168.153.130上面装的dns服务,resolv.conf 中dns的地址配置第一行为本机ip地址,在后边配置文件中的192.168.153.129为我的nginx服务器地址,nginx服务器的dns指向为192.168.153.130,这个在nginx主机nslookup时就可已用130的dns做解析了.
1.配置DNS服务器name.conf
options {
listen-on port 53 { 127.0.0.1;
192.168.153.130; // 为局域网其它机器提供Named服务,必须监听向本机IP发出的请求.
};
listen-on-v6 port 53 { ::1; }; //如不使用IPv6地址,可以删除或注释掉
directory "/var/named";
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 { any; }; //更改为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";
};
logging {
channel default_debug {
file "data/named.run";
severity dynamic;
};
};
zone "." IN {
type hint;
file "named.ca";
};
zone "music.com" IN{ //定义一个zone,zone的名字“music.com”
type master; //类型为主服务器
file "music.com.zone"; //自定义的域名到IP的正向解析配置
};
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";
2.开始建立正向解析文件:
创建并编辑正向解析文件music.com.zone(文件名要和name.conf文件中定义的zone file名一致)
#vim /var/named/music.com.zone
$TTL 1D
@ IN SOA master.music.com. email.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
IN NS master
master IN A 192.168.153.130
www IN A 192.168.153.129
nginx IN A 192.168.153.129
bbs IN A 192.168.153.129
3.配置完成后,检查配置文件的正确性:
[root@localhost ~]# /usr/sbin/named-checkconf -z
zone music.com/IN: loaded serial 0
zone 153.168.192.in-addr.arpa/IN: loaded serial 2010110901
zone localhost.localdomain/IN: loaded serial 0
zone localhost/IN: loaded serial 0
zone 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa/IN: loaded serial 0
zone 1.0.0.127.in-addr.arpa/IN: loaded serial 0
zone 0.in-addr.arpa/IN: loaded serial 0
4.重启服务:
[root@localhost ~]# /sbin/service named restart
Stopping named: .[ OK ]
Starting named: [ OK ]
5.nslookup解析测试:
[root@localhost ~]# nslookup
> master.music.com
Server: 192.168.153.130 //dns地址
Address: 192.168.153.130#53
Name: master.music.com //域名服务器主机名
Address: 192.168.153.130
> www.music.com
Server: 192.168.153.130
Address: 192.168.153.130#53
Name: www.music.com
Address: 192.168.153.129 //www域名对应的A记录地址
> nginx.music.com
Server: 192.168.153.130
Address: 192.168.153.130#53
Name: nginx.music.com
Address: 192.168.153.129
> bbs.music.com
Server: 192.168.153.130
Address: 192.168.153.130#53
Name: bbs.music.com
Address: 192.168.153.129
>
单台DNS主服务器应用实验之反向解析:
1.配置主区域数据文件(/etc/named.conf),追加反向解析如下内容:
zone "153.168.192.in-addr.arpa" IN{ type master; file "192.168.153.arpa.zone"; allow-update { none; }; };
2.配置解析数据文件.
[root@localhost ~]# cd /var/named/
[root@localhost named]# vim 192.168.153.arpa.zone
$TTL 1D
@ IN SOA 153.168.192.in-addr.arpa. music.com. (
2010110901
28800
14400
3600000
86400
)
@ IN NS master.music.com.
130 IN PTR master.music.com 192.168.153.130 -> master
129 IN PTR www.music.com. ; 192.168.153.129 -> www
129 IN PTR nginx.music.com. ; 192.168.153.129 -> nginx
129 IN PTR img.music.com. ; 192.168.153.129 -> img
3.语法检查:
[root@localhost ~]# /usr/sbin/named-checkconf -z
4.重启服务:
[root@localhost ~]# /etc/init.d/named restart
Stopping named: [ OK ]
Starting named: [ OK ]
5.反向解析验证:
[root@localhost ~]# nslookup
> 192.168.153.129
Server: 192.168.153.130
Address: 192.168.153.130#53
129.153.168.192.in-addr.arpa name = www.music.com.
129.153.168.192.in-addr.arpa name = nginx.music.com.
129.153.168.192.in-addr.arpa name = img.music.com.
>
> 192.168.153.130
Server: 192.168.153.130
Address: 192.168.153.130#53
130.153.168.192.in-addr.arpa name = master.music.com.153.168.192.in-addr.arpa.
>
DNS CNAME记录
CNAME记录,即别名记录,我们通过设置别名记录,可以将多个名称指向同一台主机,CNAME记录的前提是必须要有一条A记录,A记录是创建CNAME记录的前提.
这样可以在ip变动的情况下,我们不用一个一个的去更改主机名到主机的A记录映射,只需要改动别名到主机的一条A记录就可以全部搞定,达到事倍功半的效果.
未使用别名(CNAME)的正向解析区域配置文件:
[root@localhost named]# vim music.com.zone $TTL 1D @ IN SOA master.music.com. email.com. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum IN NS master master IN A 192.168.153.130 www IN A 192.168.153.129 nginx IN A 192.168.153.129 bbs IN A 192.168.153.129 img IN A 192.168.153.129
使用别名后的区域配置文件.
[root@localhost named]# vim music.com.zone $TTL 1D @ IN SOA master.music.com. email.com. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum IN NS master master IN A 192.168.153.130 proxy IN A 192.168.153.129 www IN CNAME proxy nginx IN CNAME proxy bbs IN CNAME proxy img IN CNAME proxy
检查并重启服务,nslookup测试验证:
[root@localhost ~]# nslookup
> img.music.com
Server: 192.168.153.130
Address: 192.168.153.130#53
img.music.com canonical name = proxy.music.com. //别名
Name: proxy.music.com
Address: 192.168.153.129
> www.music.com
Server: 192.168.153.130
Address: 192.168.153.130#53
www.music.com canonical name = proxy.music.com.
Name: proxy.music.com
Address: 192.168.153.129
>
参考文档:
https://www.cnblogs.com/zydev/p/6293745.html
http://leitelyaya.iteye.com/blog/808266