在 CentOS 系统中,您可以通过以下步骤设置静态主机名:
hostnamectl
命令(适用于 systemd)查看当前主机名:
hostnamectl
设置新的主机名:
sudo hostnamectl set-hostname your-new-hostname
将 your-new-hostname
替换为您想要设置的主机名。
验证更改:
再次运行 hostnamectl
命令以确认主机名已更改:
hostnamectl
/etc/hostname
文件备份原始文件(可选但推荐):
sudo cp /etc/hostname /etc/hostname.bak
编辑 /etc/hostname
文件:
sudo nano /etc/hostname
将文件中的内容替换为您想要的主机名,然后保存并退出编辑器。
重启网络服务: 为了使更改生效,您需要重启网络服务:
sudo systemctl restart network
验证更改:
再次运行 hostnamectl
命令以确认主机名已更改:
hostnamectl
nmcli
命令(适用于 NetworkManager)如果您使用的是 NetworkManager,可以使用 nmcli
命令来设置主机名:
查看当前主机名:
nmcli general hostname
设置新的主机名:
sudo nmcli general hostname your-new-hostname
将 your-new-hostname
替换为您想要设置的主机名。
验证更改:
再次运行 nmcli general hostname
命令以确认主机名已更改:
nmcli general hostname
/etc/hosts
文件,将新的主机名添加到其中,以避免 DNS 解析问题。通过以上方法之一,您应该能够在 CentOS 系统中成功设置静态主机名。