在 Linux 上配置辅助 DNS(Secondary DNS)主要有两种方式:
一、使用 BIND 配置辅助 DNS
1. 主 DNS 服务器配置(允许区域传输)
/etc/bind/named.conf.local 或 zone文件
zone "example.com" {
type master;
file "/etc/bind/db.example.com";
allow-transfer {
192.168.1.100; # 辅助DNS服务器IP
10.0.0.100; # 其他允许传输的IP
};
also-notify {
192.168.1.100; # 主动通知辅助DNS
};
notify yes;
};
辅助DNS服务器上的配置
zone "example.com" {
type slave;
file "/var/cache/bind/db.example.com";
masters {
192.168.1.1; # 主DNS服务器IP
};
allow-transfer { none; }; # 可禁止进一步传输
allow-query { any; };
};二、使用 dnsmasq 配置辅助 DNS
/etc/dnsmasq.conf no-resolv server=8.8.8.8 # 上游主DNS server=8.8.4.4 # 备用上游DNS cache-size=1000 作为辅助DNS时,通常转发查询到主DNS
三、使用 systemd-resolved
/etc/systemd/resolved.conf [Resolve] DNS=主DNS服务器IP FallbackDNS=辅助DNS服务器IP DNSStubListener=no
四、简单的 bash 脚本实现 DNS 负载均衡
#!/bin/bash
dns-proxy.sh - 简单的DNS代理/辅助脚本
PRIMARY_DNS="8.8.8.8"
SECONDARY_DNS="1.1.1.1"
LISTEN_PORT=53
LOG_FILE="/var/log/dns-proxy.log"
使用 netcat 或 socat 实现简单的DNS转发
while true; do
nc -l -u -p $LISTEN_PORT | \
while read query; do
# 尝试主DNS
response=$(echo "$query" | nc -u -w 2 $PRIMARY_DNS 53)
if [ $? -eq 0 ] && [ -n "$response" ]; then
echo "$response"
else
# 主DNS失败,使用辅助DNS
echo "$query" | nc -u -w 2 $SECONDARY_DNS 53
fi
echo "$(date): Query processed" >> "$LOG_FILE"
done
done五、使用 Python 实现的 DNS 辅助服务器
#!/usr/bin/env python3
dns_assistant.py
import socket
import threading
from dnslib import DNSRecord, RCODE
class SecondaryDNS:
def __init__(self, primary_dns='8.8.8.8', listen_port=53):
self.primary_dns = primary_dns
self.listen_port = listen_port
self.cache = {}
def forward_query(self, query_data, primary=True):
"""转发查询到主DNS"""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(2)
dns_server = self.primary_dns if primary else '1.1.1.1'
sock.sendto(query_data, (dns_server, 53))
response, _ = sock.recvfrom(512)
return response
except:
return None
def handle_query(self, data, client_addr, sock):
"""处理DNS查询"""
try:
# 尝试主DNS
response = self.forward_query(data, primary=True)
if not response:
# 尝试备用DNS
response = self.forward_query(data, primary=False)
if response:
sock.sendto(response, client_addr)
# 可选:缓存结果
query = DNSRecord.parse(data)
if query.q.qtype == 1: # A记录
self.cache[query.q.qname] = response
except Exception as e:
print(f"Error: {e}")
def start(self):
"""启动DNS服务器"""
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('0.0.0.0', self.listen_port))
print(f"Secondary DNS listening on port {self.listen_port}")
while True:
data, addr = sock.recvfrom(512)
thread = threading.Thread(
target=self.handle_query,
args=(data, addr, sock)
)
thread.start()
if __name__ == '__main__':
dns = SecondaryDNS(primary_dns='192.168.1.1')
dns.start()六、使用 Docker 快速部署
docker-compose.yml
version: '3'
services:
bind-secondary:
image: ubuntu/bind9
ports:
- "53:53/udp"
- "53:53/tcp"
volumes:
- ./bind-config:/etc/bind
- ./bind-cache:/var/cache/bind
command: /usr/sbin/named -g -c /etc/bind/named.conf七、配置检查和测试
1. 检查配置语法 named-checkconf /etc/bind/named.conf 2. 测试DNS解析 dig @辅助DNSIP example.com nslookup example.com 辅助DNSIP 3. 查看区域传输状态 rndc status named-compilezone -f raw -F text -o - example.com /var/cache/bind/db.example.com 4. 监控DNS查询日志 tail -f /var/log/syslog | grep named
八、关键配置注意事项
1、安全设置:
# 限制区域传输
allow-transfer { trusted-servers; };
# 使用TSIG密钥认证
server 192.168.1.100 {
keys { key-name; };
};2、性能优化:
# /etc/bind/named.conf.options
options {
directory "/var/cache/bind";
max-cache-size 256M;
max-cache-ttl 3600;
minimal-responses yes;
};3、自动同步(使用rsync或inotify):
# 监控主DNS区域文件变化
inotifywait -m -e modify /etc/bind/zones/ |
while read path action file; do
rsync -avz /etc/bind/zones/ secondary-dns:/etc/bind/zones/
done选择哪种方案取决于你的具体需求:
BIND:功能最全,适合生产环境
dnsmasq:轻量级,适合小型网络
自定义脚本:灵活,适合特殊需求
需要更详细的哪部分配置?
文章摘自:https://idc.huochengrm.cn/dns/21019.html
评论
丹舒
回复Linux辅助DNS可以通过编写shell脚本或使用现有工具如dnsmasq、unbound等实现,实现域名解析和缓存功能。
闽诗兰
回复Linux辅助DNS编写涉及网络编程、DNS协议理解,需掌握C语言及Linux系统环境。
白恬悦
回复在Linux上配置辅助DNS,可选用BIND、dnsmasq、systemd-resolved或自定义脚本等方案,选择方案需考虑需求、网络规模和复杂度,如需更详细配置,请指定具体方案。
薛瑞灵
回复Linux辅助DNS可利用bind或dnsmasq等工具,配置文件编写需遵循规范。
钭溪澈
回复Linux辅助DNS编写涉及配置DNS服务器、解析请求、缓存管理等多个方面,需要掌握相关命令和脚本编程技巧。