在搭建服務器以前,咱們須要瞭解一下DNS域名解析原理,《DNS原理及其解析過程》,整體上,《DNS原理及其解析過程》對解析過程說的很是清楚。html
在這裏,補充和強調如下了三點:linux
一、DNS查詢:分爲遞歸查詢和迭代查詢兩種
(1) 遞歸查詢: 即客戶端向本地DNS服務器請求查詢域名,本地DNS服務器收到查詢任務後如自身沒法回答則向其餘服務器查詢,直到查到結果後返回結果給客戶端。期間DNS服務器可能要查詢不少其餘DNS服務器。
(2)迭代查詢:即客戶端向本地服務器請求查詢域名,本地DNS服務器沒法回答,則給客戶端返回另外一個能查詢域名的服務器地址,客戶端再向另外一服務器查詢,期間可能客戶端需查詢多個DNS服務器,最終查到結果。
二、DNS服務器分類:
DNS服務器分爲:
(1)master(主DNS服務器):擁有區域數據的文件,並對整個區域數據進行管理。
(2)slave(從服務器或叫輔助服務器):擁有主DNS服力器的區域文件的副 本,輔助主DNS服務器對客戶端進行解析,當主DNS服務器壞了後,能夠徹底接替主服務器的工做。
(3)forward:將任何查詢請求都轉發給其餘服務器。起到一個代理的做用。
(4)cache:緩存服務器。
(4)hint:根DNS internet服務器集。windows
三、DNS劫持緩存
DNS 劫持是網絡安全界常見的一個名詞,意思是經過某些手段取得某一目標域名的解析記錄控制權,進而修改此域名的解析結果,經過此修改將對此域名的訪問由原先的 IP地址轉入到本身指定的IP,從而實現竊取資料或者破壞原有正常服務的目的。安全
關於安裝DNS服務器,咱們這裏使用Windows環境,須要下載Windows版本的Bind服務器服務器
1.下載地址《DNS Bind》,咱們下載相對穩定的版本便可,配置基本相同。網絡
2.安裝
將下載的BIND9.9.0.ZIP解壓,進入到解壓後的文件夾,運行 BINDInstall.exe,在彈出的安裝窗口中輸入一個密碼,一直默認安裝就好了,不須要更改什麼設置。默認安裝到 windows\system32\dns目錄下。dom
因爲 windows\system32\dns屬於系統目錄,所以,須要設置各類權限,所以,咱們建議安裝到非系統盤,好比(E:/DNSBindServer/),本篇博客的DNS安裝位置在(E:/DNSBindServer/)下面分佈式
以上帳號,咱們省略,使用系統帳號便可。學習
安裝完成以後,咱們打開Windows系統服務管理界面
三、環境配置
3.1首先,創建以下目錄環境
3.2生成rndc.key文件
而後CMD窗口,進E:/DNSBindServer/bin目錄,用rndc-config.exe程序生產rndc.key。執行以下命令,將在E:/DNSBindServer/etc目錄下生成rndc.key
rndc-confgen -a
3.3建立named.conf
在E:/DNSBindServer/etc目錄下建立named.conf,這個是Bind服務器默認加載的配置。
配置內容以下
include "E:/DNSBindServer/etc/rndc.key"; controls { inet 127.0.0.1 port 953 allow { 127.0.0.1; } keys { "rndc-key"; }; }; include "E:/DNSBindServer/etc/named.conf.options"; include "E:/DNSBindServer/etc/named.conf.zones";
第一段引入rndc.key
第二段是控制點,用來遠程控制,注意,這樓裏的 port:953是控制點端口,不是DNS服務端口,DNS服務端口是53。
第三段是常規配置
在E:/DNSBindServer/etc/創建named.conf.options文件,在裏面加入以下內容
logging { channel warning { file "E:/DNSBindServer/log/warning.log" versions 3 size 2048k; severity warning; print-category yes; print-severity yes; print-time yes; }; channel query { file "E:/DNSBindServer/log/query.log" versions 3 size 2048k; severity info; print-category yes; print-severity yes; print-time yes; }; channel default_syslog { file "E:/DNSBindServer/log/error.log"; severity error; print-category yes; print-severity yes; print-time yes; }; channel audit_log { file "E:/DNSBindServer/log/zone_named.log"; severity error; print-time yes; }; category default { warning;}; category general { default_syslog; }; category security { audit_log; default_syslog; }; category config { default_syslog; }; category resolver { audit_log; }; category xfer-in { audit_log; }; category xfer-out { audit_log; }; category notify { audit_log; }; category client { audit_log; }; category network { audit_log; }; category update { audit_log; }; category queries { query; }; category lame-servers { audit_log; }; }; acl "trust-lan" { 127.0.0.0/8; 192.168.0.0/16; 10.1.0.0/16; }; options { #域名文件存放的絕對路徑 directory "E:/DNSBindServer/etc/"; #若是bind啓動,自動會在{pid-file}目錄生成一個named.pid文件,打開文件就是named進程的ID pid-file "E:/DNSBindServer/log/dns_named.pid"; version "1.0.0"; allow-transfer { none; }; #容許trust-lan裏的IP從主DNS上進行區域傳輸 allow-notify { "trust-lan"; }; #從服務器接收主服務器的更新通知 allow-query { "trust-lan"; }; #容許查詢,只有trust-lan中的主機發來的DNS請求才會被處理 allow-recursion{ none;}; #打開BIND遞歸查詢功能 notify yes; forwarders { 61.234.254.6; 61.234.254.5; 59.51.78.211; 8.8.8.8; }; auth-nxdomain no; # conform to RFC1035 #listen-on port 53 { 127.0.0.1;192.168.1.210;10.1.235.92; }; listen-on-v6 { any; }; };
logging模塊是日誌通道配置,這對於配置調試很是重要,由於BIND並不自動產生日誌,所以,無比在配置階段添加日誌配置,防止配置失敗問題產生。特別是以下通道。
category default { warning;};
接下來是
acl "trust-lan" { 127.0.0.0/8; 192.168.0.0/16; 10.1.0.0/16; };
acl(Access Control List)是定義一個常量的聲明,用來限制哪些主機的請求被容許響應。
下來是很是重要的options配置
options { #域名文件存放的絕對路徑 directory "E:/DNSBindServer/etc/"; #若是bind啓動,自動會在{pid-file}目錄生成一個named.pid文件,打開文件就是named進程的ID pid-file "E:/DNSBindServer/log/dns_named.pid"; version "1.0.0"; allow-transfer { none; }; #容許trust-lan裏的IP從主DNS上進行區域傳輸 allow-notify { "trust-lan"; }; #從服務器接收主服務器的更新通知 allow-query { "trust-lan"; }; #容許普通查詢 allow-recursion{ none;}; #打開BIND遞歸查詢功能 notify yes; forwarders { #若是本地域名沒法解析到ip,那麼自動前往以下地址 61.234.254.6; 61.234.254.5; 59.51.78.211; 8.8.8.8; }; auth-nxdomain no; # conform to RFC1035 listen-on-v6 { any; }; };
*auth-nxdomain 是否作爲權威服務器回答域不存在(Auth-nxdomain)
若是設置爲'yes',則容許服務器以權威性(authoritatively)的方式返回NXDOMAIN(該域不存在)的回答,不然就不會做權威性的回答,缺省值爲」是」,這裏設置爲no,讓本地服務器到DNS分佈式系統去查詢。
最後建立named.conf.zones文件,輸入以下內容
zone "." IN { type hint; #根域名服務器 file "E:/DNSBindServer/etc/named.db.root"; }; zone "localhost" IN { type master; #該域名服務器是主域名服務器,這個選項主要用在主備部署中 file "E:/DNSBindServer/etc/localhost.zone"; allow-update { none; }; }; zone "0.0.127.in-addr.arpa" IN { #這種屬於反向解析 type master; file "E:/DNSBindServer/etc/127.0.0.zone"; allow-update { none; }; #定義了容許向主zone文件發送動態更新的匹配列表 }; zone "xushjie.com" IN { type master; file "E:/DNSBindServer/etc/xushjie.com.zone"; allow-update { none; }; }; zone "0.168.192.in-addr.arpa" IN { type master; file "E:/DNSBindServer/etc/192.168.0.zone"; allow-update { none; }; }; zone "baidu.com" IN{ type master; file "E:/DNSBindServer/etc/baidu.com.zone"; allow-update { none; }; };
咱們看到,第一項是name.db.root,
下載地址時《ftp://ftp.internic.net/domain/named.root》
解析文件配置
反向解析
127.0.0.zone
$TTL 86400 @ IN SOA localhost. root.localhost. ( 2005030122 28800 14400 3600000 86400 ) @ IN NS localhost. 1 IN PTR localhost. ;反解析,注意,1表示主機號,者此處是 127.0.0.1 2 IN PTR localhost. ;反解析,注意,2表示主機號,者此處是 127.0.0.2
192.168.0.zone
$TTL 86400 @ IN SOA ns.xushjie.com. root.xushjie.com. ( ;ns.xushjie.com.表示域名組, root.xushjie.com.表示郵箱root@xueshjie.com 2005030119 7200 3600 43200 86400 ) @ IN NS ns.xushjie.com. ;反向解析第一句表示類型(必須) 1 IN PTR www.xushjie.com. ;表示192.168.0.1 100 IN PTR www.xushjie.com. ;表示192.168.0.100
正向解析
localhost.zone
$TTL 86400 localhost. IN SOA localhost. root.localhost. ( 2001062501 ;serial 21600 ;refresh 3600 ;retry 604800 ;expire 86400 ;mininum ) @ IN NS localhost. @ IN A 127.0.0.1 @ IN AAAA ::1
xushijie.com.zone
$TTL 86400 @ IN SOA ns.xushjie.com. root.xushjie.com. ( 1053891168 21600 3600 604800 86400 ) @ IN NS ns.xushjie.com. ns IN A 192.168.0.100 www IN A 192.168.0.100
其中一句以下:用來表示域名所對應的地址組(ns.xushjie.com),以及管理員郵箱(root.xushjie.com.,本應該是root@xushjie.com,可是@具備特殊意義)
@ IN SOA ns.xushjie.com. root.xushjie.com.
再來看下個例子
baidu.com.zone
$TTL 86400 @ IN SOA ns1.baidu.com. root.baidu.com.( 2001072501 ;serial 21600 ;refresh 3600 ;retry 604800 ;expire 86400 ;mininum ) @ IN NS ns1.baidu.com. ;(定義ns1) ns1 IN A 220.181.112.244 ;(讓ns1指向對應的ip,不然如下地址沒法解析) www IN A 220.181.112.244 image IN A 180.149.131.70
關於配置,請參考以下博客
《DNS named.conf配置&DNS主從配置》
《DNS服務器bind的master 和 slave 搭建》
《DNS服務器之一:DNS簡介及BIND安裝與基本配置 系列文章》
《DNS掃盲系列之五:域名配置ZONE文件 系列文章》
4.接下來就是DNS 測試
首先要啓動BindServer
而後執行以下命令
目前爲止,咱們的DNS服務器已經安裝完成。
5.私有解析服務器設置
可是,這DNS服務器是獨立運行的,若是咱們直接執行以下命令
dig www.baidu.com
那麼不會經過BINDServer解析,所以,爲了進一步使用BINDServer,咱們須要設置主機DNS服務地址