我来详细讲解服务器DNS的配置方法,主要分为两种情况:配置服务器作为DNS客户端和配置服务器作为DNS服务器。
一、配置服务器作为DNS客户端(最常见场景)
这是让服务器能够正常解析域名的配置。
方法A:修改/etc/resolv.conf(临时/传统方式)
编辑配置文件 sudo vi /etc/resolv.conf 添加DNS服务器, nameserver 8.8.8.8 # Google DNS nameserver 8.8.4.4 # Google DNS备用 nameserver 114.114.114.114 # 国内DNS nameserver 1.1.1.1 # Cloudflare DNS
注意:在某些系统中,这个文件可能被网络管理服务覆盖,重启后会失效。
方法B:使用网络管理工具(推荐,永久生效)
Ubuntu/Debian(使用systemd-resolved或Netplan):
方法1:使用systemd-resolved sudo systemctl enable systemd-resolved sudo systemctl start systemd-resolved 配置DNS sudo vi /etc/systemd/resolved.conf 修改以下内容: [Resolve] DNS=8.8.8.8 1.1.1.1 FallbackDNS=114.114.114.114
或使用Netplan(Ubuntu 18.04+):
编辑配置文件 /etc/netplan/01-netcfg.yaml
network:
version: 2
ethernets:
eth0:
dhcp4: yes
nameservers:
addresses: [8.8.8.8, 1.1.1.1]CentOS/RHEL/Fedora(使用NetworkManager):
编辑网卡配置文件 sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0 添加或修改: DNS1=8.8.8.8 DNS2=1.1.1.1 PEERDNS=no # 防止DHCP覆盖DNS设置
或使用nmcli:
查看连接名称 nmcli connection show 设置DNS sudo nmcli connection modify "连接名" ipv4.dns "8.8.8.8 1.1.1.1" sudo nmcli connection up "连接名"
方法C:通过/etc/nsswitch.conf 控制解析顺序
sudo vi /etc/nsswitch.conf 确保hosts行类似这样,files优先于dns: hosts: files dns myhostname
2. Windows Server配置方法
1、打开控制面板 →网络和共享中心
2、点击当前连接 →属性
3、选择IPv4 →属性
4、选择使用下面的DNS服务器地址
5、填入首选和备用DNS服务器
6、点击高级 →DNS 选项卡,可添加更多DNS服务器
查看当前网络适配器
Get-NetAdapter
设置DNS(以接口索引1为例)
Set-DnsClientServerAddress -InterfaceIndex 1 -ServerAddresses 8.8.8.8,1.1.1.1
添加多个DNS
Set-DnsClientServerAddress -InterfaceIndex 1 -ServerAddresses @("8.8.8.8","1.1.1.1","114.114.114.114")Linux验证 cat /etc/resolv.conf # 查看当前DNS设置 nslookup www.google.com # 测试域名解析 dig www.google.com # 更详细的DNS查询 ping www.google.com -c 3 # 测试连通性 Windows验证 ipconfig /all # 查看DNS配置 nslookup www.google.com # 测试解析
二、配置服务器作为DNS服务器
1. 使用BIND(最常用的DNS服务器软件)
安装BIND(Linux):
Ubuntu/Debian sudo apt-get update sudo apt-get install bind9 bind9utils bind9-doc CentOS/RHEL sudo yum install bind bind-utils
基本配置:
主配置文件
sudo vi /etc/bind/named.conf.options
添加内容:
options {
directory "/var/cache/bind";
recursion yes; # 允许递归查询
allow-query { any; }; # 允许哪些客户端查询
forwarders {
8.8.8.8;
8.8.4.4;
};
dnssec-validation auto;
listen-on { any; };
listen-on-v6 { any; };
};配置区域文件(正向解析):
sudo vi /etc/bind/named.conf.local
添加:
zone "example.com" {
type master;
file "/etc/bind/db.example.com";
};创建区域数据文件:
sudo vi /etc/bind/db.example.com
内容示例:
$TTL 86400
@ IN SOA ns1.example.com. admin.example.com. (
2024010101 ; 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.100
ns1 IN A 192.168.1.100
ns2 IN A 192.168.1.101
www IN A 192.168.1.102
mail IN A 192.168.1.103启动和测试:
检查配置文件语法 sudo named-checkconf sudo named-checkzone example.com /etc/bind/db.example.com 启动服务 sudo systemctl start bind9 sudo systemctl enable bind9 测试DNS服务器 dig @localhost www.example.com
2. 使用dnsmasq(轻量级DNS服务器)
安装dnsmasq:
sudo apt-get install dnsmasq # Ubuntu/Debian sudo yum install dnsmasq # CentOS/RHEL
基本配置:
sudo vi /etc/dnsmasq.conf 常用配置选项: 监听地址 listen-address=127.0.0.1,192.168.1.100 上游DNS服务器 server=8.8.8.8 server=1.1.1.1 本地域名解析 address=/example.com/192.168.1.100 address=/www.example.com/192.168.1.102 本地hosts文件也生效 addn-hosts=/etc/hosts DHCP功能(可选) dhcp-range=192.168.1.50,192.168.1.150,12h
启动服务:
sudo systemctl restart dnsmasq sudo systemctl enable dnsmasq
三、推荐公共DNS服务器
国际DNS 8、8.8.8, 8.8.4.4 # Google DNS 1、1.1.1, 1.0.0.1 # Cloudflare DNS 9、9.9.9 # Quad9(安全DNS) 国内DNS 114、114.114.114 # 114DNS 223、5.5.5, 223.6.6.6 # 阿里DNS 119、29.29.29 # DNSPod 180、76.76.76 # 百度DNS
四、注意事项
1、配置文件备份:修改前备份原文件
sudo cp /etc/resolv.conf /etc/resolv.conf.backup
2、DNS缓存刷新:
# Linux刷新DNS缓存 sudo systemd-resolve --flush-caches # Windows刷新DNS缓存 ipconfig /flushdns
3、多DNS服务器配置:建议配置2-3个DNS服务器,提高可靠性
4、测试DNS响应速度:
dig @8.8.8.8 www.google.com | grep "Query time"
5、防火墙设置:确保53端口(DNS端口)开放
sudo ufw allow 53/tcp sudo ufw allow 53/udp
五、故障排查
1、检查网络连通性:ping 8.8.8.8
2、检查DNS解析:nslookup www.google.com
3、查看详细DNS查询:dig +trace www.google.com
4、检查服务状态:systemctl status bind9 或systemctl status dnsmasq
根据你的具体需求选择合适的配置方式,对于大多数服务器,配置为DNS客户端(使用公共DNS)就足够了,如果需要内部域名解析或特殊需求,才需要搭建自己的DNS服务器。
文章摘自:https://idc.huochengrm.cn/dns/23505.html
评论
桑丽
回复服务器配置DNS需确定域名及IP地址,修改配置文件添加相关记录即可。
祭如南
回复服务器配置DNS需要确定DNS服务器地址,设置域名解析记录,包括A记录、CNAME记录等,并确保DNS解析结果正确指向服务器IP地址,同时检查DNS解析的缓存时间(TTL)以优化网络性能。