# DNS # ## 安裝Bind9 ## ```bash apt -y install bind9 dnsutils #dnsutils不一定要安裝,測試用 ``` ## 編輯設定檔 ## >配置正解反解的設定檔為/etc/bind/named.conf.local ```bash #使用vim編輯 vim /etc/bind/named.conf.local #寫入以下內容 #域名正解 zone "it29.local" { type master; #主要區域 file "/etc/bind/db.local"; }; #域名反解 zone "1.1.192.in-addr.arpa" { type master; file "/etc/bind/db.1.168.192" }; ``` ## 配置正向對應區域 ## >設定名稱與IP對應的設定檔稱為Zone file,位於/etc/bind/db.local |Name|IP Address| |---|---| |ns1|192.168.1.1| |ns2|192.168.1.2| |www(load balancing)|192.168.1.1| ||192.168.1.2| |mail|192.168.1.2| ```bash #複製一份設定檔作為範本(要直接編輯也可,複製是為了怕編輯錯誤時可復原),以區域名稱作為檔名,比較好記憶 cp /etc/bind/db.local /etc/bind/db.it29.local #使用vim編輯 vim /etc/bind/db.it29.local #寫入以下內容 ; ; BIND data file for example.com ; $TTL 604800 #最小存留時間(秒) #主要伺服器 #負責人 @ IN SOA ns1.it29.local. root.it29.local. ( 2 ; Serial #版本,當zone file變動時管理員要自行增加號碼,slave會進行比對和決定是否要進行zone transfer 604800 ; Refresh #重整間隔(秒),Slave詢問Primary更新區域的重整間隔 86400 ; Retry #重試間隔(秒),每隔多少秒再試一次 2419200 ; Expire #到期時間(秒),直到到期,Slave的區域會停止回答 604800 ) ; Negative Cache TTL #這個紀錄的存留時間(秒) ; 86400 IN NS ns1.example.com. ns1 86400 IN A 192.168.1.1 ns2 86400 IN A 192.168.1.2 www 0 IN A 192.168.1.1 0 IN A 192.168.1.2 86400 IN MX 10 mail.lai.com mail 86400 IN A 192.168.1.2 ``` ## 配置反向對應區域 ## ```bash ; ; BIND reverse data file for example.com ; $TTL 604800 @ IN SOA ns1.example.com. root.example.com. ( 1 ; Serial 604800 ; Refresh 86400 ; Retry 2419200 ; Expire 604800 ) ; Nagative Cache TTL ; IN NS ns1.example.com. 1 IN PTR ns1.example.com. 2 IN PTR ns2.example.com. ``` ## Zone Transfer ## ## Master ## ```bash #使用vim編輯 vim /etc/bind/named.conf #加入以下內容 include "/etc/bind/named.conf.default-zones"; include "/etc/bind/named.conf.local"; include "/etc/bind/named.conf.options"; zone "example.com" { type master; file "/etc/bind/example.com"; notify yes; allow-transfer { 192.168.1.2; }; }; zone "1.168.192.in-addr.arpa" { type master; file "/etc/bind/1.168.192.rev"; notify yes; allow-transfer { 192.168.1.2; }; }; ``` ## Slave ## ```bash #使用vim編輯 vim /etc/bind/named.conf #加入以下內容 include "/etc/bind/named.conf.default-zones"; include "/etc/bind/named.conf.local"; include "/etc/bind/named.conf.options"; zone "example.com" { type slave; file "slave/example.com.saved"; masters { 192.168.1.1; }; }; zone "1.168.192.rev" { type slave; file "slave/1.168.192.rev.saved"; masters { 192.168.1.1; }; }; #因效能考量,Slave的Zone file會以binary的方式儲存,所以打開會是亂碼,若要變更為文字格式 #請在/etc/bind/named.conf.options的options內加入以下這行 masterfile-format text; ```