DNS搭建

第一步,安装软件包

DNS软件包默认是没有安装的,首先我们就要安装这个软件包

[root@localhost ~]# yum -y install bindbind-chroot caching-nameserver

DNS的守护进程 /usr/sbin/named /usr/sbin/rndc

DNS的脚本 /etc/init.d/named

DNS的端口 53 953(tcp udp)

配置文件 /var/named/chroot/etc/named.conf(主配置文件) /var/named/chroot/*

语法检查 Service named configtest

日志 /var/log/messages


第二步,复制模板文件

[root@localhost~]# cd /var/named/chroot/etc #由于安装了chroot环境

[root@localhostetc]# cp -p named.caching-nameserver.conf named.conf

第三步,编辑named.conf文件

[root@localhostetc]# vim named.conf

options { #代表全局配置

listen-onport 53 { any; }; #DNS服务监听在所有接口

listen-on-v6 port 53 { ::1; };

directory "/var/named"; # zone文件的存放目录,chroot环境

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";

// Those options should be used carefully because they disable port

// randomization

// query-source port 53;

// query-source-v6 port 53;

allow-query { 192.168.0.0/24; }; #允许查询的客户端 allow-query-cache { any; }; #允许那些客户端来查询缓存

};

logging {

channeldefault_debug {

file "data/named.run"; #定义日志的存放位置

severity dynamic;

};

};

view localhost_resolver { #定义视图的功能

match-clients {192.168.0.0/24; }; #匹配的客户端

match-destinations { any; }; #匹配的目标

recursion yes;

include "/etc/named.rfc1912.zones";

};

第四步,定义zone文件。(编辑named.rfc1912.zones文件)

[root@localhost etc]#

[root@localhost etc]# vim named.rfc1912.zones

zone "." IN { # 根区域

type hint;

file "named.ca"; #全球的13台根服务器

};

zone "example.com" IN { # 定义正向解析的区域

type master; #区域的类型为主要的

file "example.zone"; #DNS的正向解析数据库

allow-update { none; }; #默认情况下,是否允许客户端自动更新

}

zone "0.168.192.in-addr.arpa" IN { # 定义反向解析的区域

type master;

file "named.example"; #DNS反向解析的数据库

allow-update { none; };

};

第五步,复制模板文件成数据库文件

[root@localhost~]# cd /var/named/chroot/var/named/

[root@localhostnamed]# cp -p localhost.zone example.zone

[root@localhost named]# cp -p named.localnamed.example

第六步,定义数据库文件

1. 定义正向解析数据库文件

[root@localhost named]#

[root@localhost named]# vim example.zone

$TTL 86400 #最小存活时间

@ IN SOA server1.example.com. root.exmaple.com. (

# SOA,初始授权记录,仅允许存在一个SOA记录,@代表域本身,server1.example.com DNS服务器名,root.example.com代表管理员邮箱

2010022101 ;serial (d. adams)

3H ; refresh

15M ;retry

1W ;expiry

1D) ;minimum

@IN NS server1.example.com.

# 一笔NS记录,指定nameserver为server1.example.com至少要有一笔NS记录

server1 IN A 192.168.0.254

# 指定server1的ip地址为192.168.0.254,主机A记录

station10 INA 192.168.0.10

web INCNAME server1.example.com.

# CNAME 别名解析记录就是为server1.example.com. 添加一个别名,通过这个别名也可以访问。前提是www.example.com. 这个域名在DNS要解析的到

正向解析的数据库就完成了,下面定义反向解析的数据库。

* IN A 192.168.0.254

# 泛域名解析记录就是匹配所有,但是这个解析最好要放在数据库文件的最后面,因为读取数据库文件的时候是从上往下开始读取的,当上面全部都不匹配的时候,才会去读取泛域名解析记录。

@ INMX 10 mail.example.com.

# MX记录邮件交换记录当有客户端向example.com这个域发邮件,那么客户端怎么会指定将邮件发给example.com域下面的mail.example.com.,就是因为这个MX记录定义的。Mail.example.com就是example.com这个域的邮件服务器。

总结:在DNS中的资源记录类型有那些

(1) SOA资源记录

每个数据库文件按的开始处都包含了一个起始授权记录(Start ofAuthority Record),简称SOA记录。SOA定义了域的全局参数,进行整个域的管理设置。一个区域文件只允许存在唯一的SOA记录。

(2) NS资源记录

名称服务器(NS)资源记录表示该区的授权服务器,它们表示SOA资源记录中指定的该区的主和辅助服务器,也表示了任何授权区的服务器。每个区在区根处至少包含一个NS记录。

(3) A资源记录

地址(A)资源记录把FQDN映射到IP地址,因而解析器能查询FQDN对应的IP地址。

(4) PTR资源记录

相对于A资源记录,指针(PTR)记录把IP地址映射到FQDN。

(5) CNAME资源记录

规范名字(CNAME)资源记录创建特定FQDN的别名。用户可以通过定义的CANME记录中的别名来访问

(6) MX资源记录

邮件交换(MX)资源记录为DNS域名指定邮件交换服务器。邮件交换服务器是为DNS域名处理或转发邮件的主机。处理邮件指把邮件投递到目的地或转交另一不同类型的邮件传送者。转发邮件指把邮件发送到最终目的服务器。

(7) 泛域名解析记录

除了在数据库文件中定义的资源记录以为,其他的所有域名都可以被DNS所解析出来。


2. 定义反向解析数据库

[root@localhostnamed]# vim named.example

$TTL 86400

@ IN SOA server1.example.com. root.example.com. (

2010022101 ; Serial

28800 ; Refresh

14400 ; Retry

3600000 ;Expire

86400 ) ; Minimum

@ INNS server1.example.com.

254IN PTR server1.example.com.

# 主机A记录反解

10 IN PTR station10.example.com.

20 IN PTR station20.example.com.

30 IN PTR station30.example.com.

40 IN PTR station40.example.com.

50 IN PTR station50.example.com.

[root@localhost~]# service named restart

Stopping named: [ OK ]

Starting named: [ OK ]

在客户端来指定DNS Server,/etc/resolv.conf这个文件中指定。

[root@localhost~]# vim /etc/resolv.conf #nslookup测试正反解

search example.com

nameserver 192.168.0.254

辅助DNS服务

第一步,安装软件包

[root@localhost ~]# yum -y install bindbind-chroot caching-nameserver

第二步,复制模板文件

[root@localhostetc]# cp -p named.caching-nameserver.conf named.conf

第三步,编辑named.conf文件

[root@localhostetc]# vim named.conf

第四步,定义zone文件。(编辑named.rfc1912.zones文件)

[root@localhostetc]# vim named.rfc1912.zones

zone "example.com" IN {

type slave; #类型改为 slave

masters { 192.168.0.254; }; #指定主DNS服务器地址

file "slaves/example.com"; #注意,是slaves目录下

};

zone "0.168.192.in-addr.arpa" IN {

type slave;

masters { 192.168.0.254; };

file "slaves/named.example";

};

[root@localhost~]# service named restart

Stopping named: [ OK ]

Starting named: [ OK ]

[root@localhost~]# tail /var/log/messages

Feb 21 18:23:00 localhost named[7394]: zoneexample.com/IN/localhost_resolver: Transfer started.

Feb 21 18:23:00 localhost named[7394]:transfer of 'example.com/IN' from 192.168.0.254#53: connected using 192.168.0.10#55165

Feb 21 18:23:00 localhost named[7394]: zoneexample.com/IN/localhost_resolver: transferred serial 2010022101

Feb 21 18:23:00 localhost named[7394]:transfer of 'example.com/IN' from 192.168.0.254#53: end of transfer

[root@localhost ~]#

在日志里面可以看到,主DNS与辅助DNS正在同步序列号,同步成功,这个日志里面的信息非常的详细。接下来,我们在到slaves目录下面去看看,

[root@localhost named]# cd slaves/

[root@localhost slaves]# ll

total 8

-rw-r--r-- 1 named named 461 Feb 21 18:23example.com

-rw-r--r-- 1 named named 531 Feb 21 18:23named.example

刚才slaves目录下面的是什么东西都没有的,现在就多了两个文件,example.com和named.example这个两个文件。这个就是我们刚才在定义zone文件的时候在slaves目录下面定义的,文件名是随意写的,这个没有关系,但是里面东西是和主DNS一样的。

我们打开这个两个文件来看一下

[root@localhostslaves]# vim example.com

$ORIGIN .

$TTL86400 ; 1 day

example.com INSOAserver1.example.com. root.exmaple.com.(

2010022101 ; serial

10800 ; refresh (3 hours)

900 ; retry (15 minutes)

604800 ; expire (1week)

86400 ; minimum(1 day)

)

NS server1.example.com.

$ORIGINexample.com.

server1 A 192.168.0.254

[root@localhostslaves]# vim example.com

$ORIGIN .

$TTL86400 ; 1 day

0.168.192.in-addr.arpa INSOAserver1.example.com. root.example.com. (

2010022101 ; serial

28800 ; refresh (8 hours)

14400 ; retry (4 hours)

3600000 ;expire (5 weeks 6 days 16 hours)

86400 ;minimum (1 day)

)

NS server1.example.com.

$ORIGIN 0.168.192.in-addr.arpa.

10 PTR station10.example.com.










DNS服务器的其他知识

1、递归查询:一般在客户端与服务器之间的查询叫做递归查询。

迭代查询:服务器与服务器之间的查询叫做迭代查询。

2、dns客户端配置

/etc/hosts

/etc/resolv.conf

/etc/nsswitch

3、dns工具 host、nslookup、dig

4、view

5、访问控制

allow-query #允许查询,查询权威的记录和缓存的记录

allow-query-cache # 允许查询缓存

allow-recursion # 允许递归查询,先查权威记录,然后去其他DNS查

allow-transfer # 主辅同步

forward only # 如果没有,则停止,返回客户端无结果

forward first #如果没有,去其他服务器查询

6、rndc 管理工具