
在Linux系统中修改主机名后,需执行特定步骤使更改生效。方法因Linux发行版而异:
Systemd系统 (例如Ubuntu 16.04及以上、CentOS 7及以上):
-
修改主机名: 使用文本编辑器(例如
nano或vim)编辑/etc/hostname文件,输入新主机名,保存并关闭。 命令示例:sudo nano /etc/hostname -
更新hosts文件: 编辑
/etc/hosts文件,将旧主机名替换为新主机名,保存并关闭。 命令示例:sudo nano /etc/hosts -
重启Systemd-hostnamed服务: 执行
sudo systemctl restart systemd-hostnamed使更改生效。 -
(可选) 重启系统:
sudo reboot(通常无需重启,但重启可确保所有服务识别新主机名)。
SysVinit系统 (例如CentOS 6、Debian 8):
-
修改主机名: 使用文本编辑器编辑
/etc/sysconfig/network文件,找到HOSTNAME行,修改其值为新主机名,保存并关闭。 命令示例:sudo nano /etc/sysconfig/network -
更新hosts文件: 编辑
/etc/hosts文件,将旧主机名替换为新主机名,保存并关闭。 命令示例:sudo nano /etc/hosts -
重启网络服务: 执行
sudo service network restart使更改生效。 -
(可选) 重启系统:
sudo reboot(通常无需重启,但重启可确保所有服务识别新主机名)。
修改完成后,使用hostname命令验证主机名是否已更改。










