码迷,mamicode.com
首页 > 其他好文 > 详细

正向解析DNS服务

时间:2019-01-31 21:33:58      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:code   loaded   resolve   rect   multicast   libs   disco   flags   question   

前提准备: 一台centos7做服务器,两张网卡(桥接and仅主机),一台centos6做客户端,仅主机 ##### A、搭建DNS服务器: 1、[root@centos7 ~]# yum install bind

Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
Installed:
bind.x86_64 32:9.9.4-72.el7
Dependency Installed:
python-ply.noarch 0:3.4-11.el7
Complete!

2、[root@centos7 ~]# rpm -ql bind
/etc/logrotate.d/named
/etc/named
/etc/named.conf
/etc/named.iscdlv.key
/etc/named.rfc1912.zones
/etc/named.root.key
/etc/rndc.conf
/etc/rndc.key
/etc/rwtab.d/named
/etc/sysconfig/named
/run/named
/var/log/named.log
/var/named
/var/named/data
/var/named/dynamic
/var/named/named.ca
/var/named/named.empty
/var/named/named.localhost
/var/named/named.loopback
/var/named/slaves
3、[root@centos7 ~]# rpm -qa "bind*"
bind-libs-lite-9.9.4-72.el7.x86_64
bind-license-9.9.4-72.el7.noarch
bind-9.9.4-72.el7.x86_64
bind-libs-9.9.4-72.el7.x86_64
bind-utils-9.9.4-72.el7.x86_64
4、数据库存放于 /var/named 下 ,且系统默认named已安装,

5、查看named的状态且启动该服务:
[root@centos7 ~]# systemctl status named
● named.service - Berkeley Internet Name Domain (DNS)
Loaded: loaded (/usr/lib/systemd/system/named.service; disabled; vendor preset: disabled)
Active: inactive (dead)
[root@centos7 ~]# systemctl start named
[root@centos7 ~]#

6、查看IP,有IP即可上网
[root@centos7 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:87:73:17 brd ff:ff:ff:ff:ff:ff
inet 192.168.58.254/24 brd 192.168.58.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe87:7317/64 scope link
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:87:73:21 brd ff:ff:ff:ff:ff:ff
inet 172.20.119.243/16 brd 172.20.255.255 scope global noprefixroute dynamic ens37
valid_lft 83928sec preferred_lft 83928sec
inet6 fe80::23ec:ee7:326a:3f9c/64 scope link noprefixroute
valid_lft forever preferred_lft forever

7、编辑网卡配置文件,添加DNS1=192.168.58.254:
[root@Centos6 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
[root@Centos6 ~]# service network restart
Shutting down interface eth0: Device state: 3 (disconnected)
[ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: Active connection state: activating
Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/6
state: activated
Connection activated
[ OK ]

8、我们在客户端查看配置文件,会显示主机IP:
[root@Centos6 ~]# cat /etc/resolv.conf

Generated by NetworkManager

domain localdomain
search localdomain
nameserver 192.168.58.1
nameserver 192.168.58.254

(host、dig、nslookup是三个专业测试名字解析的工具)

9、[root@Centos6 ~]# host www.qq.com
;; connection timed out; trying next origin
;; connection timed out; no servers could be reached

此时,我们去修改配置文件:
10、[root@centos7 ~]# vim /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.
//
// See the BIND Administrator‘s Reference Manual (ARM) for details about the
// configuration located in /usr/share/doc/bind-{version}/Bv9ARM.html

options {
listen-on port 53 { localhost; };
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";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; };

    /* 
     - If you are building an AUTHORITATIVE DNS server, do NOT enable recursion.
     - If you are building a RECURSIVE (caching) DNS server, you need to enable 

"/etc/named.conf" 61L, 1802C

将文件中的127.0.0.1 改为localhost,将allow-query {} 中改为 any ,

11、使其生效:
[root@centos7 ~]# rndc reload
server reload successful

12、此时作为客户端,去访问百度,能通,如下:

[root@Centos6 ~]# dig www.baidu.com

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.68.rc1.el6 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 60669
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 5, ADDITIONAL: 5

;; QUESTION SECTION:
;www.baidu.com.         IN  A

;; ANSWER SECTION:
www.baidu.com.      1200    IN  CNAME   www.a.shifen.com.
www.a.shifen.com.   300 IN  A   61.135.169.125
www.a.shifen.com.   300 IN  A   61.135.169.121

;; AUTHORITY SECTION:
a.shifen.com.       1200    IN  NS  ns4.a.shifen.com.
a.shifen.com.       1200    IN  NS  ns1.a.shifen.com.
a.shifen.com.       1200    IN  NS  ns2.a.shifen.com.
a.shifen.com.       1200    IN  NS  ns5.a.shifen.com.
a.shifen.com.       1200    IN  NS  ns3.a.shifen.com.

;; ADDITIONAL SECTION:
ns5.a.shifen.com.   1200    IN  A   180.76.76.95
ns1.a.shifen.com.   1200    IN  A   61.135.165.224
ns3.a.shifen.com.   1200    IN  A   112.80.255.253
ns2.a.shifen.com.   1200    IN  A   220.181.57.142
ns4.a.shifen.com.   1200    IN  A   14.215.177.229

;; Query time: 1018 msec
;; SERVER: 192.168.58.254#53(192.168.58.254)
;; WHEN: Thu Jan 31 19:09:55 2019
;; MSG SIZE  rcvd: 260

分析:此时作为客户端尽管无上外网功能,去ping百度可通,当把7的内网关闭,6无法访问外网,却可以访问7访问过的百度,这是7留下了缓存!当把7的缓存rndc flush 清除掉,6就无法再访问外网了,所以,7就叫只缓存服务器,只利用缓存去访问。

接下来,我们的目标是把它变成能有自己的数据库,可以解析数据的域后缀的DNS服务器。

1、查看配置文件:

[root@centos7 ~]# vim /etc/named.conf
其中的 “ include "/etc/named.rfc1912.zones"; ” 专门存放域名的

查看:[root@centos7 ~]# vim /etc/named.rfc1912.zones
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; };
};

我们把zone "magedu.com" IN {
  type master;
  file "magedu.com.zones";

}; 添加进去

[root@centos7 ~]# named-checkconf 来检查语法,无错不提示

2、[root@centos7 ~]# cd /var/named

[root@centos7 named]# ls
data dynamic named.ca named.empty named.localhost named.loopback slaves
[root@centos7 named]# cp -p named.localhost magedu.com.zone
[root@centos7 named]# id named
uid=25(named) gid=25(named) groups=25(named)
[root@centos7 named]# vim magedu.com.zone

$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
AAAA :
"magedu.com.zone" 10L, 152C

我们将其文件修改,变成下图所示:
$TTL  1D
@      IN   SOA  master   admin.magedu.com.  (
                                    2019013120               ;  serial
                                                                        1D       ;   refresh
                                                                        1H       ;   retry
                                                                        1W      ;   expire
                                                                        3H    )   ;   minimun
        NS         master
master       A                192.168.58.254
www          CNAME      websrv
websrv      A                 192.168.58.133
blog           A                 192.168.58.136
@              mx               10  mailsrv1
@              mx               20  mailsrv2
mailsrv1     A                 192.168.58.137
mailsrv2     A                  192.168.58.138

使用“ named-checkzone ” 来检查语法。

[root@centos7 ~]# rndc reload
server reload successful

[root@Centos6 ~]# cat /etc/resolv.conf

Generated by NetworkManager

domain localdomain
nameserver 192.168.58.254

搜索结果如下:
[root@Centos6 ~]# dig www.magedu.com
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.68.rc1.el6 <<>> www.magedu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15911
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 18

;; QUESTION SECTION:
;www.magedu.com. IN A

;; ANSWER SECTION:
www.magedu.com. 600 IN A 101.200.188.230

;; AUTHORITY SECTION:
magedu.com. 172154 IN NS ns2.alidns.com.
magedu.com. 172154 IN NS ns1.alidns.com.

我们现在恢复网卡:ifconfig Eth1 up
再添加一个路由:
route add default gw 192.168.58.100 (可随便写)

ping www.baidu.com 可通但IP每次都不一样,

说明DNS服务可以做到均衡负载的功能。

正向解析DNS服务

标签:code   loaded   resolve   rect   multicast   libs   disco   flags   question   

原文地址:http://blog.51cto.com/14128387/2348284

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!