DNS是域名系统(Domain Name System)的缩写,是因特网的一项核心服务,它能够提供域名与IP地址之间对应关系的转换服务。这样我们就可以更方便地去访问互联网了,不用去记住那一串IP数字。本文档主要是说明如何把一台Centos主机配置成一个DNS服务器,以便能提供域名解析服务。
操作系统:Centos 6.5
IP地址:10.1.15.104
测试域名:qiaohh.com
作用:主要提供解析qiaohh.com域名的服务
(1)先运行命令rpm -qa | grep bind 检查DNS服务器软件的安装情况
[root@qiaohh ~]# rpm -qa | grep bind
samba-winbind-clients-3.6.9-164.el6.x86_64
bind-libs-9.8.2-0.17.rc1.el6_4.6.x86_64
PackageKit-device-rebind-0.5.8-21.el6.x86_64
ypbind-1.20.4-30.el6.x86_64
bind-utils-9.8.2-0.17.rc1.el6_4.6.x86_64
samba-winbind-3.6.9-164.el6.x86_64
rpcbind-0.2.0-11.el6.x86_64
(2)运行命令yum install –y bind bind-chroot 安装bind 程序
出现Complete!说明安装成功
(3)此时再运行命令rpm –qa | grep bind 检查DNS服务器软件的安装情况
[root@qiaohh ~]# rpm -qa | grep bind
bind-libs-9.8.2-0.62.rc1.el6_9.2.x86_64
bind-utils-9.8.2-0.62.rc1.el6_9.2.x86_64
samba-winbind-clients-3.6.9-164.el6.x86_64
PackageKit-device-rebind-0.5.8-21.el6.x86_64
ypbind-1.20.4-30.el6.x86_64
bind-chroot-9.8.2-0.62.rc1.el6_9.2.x86_64
bind-9.8.2-0.62.rc1.el6_9.2.x86_64
samba-winbind-3.6.9-164.el6.x86_64
rpcbind-0.2.0-11.el6.x86_64
也可以运行命令rpm -qai bind | grep “Install Date”查看软件包安装日期
vi /etc/named.conf
//
// named.conf
//
// Provided by Red Hat bind 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.
//
options {
//将下行中的127.0.0.1 修改为any,代表允许监听任何IP地址。
listen-on port 53 { any; }; //开启监听端口53,接受任意IP的连接
listen-on-v6 port 53 { ::1; }; // 支持IP V6
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";
//将下行中的localhost修改为any,代表允许任何主机查询。
allow-query { localhost; }; //允许任意IP查询
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;
};
};
//此文件内定义了全球13台根DNS服务器的IP地址
zone "." IN {
type hint;
file "named.ca";
};
//此文件保存着正向与反向解析的区域信息,非常的重要。
include "/etc/named.rfc1912.zones"; //主要配置文件
include "/etc/named.root.key";
为了避免经常修改主配置文件named.conf而导致DNS服务出错,所以规则的区域信息保存在了/etc/named.crc1912.zones文件中,这个文件用于定于域名与IP地址解析规则保存的文件位置以及区域服务类型等内容,一定要谨慎修改。
配置区域数据信息,正向解析的作用是根基主机名(域名)查找到对应的IP地址,区域文件中已有一些默认的信息,可不必理会,直接在下面追加即可:
[root@qiaohh ~]# vi /etc/named.rfc1912.zones
// named.rfc1912.zones:
//
// Provided by Red Hat caching-nameserver package
//
// ISC BIND named zone configuration for zones recommended by
// RFC 1912 section 4.1 : localhost TLDs and address zones
// and http://www.ietf.org/internet-drafts/draft-ietf-dnsop-default-local-zones-02.txt
// (c)2007 R W Franks
//
// See /usr/share/doc/bind*/sample/ for example named configuration files.
//
zone "localhost.localdomain" IN {
type master;
file "named.localhost";
allow-update { none; };
};
zone "localhost" IN {
type master;
file "named.localhost";
allow-update { none; };
};
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 {
type master;
file "named.loopback";
allow-update { none; };
};
zone "1.0.0.127.in-addr.arpa" IN {
type master;
file "named.loopback";
allow-update { none; };
};
zone "0.in-addr.arpa" IN {
type master;
file "named.empty";
allow-update { none; };
};
//qiaohh.com的正向区域
zone "qiaohh.com" IN {
type master;
file "qiaohh.com.zone";
allow-update { none; };
};
//qiaohh.com的反向区域
zone "15.1.10.in-addr.arpa" IN {
type master;
file "10.1.15.arpa";
allow-update {none;};
};
这里需要注意的是,添加反向区域时,网络号要反过来写(网络好是IP地址与子网掩码进行与操作后的结果)。例如我现在配置的网络是10.1.15这个网段,那么它的反向区域是1.16.172.in-addr.arpa。区域里面的file字段表明解析时的数据来源文件,接下来我们去创建named.qiaohh.com和10.1.15.arpa文件。
在配置named.conf时,指明的资源文件目录是/var/named,故先进入该目录
配置解析数据信息,我们可以直接复制正向解析模板文件:“/var/named/named.localhost”,填写信息后即可直接使用。
(1)cd /var/named切换工作目录到bind(named)数据目:
(2)查看区域数据文件的权限:
[root@qiaohh named]# ls -al named.localhost
-rw-r-----. 1 root named 152 6月 21 2007 named.localhost
(3)执行cp命令时加入-a,,代表连通复制原来文件的属性、所有者、组等信息:
cp -a named.localhost qiaohh.com.zone
(4)编辑qiaohh.com.zone域名的区域数据文件:
修改后的配置如下:
[root@qiaohh named]# cat qiaohh.com.zone
$TTL 1D //生存时间为1天
@ IN SOA qiaohh.com. rname.invalid. (
0 ; serial //更新序列号
1D ; refresh //更新时间
1H ; retry //重试时间
1W ; expire //失效时间
3H ) ; minimum //无效解析结论的缓存时间
NS @
A 127.0.0.1
AAAA ::1
IN MX 10 mail.qiaohh.com
mail IN A 10.1.15.100
www IN A 10.1.15.150
bbs IN A 10.1.15.200
//留意qiaohh.com后面的那个不起眼的点(.)
[root@qiaohh named]# cat 10.1.15.arpa
$TTL 1D
@ IN SOA qiaohh.com. rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
AAAA ::1
100 PTR mail.qiaohh.com.
150 PTR www.qiaohh.com.
200 PTR bbs.qiaohh.com.
//留意qiaohh.com后面的那个不起眼的点(.)
[root@qiaohh ~]# service named start
Generating /etc/rndc.key:[确定]
启动 named:[确定]
至此,DNS服务器端的配置已完成,下面我们稍微配置一下客户端测试我们的DNS服务器是否正常。
操作系统:windows和Linux(10.1.15.50)都可以,
IP地址:能够ping 通DNS服务器的IP(10.1.15.104)都可以
作用:测试DNS服务器是否正常
yum install bind-utils
[root@mail ~]# cat /etc/resolv.conf
nameserver 10.1.15.104
resolv.conf文件中可能会有多个nameserver,必须把我们的DNS服务器放在所有nameserver的最前面,这样当需要解析域名时,第一个使用的就是我们配置的DNS服务器,其他的都是候选项。
如果是windosw客户端,需要在本地连接属性里面修改DNS为10.1.15.104
如果服务器本身可以解析,其他客户端无法解析,需要关闭DNS服务器的防火墙
[root@qiaohh ~]# service iptables stop
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:清除防火墙规则:[确定]
iptables:正在卸载模块:[确定]
(1)Linux系统客户端10.1.15.50
[root@mail ~]# nslookup
> mail.qiaohh.com
Server: 10.1.15.104
Address: 10.1.15.104#53
Name: mail.qiaohh.com
Address: 10.1.15.100
> www.qiaohh.com
Server: 10.1.15.104
Address: 10.1.15.104#53
Name: www.qiaohh.com
Address: 10.1.15.150
> bbs.qiaohh.com
Server: 10.1.15.104
Address: 10.1.15.104#53
Name: bbs.qiaohh.com
Address: 10.1.15.200
>
(2)Windows客户端10.1.15.101
C:\Users\Administrator>nslookup
默认服务器: UnKnown
Address: 10.1.15.104
> mail.qiaohh.com
服务器: UnKnown
Address: 10.1.15.104
名称: mail.qiaohh.com
Address: 10.1.15.100
> www.qiaohh.com
服务器: UnKnown
Address: 10.1.15.104
名称: www.qiaohh.com
Address: 10.1.15.150
> bbs.qiaohh.com
服务器: UnKnown
Address: 10.1.15.104
名称: bbs.qiaohh.com
Address: 10.1.15.200
>
>
>
>
从结果可以看到,我们配置的三个域名都能成功解析,并且DNS服务器就是我们配置的那个服务器。
(1)Linux系统10.1.15.50
[root@mail ~]# nslookup
> 10.1.15.100
Server: 10.1.15.104
Address: 10.1.15.104#53
100.15.1.10.in-addr.arpa name = mail.qiaohh.com.
> 10.1.15.150
Server: 10.1.15.104
Address: 10.1.15.104#53
150.15.1.10.in-addr.arpa name = www.qiaohh.com.
> 10.1.15.200
Server: 10.1.15.104
Address: 10.1.15.104#53
200.15.1.10.in-addr.arpa name = bbs.qiaohh.com.
>
(2) windows系统10.1.15.101
C:\Users\Administrator>nslookup
默认服务器: UnKnown
Address: 10.1.15.104
> 10.1.15.100
服务器: UnKnown
Address: 10.1.15.104
名称: mail.qiaohh.com
Address: 10.1.15.100
> 10.1.15.150
服务器: UnKnown
Address: 10.1.15.104
名称: www.qiaohh.com
Address: 10.1.15.150
> 10.1.15.200
服务器: UnKnown
Address: 10.1.15.104
名称: bbs.qiaohh.com
Address: 10.1.15.200
从结果来看,可以正确解析我们的IP地址,并且DNS服务器就是我们配置的那个服务器。
root@mail ~]# dig -t ns qiaohh.com
; <<>> DiG 9.9.4-RedHat-9.9.4-14.el7 <<>> -t ns qiaohh.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 25149
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;qiaohh.com. IN NS
;; ANSWER SECTION:
qiaohh.com. 86400 IN NS qiaohh.com.
;; ADDITIONAL SECTION:
qiaohh.com. 86400 IN A 10.1.15.104
qiaohh.com. 86400 IN AAAA ::1
;; Query time: 1 msec
;; SERVER: 10.1.15.104#53(10.1.15.104)
;; WHEN: Fri Jul 07 17:49:52 CST 2017
;; MSG SIZE rcvd: 97
(1) dig mail.qiaohh.com
[root@mail ~]# dig mail.qiaohh.com
; <<>> DiG 9.9.4-RedHat-9.9.4-14.el7 <<>> mail.qiaohh.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 65263
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;mail.qiaohh.com. IN A
;; ANSWER SECTION:
mail.qiaohh.com. 86400 IN A 10.1.15.100
;; AUTHORITY SECTION:
qiaohh.com. 86400 IN NS qiaohh.com.
;; ADDITIONAL SECTION:
qiaohh.com. 86400 IN A 127.0.0.1
qiaohh.com. 86400 IN AAAA ::1
;; Query time: 0 msec
;; SERVER: 10.1.15.104#53(10.1.15.104)
;; WHEN: Fri Jul 07 17:49:09 CST 2017
;; MSG SIZE rcvd: 118
(2)dig www.qiaohh.com
[root@mail ~]# dig www.qiaohh.com
; <<>> DiG 9.9.4-RedHat-9.9.4-14.el7 <<>> www.qiaohh.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 40150
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;www.qiaohh.com. IN A
;; ANSWER SECTION:
www.qiaohh.com. 86400 IN A 10.1.15.150
;; AUTHORITY SECTION:
qiaohh.com. 86400 IN NS qiaohh.com.
;; ADDITIONAL SECTION:
qiaohh.com. 86400 IN A 127.0.0.1
qiaohh.com. 86400 IN AAAA ::1
;; Query time: 3 msec
;; SERVER: 10.1.15.104#53(10.1.15.104)
;; WHEN: Fri Jul 07 17:47:08 CST 2017
;; MSG SIZE rcvd: 117
(3)dig bbs.qiaohh.com
[root@mail ~]# dig bbs.qiaohh.com
; <<>> DiG 9.9.4-RedHat-9.9.4-14.el7 <<>> bbs.qiaohh.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 48279
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;bbs.qiaohh.com. IN A
;; ANSWER SECTION:
bbs.qiaohh.com. 86400 IN A 10.1.15.200
;; AUTHORITY SECTION:
qiaohh.com. 86400 IN NS qiaohh.com.
;; ADDITIONAL SECTION:
qiaohh.com. 86400 IN A 127.0.0.1
qiaohh.com. 86400 IN AAAA ::1
;; Query time: 1 msec
;; SERVER: 10.1.15.104#53(10.1.15.104)
;; WHEN: Fri Jul 07 17:34:58 CST 2017
;; MSG SIZE rcvd: 117