温馨提示×

centos如何替换域名解析

小樊
40
2026-09-18 10:10:34
栏目: 云计算

在 CentOS 中“替换域名解析”通常指修改系统使用的 DNS 服务器修改某个域名的解析结果。下面按常见场景说明。


一、修改 DNS 服务器(最常见)

CentOS 6 / 早期版本(直接改 resolv.conf)

vi /etc/resolv.conf

内容示例:

nameserver 8.8.8.8
nameserver 114.114.114.114

⚠️ 注意:如果使用了 NetworkManager,重启网络可能被覆盖。


CentOS 7 / 8(推荐用 nmcli)

查看网卡名:

nmcli connection show

修改 DNS(假设网卡是 eth0):

nmcli connection modify eth0 ipv4.dns "8.8.8.8 114.114.114.114"
nmcli connection modify eth0 ipv4.ignore-auto-dns yes
nmcli connection down eth0
nmcli connection up eth0

验证:

cat /etc/resolv.conf

二、临时替换某个域名的解析(hosts)

适合测试或强制解析

vi /etc/hosts

示例:

127.0.0.1   localhost
192.168.1.10 example.com

优先级:/etc/hosts > DNS

验证:

ping example.com

三、永久替换某个域名解析(无 DNS 权限时)

只能靠:

  • /etc/hosts
  • 或自建 DNS(如 dnsmasq)

使用 dnsmasq 示例

yum install dnsmasq -y

配置:

vi /etc/dnsmasq.conf
address=/example.com/192.168.1.10

启动:

systemctl enable dnsmasq
systemctl start dnsmasq

并将本机 DNS 指向:

nameserver 127.0.0.1

四、验证当前解析

nslookup example.com
# 或
dig example.com

如果你能说明:

  • CentOS 版本(6 / 7 / 8 / Stream)
  • 改 DNS 服务器还是改某个域名指向
  • 是否用于生产环境

我可以给你更精确的操作步骤。

0