Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux DNS伺服器搭建全攻略:BIND主從配置實戰

編輯:Linux 教學

DNS(Domain Name System)將域名解析為IP位址,是網路基礎設施核心。本文以BIND 9為例,詳細講解主/從DNS伺服器在Linux上的完整部署流程。

環境假設

  • 主DNS:ns1.example.com (192.168.1.10)
  • 從DNS:ns2.example.com (192.168.1.11)
  • 域名:example.com
  • 作業系統:CentOS 7 / RHEL 7 / Rocky Linux

安裝與基礎配置

yum install bind bind-utils -y
systemctl enable --now named

核心配置檔 /etc/named.conf

options {
    listen-on port 53 { 127.0.0.1; 192.168.1.10; };
    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";
    allow-query     { localhost; 192.168.1.0/24; };
    recursion yes;
    dnssec-enable yes;
    dnssec-validation yes;
    pid-file "/run/named/named.pid";
    session-keyfile "/run/named/session.key";
};

logging {
    channel default_debug {
        file "data/named.run";
        severity dynamic;
    };
};

zone "." IN {
    type hint;
    file "named.ca";
};

zone "example.com" IN {
    type master;
    file "example.com.zone";
    allow-update { none; };
    allow-transfer { 192.168.1.11; };  # 允許從伺服器區域傳送
};

zone "1.168.192.in-addr.arpa" IN {
    type master;
    file "192.168.1.zone";
    allow-transfer { 192.168.1.11; };
};

正向解析區域檔 /var/named/example.com.zone

$TTL 86400
@   IN  SOA  ns1.example.com. admin.example.com. (
        2024010101 ; Serial (YYYYMMDDNN)
        3600       ; Refresh (1h)
        1800       ; Retry (30m)
        604800     ; Expire (1w)
        86400 )    ; Minimum TTL (1d)
    IN  NS   ns1.example.com.
    IN  NS   ns2.example.com.
ns1 IN  A    192.168.1.10
ns2 IN  A    192.168.1.11
www IN  A    192.168.1.20
mail IN  A    192.168.1.21
@   IN  MX  10 mail.example.com.

反向解析區域檔 /var/named/192.168.1.zone

$TTL 86400
@   IN  SOA  ns1.example.com. admin.example.com. (
        2024010101
        3600
        1800
        604800
        86400 )
    IN  NS   ns1.example.com.
    IN  NS   ns2.example.com.
10  IN  PTR  ns1.example.com.
11  IN  PTR  ns2.example.com.
20  IN  PTR  www.example.com.
21  IN  PTR  mail.example.com.

從DNS配置

在ns2上安裝bind後,修改/etc/named.conf區域定義:


zone "example.com" IN {
    type slave;
    file "slaves/example.com.zone";
    masters { 192.168.1.10; };
};

zone "1.168.192.in-addr.arpa" IN {
    type slave;
    file "slaves/192.168.1.zone";
    masters { 192.168.1.10; };
};

type slave + masters 指定主伺服器,啟動時自動 AXFR/IXFR 同步區域資料至 /var/named/slaves/。

驗證與測試

# 語法檢查
named-checkconf
named-checkzone example.com /var/named/example.com.zone

# 重載
rndc reload
# 或 systemctl reload named

# 客戶端測試
dig @192.168.1.10 www.example.com
dig @192.168.1.11 www.example.com   # 從伺服器
nslookup mail.example.com 192.168.1.10

安全加固建議

  • 啟用TSIG金鑰驗證區域傳送:dnssec-keygen -a HMAC-SHA256 -b 256 -n HOST transfer-key,雙端配置key "transfer-key" { algorithm hmac-sha256; secret "..."; };並在allow-transfer { key transfer-key; };引用。
  • 限制allow-query、allow-recursion至信任網段。
  • 關閉不必要的recursion(授權優先伺服器建議關閉遞迴)。
  • 定期更新named.ca根區域檔。
  • 監控rndc stats、日誌異常查詢。

常見錯誤排查

現象可能原因
SERVFAIL區域檔語法錯誤、Serial未遞增、權限問題
REFUSEDallow-query/allow-transfer未包含客戶端IP
從伺服器不同步防火牆阻擋53/tcp、主伺服器allow-transfer缺失、Serial未遞增
解析緩慢遞迴查詢超時、根伺服器不可達、dnssec-validation失敗

BIND 9穩定成熟,配合rndc管理工具、dig測試指令,是企業級DNS部署的標準選擇。

Copyright © Windows教程網 All Rights Reserved