CentOS怎么配置DNS域名?

HCRM技术_小炮 DNS 2025-10-15 3 0

在CentOS中配置DNS域名解析有以下几种方法:

方法1:使用nmcli命令(推荐)

查看当前网络连接

nmcli connection show

修改DNS配置

临时修改(重启后失效)
nmcli connection modify "连接名" ipv4.dns "8.8.8.8 8.8.4.4"
nmcli connection modify "连接名" ipv4.ignore-auto-dns yes
永久修改
nmcli connection up "连接名"

示例

假设连接名为ens33
nmcli connection modify ens33 ipv4.dns "8.8.8.8 114.114.114.114"
nmcli connection modify ens33 ipv4.ignore-auto-dns yes
nmcli connection up ens33

方法2:修改配置文件

编辑resolv.conf文件

vi /etc/resolv.conf

添加或修改以下内容:

nameserver 8.8.8.8
nameserver 114.114.114.114
search localdomain
options timeout:2 attempts:3 rotate

注意:在某些系统中,/etc/resolv.conf可能会被网络服务覆盖。

编辑网卡配置文件

CentOS 7/8
vi /etc/sysconfig/network-scripts/ifcfg-ens33
CentOS 9/Stream
vi /etc/NetworkManager/system-connections/ens33.nmconnection

在文件中添加或修改:

DNS1=8.8.8.8
DNS2=114.114.114.114
PEERDNS=no

方法3:使用systemd-resolved(CentOS 8+)

启用并配置systemd-resolved

systemctl enable systemd-resolved
systemctl start systemd-resolved
设置DNS
resolvectl dns ens33 8.8.8.8 114.114.114.114
查看状态
resolvectl status

方法4:配置静态主机名解析

编辑hosts文件

vi /etc/hosts

添加域名解析:

192、168.1.100 server1.example.com server1
192、168.1.101 server2.example.com server2

常用DNS服务器地址

国内常用DNS
114、114.114.114      # 114DNS
223、5.5.5           # 阿里DNS
119、29.29.29        # 腾讯DNS
国际常用DNS
8、8.8.8             # Google DNS
1、1.1.1             # Cloudflare DNS
208、67.222.222      # OpenDNS

验证DNS配置

测试DNS解析

测试域名解析
nslookup google.com
dig google.com
测试特定DNS服务器
nslookup google.com 8.8.8.8
查看当前使用的DNS
cat /etc/resolv.conf
systemd-resolve --status  # CentOS 7
resolvectl status         # CentOS 8+

检查网络连接

检查网络接口
ip addr show
检查路由
route -n
测试网络连通性
ping -c 3 8.8.8.8

完整配置示例

1. 使用nmcli配置
nmcli connection modify ens33 ipv4.dns "223.5.5.5 114.114.114.114"
nmcli connection modify ens33 ipv4.ignore-auto-dns yes
nmcli connection up ens33
2. 验证配置
nmcli connection show ens33 | grep ipv4.dns
3. 测试DNS
nslookup baidu.com
4. 重启网络服务(可选)
systemctl restart NetworkManager

选择其中一种方法配置即可,推荐使用nmcli命令,这是当前CentOS版本中最稳定和推荐的方式。

文章摘自:https://idc.huochengrm.cn/dns/18072.html

评论