CentOS下搭建DNS服务器

20 篇文章 1 订阅

DNS是域名系统(Domain Name System)的缩写,是因特网的一项核心服务,它能提供域名与IP地址之间对应关系的转换服务。

这样我们就可以更方便地去访问互联网了,不用去记住那一串IP数字。本文档主要是说明如何把一台CentOS主机配置成一个DNS

服务器,以便能提供域名解析服务。


(1) DNS服务器端配置

操作系统:CentOS 6.4

IP地址:172.16.1.4

DNS软件:Bind 9.8

测试域名:realhostip.com

作用:主要提供解析realhostip.com域名的服务


1. 安装bind

# yum install bind


2. 修改/etc/named.conf配置文件

# vi /etc/named.conf

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. // named.conf  
  3. //  
  4. // Provided by Red Hat bind package to configure the ISC BIND named(8) DNS  
  5. // server as a caching only nameserver (as a localhost DNS resolver only).  
  6. //  
  7. // See /usr/share/doc/bind*/sample/ for example named configuration files.  
  8. //  
  9.   
  10. options {  
  11.         listen-on port 53 { any; }; //开启监听端口53,接受任意IP连接  
  12.         listen-on-v6 port 53 { ::1; };  //支持IP V6  
  13.         directory       "/var/named";   //所有的正向反向区域文件都在这个目录下创建  
  14.         dump-file       "/var/named/data/cache_dump.db";  
  15.         statistics-file "/var/named/data/named_stats.txt";  
  16.         memstatistics-file "/var/named/data/named_mem_stats.txt";  
  17.         allow-query     { 0.0.0.0/0; }; //允许任意IP查询  
  18.         recursion yes;  
  19.   
  20.         dnssec-enable yes;  
  21.         dnssec-validation yes;  
  22.         dnssec-lookaside auto;  
  23.   
  24.         /* Path to ISC DLV key */  
  25.         bindkeys-file "/etc/named.iscdlv.key";  
  26.   
  27.         managed-keys-directory "/var/named/dynamic";  
  28. };  
  29.   
  30. logging {  
  31.         channel default_debug {  
  32.                 file "data/named.run";  
  33.                 severity dynamic;  
  34.         };  
  35. };  
  36.   
  37. zone "." IN {  
  38.         type hint;  
  39.         file "named.ca";  
  40. };  
  41.   
  42. include "/etc/named.rfc1912.zones"; //主要配置文件  
  43. include "/etc/named.root.key";  

3. 修改/etc/named.rfc1912.zones文件,添加realhostip.com的正向和反向区域

# vi /etc/ named.rfc1912.zones

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // named.rfc1912.zones:  
  2. //  
  3. // Provided by Red Hat caching-nameserver package   
  4. //  
  5. // ISC BIND named zone configuration for zones recommended by  
  6. // RFC 1912 section 4.1 : localhost TLDs and address zones  
  7. // and http://www.ietf.org/internet-drafts/draft-ietf-dnsop-default-local-zones-02.txt  
  8. // (c)2007 R W Franks  
  9. //   
  10. // See /usr/share/doc/bind*/sample/ for example named configuration files.  
  11. //  
  12.   
  13. zone "localhost.localdomain" IN {  
  14.         type master;  
  15.         file "named.localhost";  
  16.         allow-update { none; };  
  17. };  
  18.   
  19. zone "localhost" IN {  
  20.         type master;  
  21.         file "named.localhost";  
  22.         allow-update { none; };  
  23. };  
  24.   
  25. 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 {  
  26.         type master;  
  27.         file "named.loopback";  
  28.         allow-update { none; };  
  29. };  
  30.   
  31. zone "1.0.0.127.in-addr.arpa" IN {  
  32.         type master;  
  33.         file "named.loopback";  
  34.         allow-update { none; };  
  35. };  
  36.   
  37. zone "0.in-addr.arpa" IN {  
  38.         type master;  
  39.         file "named.empty";  
  40.         allow-update { none; };  
  41. };  
  42.   
  43. //realhostip.com的正向区域  
  44. zone "realhostip.com" IN {  
  45.         type master;  
  46.         file "named.realhostip.com";  
  47.         allow-update { none; };  
  48. };  
  49. //realhostip.com的反向区域  
  50. zone "1.16.172.in-addr.arpa" IN {  
  51.         type master;  
  52.         file "172.16.1.arpa";  
  53.         allow-update { none; };  
  54. };  

这里需要注意的是,添加反向区域时,网络号要反过来写(网络号是IP地址与子网掩码进行与操作后的结果)。

例如,我现在配置的网络号172.16.1这个网段,那么它的反向区域是1.16.172.in-addr.arpa。区域里面的file

字段表明解析时的数据来源文件,接下来我们去创建named.realhostip.com和172.16.1.arpa文件。


4. 创建正向和反向区域资源文件

在配置named.conf时,指明的资源文件目录是/var/named,故先进入该目录。

# cd /var/named

# vi named.realhostip.com

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. $TTL 1D  
  2. @       IN SOA  realhostip.com. rname.invalid. (  
  3.                                         0       ; serial  
  4.                                         1D      ; refresh  
  5.                                         1H      ; retry  
  6.                                         1W      ; expire  
  7.                                         3H )    ; minimum  
  8.         NS      @  
  9.         A       127.0.0.1  
  10.         AAAA    ::1  
  11. 172-16-1-50 IN A 172.16.1.50  
  12. 172-16-1-51 IN A 172.16.1.51  

以上我添加了两条记录,其中172-16-1-50 IN A 172.16.1.50表明域名172-16-1-50.realhostip.com对应的IP地址为172.16.1.50。

如果需要添加多条,按此类似添加,留意realhostip.com后面的那个不起眼的点(.)。


# vi 172.16.1.arpa

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. $TTL 1D  
  2. @       IN SOA  realhostip.com. rname.invalid. (  
  3.                                         0       ; serial  
  4.                                         1D      ; refresh  
  5.                                         1H      ; retry  
  6.                                         1W      ; expire  
  7.                                         3H )    ; minimum  
  8.         NS      @  
  9.         AAAA    ::1  
  10. 50      PTR     172-16-1-50.realhostip.com.  
  11. 51      PTR     172-16-1-51.realhostip.com.  

以上我也添加了两条记录,其中50     PTR     172-16-1-50.realhostip.com表明IP地址172.16.1.50对应的

域名为172-16-1-50.realhostip.com。如果要添加多条,按此类似添加,留意realhostip.com后面的那个不起眼的点(.)。


5. 启动named服务

#service named start

至此,DNS服务器端的配置已完成,下面我们稍微配置一下客户端来测试我们的DNS服务器是否正常工作。

 

(2) 客户端配置

操作系统:windows和linux都可以,我这里是CentOS 6.4

IP地址:能够ping通DNS服务器的IP(172.16.1.4)都可以,我这里是172.16.1.104

作用:测试DNS服务器是否正常工作。

 

1. 安装bind-utils包,以便能使用nslookup、dig和host工具

yum install bind-utils

 

2. 修改DNS配置使用我们的DNS服务器

vi /etc/resolv.conf

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. nameserver 172.16.1.4  
  2. nameserver 192.168.13.31  
  3. nameserver 172.16.1.1  

如果是windows客户端,需要在本地连接属性里面修改,如下图:




resolv.conf文件中可能会有多个nameserver,必须把我们的DNS服务器放在所有nameserver的最前面,

这样当需要解析域名时,第一个使用的就是我们配置的DNS服务器,其它的都是候选项。

 

3. 正向解析测试,使用nslookup命令

#nslookup

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. > 172-16-1-50.realhostip.com  
  2. Server:         172.16.1.4  
  3. Address:        172.16.1.4#53  
  4.   
  5. Name:   172-16-1-50.realhostip.com  
  6. Address: 172.16.1.50  
  7.   
  8. >   
  9. > 172-16-1-51.realhostip.com  
  10. Server:         172.16.1.4  
  11. Address:        172.16.1.4#53  
  12.   
  13. Name:   172-16-1-51.realhostip.com  
  14. Address: 172.16.1.51  
  15. >  

从结果可以看到,我们配置的两个域名都能成功解析,并且DNS服务器就是我们配置的那个服务器。


4. 反向解析,使用nslookup命令

#nslookup

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. >   
  2. > 172.16.1.51  
  3. Server:         172.16.1.4  
  4. Address:        172.16.1.4#53  
  5.   
  6. 51.1.16.172.in-addr.arpa        name = 172-16-1-51.realhostip.com.  
  7. >   
  8. >   
  9. > 172.16.1.50  
  10. Server:         172.16.1.4  
  11. Address:        172.16.1.4#53  
  12.   
  13. 50.1.16.172.in-addr.arpa        name = 172-16-1-50.realhostip.com.  
  14. >   
  15. >  

从结果来看,可以正确解析我们的IP地址,并且DNS服务器就是我们配置的那个服务器。


5. 查看realhostip.com这个域名是哪个DNS服务器管理的,使用dig命令

# dig -t ns realhostip.com

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6_4.6 <<>> -t ns realhostip.com  
  2. ;; global options: +cmd  
  3. ;; Got answer:  
  4. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 37964  
  5. ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 2  
  6.   
  7. ;; QUESTION SECTION:  
  8. ;realhostip.com.                        IN      NS  
  9.   
  10. ;; ANSWER SECTION:  
  11. realhostip.com.         86400   IN      NS      realhostip.com.  
  12.   
  13. ;; ADDITIONAL SECTION:  
  14. realhostip.com.         86400   IN      A       172.16.1.4  
  15. realhostip.com.         86400   IN      AAAA    ::1  
  16.   
  17. ;; Query time: 1 msec  
  18. ;; SERVER: 172.16.1.4#53(172.16.1.4)  
  19. ;; WHEN: Wed Oct 23 14:15:22 2013  
  20. ;; MSG SIZE  rcvd: 90  

6. 使用dig命令进行正向解析

# dig 172-16-1-50.realhostip.com

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6_4.6 <<>> 172-16-1-50.realhostip.com  
  2. ;; global options: +cmd  
  3. ;; Got answer:  
  4. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 21109  
  5. ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2  
  6.   
  7. ;; QUESTION SECTION:  
  8. ;172-16-1-50.realhostip.com.    IN      A  
  9.   
  10. ;; ANSWER SECTION:  
  11. 172-16-1-50.realhostip.com. 86400 IN    A       172.16.1.50  
  12.   
  13. ;; AUTHORITY SECTION:  
  14. realhostip.com.         86400   IN      NS      realhostip.com.  
  15.   
  16. ;; ADDITIONAL SECTION:  
  17. realhostip.com.         86400   IN      A       172.16.1.4  
  18. realhostip.com.         86400   IN      AAAA    ::1  
  19.   
  20. ;; Query time: 1 msec  
  21. ;; SERVER: 172.16.1.4#53(172.16.1.4)  
  22. ;; WHEN: Wed Oct 23 14:17:57 2013  
  23. ;; MSG SIZE  rcvd: 118  

注意:

windows客户端上只有nslookup工具。

 

(3) 相关资料

[1] http://www.linuxidc.com/Linux/2012-03/56086.htm

[2] http://www.linuxidc.com/Linux/2013-07/87440.htm

[3] http://wenku.baidu.com/view/e7095ad7b14e852458fb57db.html

[4] http://blog.csdn.net/flyoxs/article/details/5940415

[5] http://yuelei.blog.51cto.com/202879/106228

[6] http://yuelei.blog.51cto.com/202879/109657

[7] http://future.blog.51cto.com/26959/90603

[8] http://www.linuxdiyf.com/viewarticle.php?id=16156

[9] http://linux.chinaunix.net/techdoc/install/2007/10/25/970650.shtml

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值