檢查配置文件
/etc/hostname
和 /etc/hosts
文件中的主機名已更新為正確值,格式正確且無語法錯誤。cloud-init
,需將 /etc/cloud/cloud.cfg
中的 preserve_hostname
設為 false
,并重啟系統。重啟相關服務
sudo systemctl restart networking
或 sudo netplan apply
(適用于使用 netplan 的系統)。nscd
),需重啟緩存服務:sudo systemctl restart nscd
。永久設置主機名
hostnamectl
命令永久修改主機名:sudo hostnamectl set-hostname 新主機名
,需重啟系統生效。驗證與排查
hostname
或 hostname -f
命令驗證主機名是否生效。sudo journalctl -xe
)排查是否有錯誤信息。注意:部分步驟需管理員權限(使用 sudo
),確保操作前備份重要配置文件。