安装 dnsmasq
CentOS/RHEL
sudo yum install dnsmasq
Ubuntu/Debian
sudo apt-get install dnsmasq
基本配置
1 启用和启动服务
sudo systemctl enable dnsmasq sudo systemctl start dnsmasq
2 设置接口
告诉 dnsmasq 监听哪个网络接口:
sudo nano /etc/dnsmasq/dnsmasq.conf
interface=
如果需要指定特定的接口(eth):
interface=eth
3 设置域名和DNS服务器
sudo nano /etc/dnsmasq/dnsmasq.conf
domain=mydomain.com
如果需要设置 DNS 前缀(10.../24):
domain=10.../24
如果需要指定 DNS 所属的服务器(8.8.8.8):
server=
如果需要多个 DNS 服务器:
server=8.8.8.8, 8.8.4.4
4 启用递归查询(可选)
如果要允许 dnsmasq 作为递归 DNS 服务器:
options=--enable-recursive
5 设置NTP(可选)
如果需要同步时间:
sudo apt-get install ntpdate sudo ntpdate -s pool.ntp.org
扩展配置
1 访问控制(可选)
限制某些用户或组的访问:
sudo nano /etc/dnsmasq/dnsmasq.conf
auth
user-controller
然后设置访问控制规则:
sudo dnsmasq --add-user-controller myuser mypass
2 重定向(可选)
自动重定向用户到指定的服务器:
sudo nano /etc/dnsmasq/dnsmasq.conf
@=192.168.1.1,192.168.2.1
这将重定向请求到 192.168.1.1 或 192.168.2.1。
3 缓存设置(可选)
设置 DNS 缓存大小:
sudo nano /etc/dnsmasq/dnsmasq.conf
cache-size=100
4 防火墙设置(可选)
确保 dnsmasq 只接受必要的连接:
sudo iptables -P INPUT ACCEPT sudo iptables -P FORWARD ACCEPT sudo iptables -P OUTPUT ACCEPT sudo iptables -A INPUT -p udp --dport 53 -j ACCEPT sudo iptables -A FORWARD -i eth -p udp --dport 53 -j ACCEPT
高级配置(可选)
1 启用 SSL/TLS
如果需要加密 DNS 传输:
sudo apt-get install dnsmasq-tls sudo nano /etc/dnsmasq/dnsmasq.conf
tls-v1
tls-ca-certificate-file=/etc/dnsmasq/ca.pem
tls-key-file=/etc/dnsmasq/server.key
2 启用 DNSSEC
如果需要 DNS 安全:
sudo nano /etc/dnsmasq/dnsmasq.conf
dnssec
日志和监控
1 查看日志
sudo tail -f /var/log/messages
2 启用日志
在配置文件中添加:
sudo nano /etc/dnsmasq/dnsmasq.conf
log-queries
故障排除
1 DNS 解析失败
检查 /etc/resolv.conf 是否包含正确的 DNS 服务器:
sudo nano /etc/resolv.conf
nameservers=192.168.1.1
2 DHCP 问题
检查 DHCP 记录:
sudo nmap 192.168.1./24 | grep BOOTP
确保 DHCP 槽光板正确分配 IP 地址。
验证
1 检查 DNS 解析
nslookup mydomain.com dig mydomain.com
2 检查 DHCP 分配
arp -a
查看 arp 表,确保所有设备都有正确的 MAC 到 IP 映射。
维护
1 定期检查日志
sudo logrotate -v /var/log/messages
2 备份配置
sudo cp /etc/dnsmasq/dnsmasq.conf /etc/dnsmasq/dnsmasq.conf~
3 重启服务
sudo systemctl restart dnsmasq
通过以上步骤,你可以配置并管理一个功能强大且稳定的 dnsmasq 服务器,记得根据实际网络环境进行调整和优化。









