下面具体来搭建DNS服务
第一步,安装软件包
DNS软件包默认是没有安装的,首先我们就要安装这个软件包
[root@localhost ~]# yum -y install bind bind-chroot caching-nameserver
Loaded plugins: rhnplugin
This system is not registered with RHN.
RHN support will be disabled.
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package bind.i386 30:9.3.6-4.P1.el5 set to be updated
---> Package bind-chroot.i386 30:9.3.6-4.P1.el5 set to be updated
---> Package caching-nameserver.i386 30:9.3.6-4.P1.el5 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
=================================================================================================
Package Arch Version Repository Size
=================================================================================================
Installing:
bind i386 30:9.3.6-4.P1.el5 Server 978 k
bind-chroot i386 30:9.3.6-4.P1.el5 Server 44 k
caching-nameserver i386 30:9.3.6-4.P1.el5 Server 60 k
Total download size: 1.1 M
Downloading Packages:
(1/3): bind-9.3.6-4.P1.el5.i386.rpm | 978 kB 00:00
(2/3): bind-chroot-9.3.6-4.P1.el5.i386.rpm | 44 kB 00:00
(3/3): caching-nameserver-9.3.6-4.P1.el5.i386.rpm | 60 kB 00:00
-------------------------------------------------------------------------------------------------
Total 1.3 MB/s | 1.1 MB 00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : bind 1/3
Installing : caching-nameserver 2/3
Installing : bind-chroot 3/3
Installed:
bind.i386 30:9.3.6-4.P1.el5 bind-chroot.i386 30:9.3.6-4.P1.el5
caching-nameserver.i386 30:9.3.6-4.P1.el5
Complete!
DNS的所有软件包就安装成功了。
第二步,复制模板文件
由于安装了chroot环境,所以我们的DNS主配置文件应该在/var/named/chroot/etc目录下面
[root@localhost ~]# cd /var/named/chroot/
[root@localhost chroot]# ls
dev etc var
[root@localhost chroot]# cd etc/
[root@localhost etc]# ls
localtime named.caching-nameserver.conf named.rfc1912.zones rndc.key
[root@localhost etc]#
打开named.caching-nameserver.conf文件来看看,
//
// named.caching-nameserver.conf
//
// Provided by Red Hat caching-nameserver package to configure the
// ISC BIND named(8) DNS server as a caching only nameserver
// (as a localhost DNS resolver only).
//
// See /usr/share/doc/bind*/sample/ for example named configuration files.
//
// DO NOT EDIT THIS FILE - use system-config-bind or an editor
// to create named.conf - edits to this file will be lost on
// caching-nameserver package upgrade.
//
这个文件告诉我们不要直接的编辑这个文件,去创建一个named.conf文件,然后编辑named.conf文件,当有了named.conf,将不在读取这个文件。
现在就将named.caching-nameserver.conf文件复制成named.conf文件。
[root@localhost etc]# ls
localtime named.caching-nameserver.conf named.rfc1912.zones rndc.key
[root@localhost etc]# cp -p named.caching-nameserver.conf named.conf
[root@localhost etc]# ls
localtime named.conf rndc.key
named.caching-nameserver.conf named.rfc1912.zones
[root@localhost etc]#
可以看到,named.conf文件就被创建成功了。最好在copy的时候加上-P的参数,保留权限。否则启动服务的时候会报权限拒绝的。
第三步,编辑named.conf文件
[root@localhost etc]#
[root@localhost etc]# vim named.conf
options {
listen-on port 53 { any; };
listen-on-v6 port 53 { ::1; };
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";
// Those options should be used carefully because they disable port
// randomization
// query-source port 53;
// query-source-v6 port 53;
第四步,定义zone文件。(编辑named.rfc1912.zones文件)
[root@localhost etc]#
[root@localhost etc]# vim named.rfc1912.zones
zone "." IN {
type hint;
file "named.ca";
};
zone "example.com" IN {
type master;
file "example.zone";
allow-update { none; };
};
zone "0.168.192.in-addr.arpa" IN {
type master;
file "named.example";
allow-update { none; };
};
解释这些语法参数的意思
Zone “.” 根区域
Zone “example.com” 定义正向解析的区域
zone "0.168.192.in-addr.arpa" 定义反向解析的区域
IN Internet记录
type hint 根区域的类型为hint
type master 区域的类型为主要的
file “named.ca” ; 区域文件是named,ca
file "example.zone"; 指定正向解析的区域文件是example.com
file "named.example"; 指定反向解析的区域文件是named,example
allow-update { none; }; 默认情况下,是否允许客户端自动更新
在named.ca文件中就定义了全球的13台根服务器,
在example.com文件中就定义DNS的正向解析数据库
在named.example文件中就定义DNS反向解析的数据库
定义zone文件就完成了,下面来编辑DNS的数据库文件。
第六步,定义数据库文件
1. 定义正向解析数据库文件
[root@localhost named]#
[root@localhost named]# vim example.zone
$TTL 86400
@ IN SOA server1.example.com. root.exmaple.com. (
2010022101 ; serial (d. adams)
3H ; refresh
15M ; retry
1W ; expiry
1D ) ; minimum
@ IN NS server1.example.com.
server1 IN A 192.168.0.254
station10 IN A 192.168.0.10
station20 IN A 192.168.0.20
station30 IN A 192.168.0.30
station40 IN A 192.168.0.40
station50 IN A 192.168.0.50
关于正向解析数据库中每一行参数的解释
$TTL 86400
最小的存活的时间是86400S(24H)
@ IN SOA server1.example.com. root.exmaple.com. (
这是一笔SOA记录,只允许存在一个SOA记录
@是代表要解析的这个域本身(example.com)
IN是Internet记录。
SOA 是初始授权记录,指定网络中第一台DNS Server。
Server1.example.com是DNS服务器的名字。
Root.example.com是指管理员的邮箱。
2010022101 ; serial (d. adams)
3H ; refresh
15M ; retry
1W ; expiry
1D ) ; minimum
这些部分主要是用来主DNS和辅助DNS做同步用的
2010022101 序列号,当主DNS数据改变时,这个序列号就要被增加1,而辅助DNS通过序列号来和主DNS同步。
3H 刷新,主DNS和辅助DNS每隔三小时同步一次。
15M 重试,3H之内,没有同步,每隔15M在尝试同步
1W 过期,1W之内,还没有同步,就不同步了
1D 生存期,没有这条记录,缓存的时间。
@ IN NS server1.example.com.
这是一笔NS记录,指定nameserver为server1.example.com
至少要有一笔NS记录
server1 IN A 192.168.0.254
指定server1的ip地址为192.168.0.254
station10 IN A 192.168.0.10
指定station10的ip地址为192.168.0.10
IN Internet NS nameserver A address
正向解析的数据库就完成了,下面定义反向解析的数据库。
1. 定义反向解析数据库
[root@localhost named]#
[root@localhost named]# vim named.example
$TTL 86400
@ IN SOA server1.example.com. root.example.com. (
2010022101 ; Serial
28800 ; Refresh
14400 ; Retry
3600000 ; Expire
86400 ) ; Minimum
@ IN NS server1.example.com.
254 IN PTR server1.example.com.
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.
其实反向解析的数据库文件的配置和正向解析的差不多,只需要将ip地址和域名换一个位置就可以了,把A换成PTR就ok了。
DNS的基本配置就完成了,在来看看DNS是否能够正常工作。
我们先重启一下DNS服务
[root@localhost ~]#
[root@localhost ~]# service named restart
Stopping named: [ OK ]
Starting named: [ OK ]
[root@localhost ~]#
可以看到,DNS服务启动成功了。
在查询以前,要在客户端来指定DNS Server,在/etc/resolv.conf这个文件中指定。
[root@localhost ~]#
[root@localhost ~]# vim /etc/resolv.conf
search example.com
nameserver 192.168.0.254
~
再来使用nslookup工具来查询一下
[root@localhost ~]#
[root@localhost ~]# nslookup
> server1.example.com
Server: 192.168.0.254
Address: 192.168.0.254#53