DNS 全称domain name system域名系统的缩写,它是一种将IP地址转换成对应的主机名或者将主机名转换成对应的IP的一种服务机制。其中通过域名解析出IP叫做正向解析,通过IP解析出域名叫做反向解析。DNS使用TCP和UDP协议,端口号都是53,但它主要使用UDP,服务器之间备份使用TCP。全世界只有13台“根”服务器,一个主根服务器放在美国,其他12台为辅根服务器,DNS服务器根据角色可分为:主DNS,从DNS,缓存DNS服务器,DNS转发服务器。
首先来看看域名的结构组成,我们平时访问网站的时候,都会用一个域名去请求,比如,其实.com后面还有一个点,这个点叫做根域。下图是一个域名的树状结构,根域下面会有.com,.cn,.net等顶级域,顶级域下面又有二级域名,比如baidu.com或者.com.cn等,而则为子域,我们经常用子域来做网站的域名。
早期的域名到IP的映射是保存在hosts文件中的:
[root@cp1 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
DNS解析过程:先在本地/etc/hosts文件中查找解析,如果找到直接使用;如果找不到,则进行下图所示的解析过程:
查看DNS配置文件的内容:
[root@cp1 ~]# cat /etc/resolv.conf
; generated by /sbin/dhclient-script
search localdomain
nameserver 192.168.147.2
我们使用bind来搭建DNS服务,
首先安装bind:
[root@cp1 ~]# yum install -y bind
配置文件说明:
主配置文件:/etc/named.conf
(监听的端口53,IP127.0.0.1)
域服务器配置文件:/var/named/named.ca
本地主机配置文件:/var/named/named.localhost
反解析配置文件:/var/named/named.loopback
启动DNS服务named:
[root@cp1 ~]# /etc/init.d/named start
Generating /etc/rndc.key: [确定]
启动 named: [确定]
测试1:正向解析
测试使用@127.0.0.1解析localhost:
[root@cp1 ~]# dig @127.0.0.1 localhost
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.47.rc1.el6_8.4 <<>> @127.0.0.1 localhost
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 11354
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1
;; QUESTION SECTION:
;localhost. IN A
;; ANSWER SECTION:
localhost. 86400 IN A 127.0.0.1
;; AUTHORITY SECTION:
localhost. 86400 IN NS localhost.
;; ADDITIONAL SECTION:
localhost. 86400 IN AAAA ::1
;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Mon Mar 13 22:31:29 2017
;; MSG SIZE rcvd: 85
测试使用@127.0.0.1解析localhost.localdomain:
[root@cp1 ~]# dig @127.0.0.1 localhost.localdomain
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.47.rc1.el6_8.4 <<>> @127.0.0.1 localhost.localdomain
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 26777
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1
;; QUESTION SECTION:
;localhost.localdomain. IN A
;; ANSWER SECTION:
localhost.localdomain. 86400 IN A 127.0.0.1
;; AUTHORITY SECTION:
localhost.localdomain. 86400 IN NS localhost.localdomain.
;; ADDITIONAL SECTION:
localhost.localdomain. 86400 IN AAAA ::1
;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Mon Mar 13 22:32:30 2017
;; MSG SIZE rcvd: 97
测试结果发现,将localhost和localhost.localdomain都解析到了127.0.0.1。
测试2:反向解析
反解析配置文件:/var/named/named.loopback
测试使用@127.0.0.1反解析127.0.0.1:
[root@cp1 ~]# dig @127.0.0.1 -x 127.0.0.1
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.47.rc1.el6_8.4 <<>> @127.0.0.1 -x 127.0.0.1
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 25857
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2
;; QUESTION SECTION:
;1.0.0.127.in-addr.arpa. IN PTR
;; ANSWER SECTION:
1.0.0.127.in-addr.arpa. 86400 IN PTR localhost.
;; AUTHORITY SECTION:
1.0.0.127.in-addr.arpa. 86400 IN NS 1.0.0.127.in-addr.arpa.
;; ADDITIONAL SECTION:
1.0.0.127.in-addr.arpa. 86400 IN A 127.0.0.1
1.0.0.127.in-addr.arpa. 86400 IN AAAA ::1
;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Mon Mar 13 22:33:43 2017
;; MSG SIZE rcvd: 121
测试结果发现,将127.0.0.1反解析到了localhost。
配置一个正向解析的域:123.com
编辑主配置文件:
[root@cp1 ~]# vim /etc/named.conf
在最后面添加一个域“123.com”,格式参照zone "."格式:
zone "123.com" IN {
type master;
file "123.com.zone";
};
检查主配置文件是否有错:
[root@cp1 ~]# named-checkconf
编辑123.com.zone文件:
[root@cp1 ~]# vim /var/named/123.com.zone
内容如下,格式参考/var/named/named.localhost:
$TTL 1D
@ IN SOA @ admin.123.com. (
2015010901 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
IN NS ns.123.com.
IN MX 5 mail.123.com.
mail IN A 192.168.134.129
ns IN A 192.168.134.128
www IN A 11.11.11.11
bbs IN CNAME www
检查域配置文件是否有错:
[root@cp1 ~]# named-checkzone "123.com" /var/named/123.com.zone
zone 123.com/IN: loaded serial 2015010901
OK
再次编辑主配置文件,
[root@cp1 ~]# vim /etc/named.conf
加入监听DNS的IP192.168.147.137
listen-on port 53 { 127.0.0.1;192.168.147.137; };
重启:
[root@cp1 ~]# /etc/init.d/named restart