正向解析DNS服務

DNS(Domain Name System,域名系統),萬維網上做爲域名和IP地址相互映射的一個分佈式數據庫,可以使用戶更方便的訪問互聯網,而不用去記住可以被機器直接讀取的IP數串。經過域名,最終獲得該域名對應的IP地址的過程叫作域名解析(或主機名解析)。DNS協議運行在UDP協議之上,使用端口號53。

/etc/resolv.conf它是DNS客戶機配置文件,用於設置DNS服務器的IP地址及DNS域名,還包含了主機的域名搜索順序。該文件是由域名解析 器(resolver,一個根據主機名解析IP地址的庫)使用的配置文件。它的格式很簡單,每行以一個關鍵字開頭,後接一個或多個由空格隔開的參數。

resolv.conf的關鍵字主要有四個,分別是:
nameserver    //定義DNS服務器的IP地址
domain       //定義本地域名
search        //定義域名的搜索列表
sortlist        //對返回的域名進行排序
下面咱們給出一個/etc/resolv.conf的示例:
domain  51osos.com
search  www.51osos.com  51osos.com
nameserver 202.102.192.68
nameserver 202.102.192.69
最主要是nameserver關鍵字,若是沒指定nameserver就找不到DNS服務器,其它關鍵字是可選的。
nameserver表示解析域名時使用該地址指定的主機爲域名服務器。其中域名服務器是按照文件中出現的順序來查詢的,且只有當第一個nameserver沒有反應時才查詢下面的nameserver。
domain   聲明主機的域名。不少程序用到它,如郵件系統;當爲沒有域名的主機進行DNS查詢時,也要用到。若是沒有域名,主機名將被使用,刪除全部在第一個點( .)前面的內容。
search   它的多個參數指明域名查詢順序。當要查詢沒有域名的主機,主機將在由search聲明的域中分別查找。
domain和search不能共存;若是同時存在,後面出現的將會被使用。
sortlist  容許將獲得域名結果進行特定的排序。它的參數爲網絡/掩碼對,容許任意的排列順序。

 「search domainname.com」表示當提供了一個不包括徹底域名的主機名時,在該主機名後添加domainname.com的後 綴;「nameserver」表示解析域名時使用該地址指定的主機爲域名服務器。其中域名服務器是按照文件中出現的順序來查詢的。
其中domainname和search可同時存在,也可只有一個;nameserver可指定多個

前提準備:
一臺centos7作服務器,兩張網卡(橋接and僅主機),一臺centos6作客戶端,僅主機html

A、搭建DNS服務器:
一、[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!
二、[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
三、[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
四、數據庫存放於 /var/named 下 ,且系統默認named已安裝,
五、查看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 ~]#
六、查看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 foreverpython

七、編輯網卡配置文件,添加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 ]
八、咱們在客戶端查看配置文件,會顯示主機IP:
[root@Centos6 ~]# cat /etc/resolv.confweb

Generated by NetworkManager

domain localdomain
search localdomain
nameserver 192.168.58.1
nameserver 192.168.58.254數據庫

(host、dig、nslookup是三個專業測試名字解析的工具)

九、[root@Centos6 ~]# host www.qq.com
;; connection timed out; trying next origin
;; connection timed out; no servers could be reached
此時,咱們去修改配置文件:
十、[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.htmlvim

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; }; centos

/* 
     - 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 ,

十一、使其生效:
[root@centos7 ~]# rndc reload
server reload successful
十二、此時做爲客戶端,去訪問百度,能通,以下:服務器

[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服務器。

一、查看配置文件:

[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; };
};dom

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 來檢查語法,無錯不提示

二、[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服務能夠作到均衡負載的功能。

相關文章
相關標籤/搜索