在Linux上常用的DNS服务器软件有: - BIND (Berkeley Internet Name Domain) - 最广泛使用的DNS服务器 - dnsmasq - 轻量级DNS转发器和DHCP服务器 - Unbound - 专注于安全和验证的递归DNS服务器 - PowerDNS - 功能丰富的DNS服务器
# 在基于Debian/Ubuntu的系统上
sudo apt update
sudo apt install bind9 bind9utils bind9-doc
# 在基于RHEL/CentOS的系统上
sudo yum install bind bind-utils
主配置文件位于 /etc/bind/named.conf
(或 /etc/named.conf
)
sudo nano /etc/bind/named.conf
典型配置包括:
- options
: 全局设置
- logging
: 日志配置
- zone
: 定义DNS区域
zone "example.com" {
type master;
file "/etc/bind/db.example.com";
};
sudo nano /etc/bind/db.example.com
示例内容:
$TTL 86400
@ IN SOA ns1.example.com. admin.example.com. (
2023072101 ; Serial
3600 ; Refresh
1800 ; Retry
604800 ; Expire
86400 ) ; Minimum TTL
@ IN NS ns1.example.com.
@ IN NS ns2.example.com.
@ IN A 192.168.1.10
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.30
zone "1.168.192.in-addr.arpa" {
type master;
file "/etc/bind/db.192";
};
sudo nano /etc/bind/db.192
示例内容:
$TTL 86400
@ IN SOA ns1.example.com. admin.example.com. (
2023072101 ; Serial
3600 ; Refresh
1800 ; Retry
604800 ; Expire
86400 ) ; Minimum TTL
@ 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.
30 IN PTR mail.example.com.
sudo named-checkconf
sudo named-checkzone example.com /etc/bind/db.example.com
sudo named-checkzone 1.168.192.in-addr.arpa /etc/bind/db.192
# 在systemd系统上
sudo systemctl restart named
# 或
sudo systemctl restart bind9
# 设置开机启动
sudo systemctl enable named
# 或
sudo systemctl enable bind9
# 允许DNS服务(端口53)
sudo ufw allow 53/tcp
sudo ufw allow 53/udp
# 使用dig测试
dig @your_dns_server_ip example.com
dig @your_dns_server_ip -x 192.168.1.10
# 使用nslookup测试
nslookup example.com your_dns_server_ip
nslookup 192.168.1.10 your_dns_server_ip
options {
forwarders {
8.8.8.8;
8.8.4.4;
};
forward only;
};
acl "trusted" {
192.168.1.0/24;
10.0.0.0/8;
};
options {
allow-query { trusted; };
allow-recursion { trusted; };
};
options {
dnssec-enable yes;
dnssec-validation yes;
dnssec-lookaside auto;
};
logging {
channel default_debug {
file "/var/log/named/named.run";
severity dynamic;
};
channel query_log {
file "/var/log/named/query.log";
severity info;
};
category queries { query_log; };
};
记得定期轮转日志文件以管理磁盘空间。
希望这个指南能帮助您在Linux系统上成功设置DNS服务器。根据您的具体需求,可能需要调整配置细节。