在 CentOS 上“切换域名”通常指以下几种情况之一,我先按常见场景给你说明,你可以对照自己的需求:
如果你的意思是把 CentOS 服务器的主机名改成某个域名(如 web.example.com):
hostnamectl set-hostname web.example.com
立即生效,重启也保留。
查看:
hostname
如果你是想让某个域名指向本机或别的 IP(类似“切换域名指向”):
编辑:
vi /etc/hosts
示例:
127.0.0.1 localhost
192.168.1.10 old.example.com
192.168.1.20 new.example.com
保存后立刻生效,不需要重启。
如果你是指网站从 old.com 切到 new.com:
vi /etc/nginx/conf.d/your_site.conf
修改:
server_name new.example.com;
然后:
nginx -t
systemctl reload nginx
vi /etc/httpd/conf.d/your_site.conf
修改:
ServerName new.example.com
然后:
systemctl reload httpd
如果你是想让用户访问 old.com 自动到 new.com:
在域名服务商后台,把 new.example.com 的 A 记录指向服务器 IP。
Nginx:
server {
listen 80;
server_name old.example.com;
return 301 https://new.example.com$request_uri;
}
vi /etc/resolv.conf
nameserver 8.8.8.8
nameserver 114.114.114.114
(CentOS 7+ 可能被 NetworkManager 覆盖,需改网卡配置)
如果你能具体说一下:
我可以给你更精确的命令。