CentOS怎么配置DNS域名?

HCRM技术_小炮 DNS 2025-10-15 112 6

在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

评论

精彩评论
  • 2026-03-07 04:56:12

    在CentOS中配置DNS域名解析,推荐使用nmcli命令进行临时或永久修改,方法简单,操作便捷。

  • 2026-03-27 02:08:17

    在CentOS中配置DNS域名,首先编辑`/etc/resolv.conf`文件,设置`nameserver`指向目标DNS服务器地址,然后重启网络服务或重启系统使配置生效。

  • 2026-04-04 06:47:29

    在CentOS中配置DNS域名解析有三种常用方法:使用nmcli命令、修改配置文件和使用systemd-resolved,推荐使用nmcli命叧,这是当前最稳定和推荐的方式之一来设置和管理网络连接和相关的网络参数包括dns服务器地址等。。

  • 2026-04-04 19:51:46

    CentOS配置DNS域名,首先编辑`/etc/resolv.conf`文件,设置`nameserver`指向所需DNS服务器地址,然后重启网络服务或重启系统使配置生效。

  • 2026-04-13 14:08:04

    CentOS配置DNS域名,编辑/etc/resolv.conf文件,设置nameserver和search选项。

  • 2026-09-13 17:49:38

    CentOS配置DNS域名需编辑`/etc/resolv.conf`文件,设置`nameserver`指向目标DNS服务器地址,重启网络服务即可生效。