在 Ubuntu 系统中,设置主机名(hostname)通常可以通过以下步骤实现,并确保其永久生效:
临时设置主机名:
打开终端并使用 hostnamectl
命令来设置主机名。例如,将主机名设置为 myhostname
:
sudo hostnamectl set-hostname myhostname
永久设置主机名:
要使主机名在系统重启后仍然有效,你需要编辑 /etc/hostname
文件。
nano
或 vim
)打开 /etc/hostname
文件:sudo nano /etc/hostname
更新 /etc/hosts
文件:
为了确保主机名解析正确,你需要更新 /etc/hosts
文件,将新的主机名添加到文件中。
/etc/hosts
文件:sudo nano /etc/hosts
127.0.0.1 localhost
myhostname
替换为你设置的主机名:127.0.0.1 localhost myhostname
重启系统: 为了确保所有更改生效,建议重启系统:
sudo reboot
完成以上步骤后,你的 Ubuntu 系统应该会使用新的主机名,并且在重启后仍然保持该主机名。