hostname是标识网络上一台机器的标签。你应该在同一个网络上在不同的机器上使用不同的hostname。
在Ubuntu以前的16.04版本和大部分的Linux上,可以直接修改/etc/hostname文件即可。但在Ubuntu18.04,直接修改后,机器重启后又会恢复原始的设置,可以参照下面的办法设置:
显示 Hostname
显示当前的 hostname:
hostnamectl
修改Hostname
The following steps outline how to change the hostname in Ubuntu 18.04.
-
改变 hostname,使用
hostnamectl
。在Ubuntu 18.04,我们改变 system hostname 和相关的设置使用命令
hostnamectl
。设置hostname 为
linuxize,可以使用如下的命令:
sudo hostnamectl set-hostname linuxize
hostnamectl
命令不产生输出,返回0 标识成功, 非0表示失败。 -
编辑/etc/hosts文件
。打开
/etc/hosts
文件,修改旧的 hostname 为新的:命令: sudo nano /etc/hosts
127.0.0.1 localhost 127.0.0.1 linuxize # The following lines are desirable for IPv6 capable hosts ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters
-
编辑cloud.cfg
文件。如果
cloud-init
package 安装了,需要修改cloud.cfg
文件。该软件包通常缺省安装用于处理 cloud instances的初始化。查看是否安装:
如果显示如下,咋么有安装,不需要进一步处理。ls -l /etc/cloud/cloud.cfg
如果像下面,则该模块已经安装:ls: cannot access '/etc/cloud/cloud.cfg': No such file or directory
需要打开-rw-r--r-- 1 root root 3169 Apr 27 09:30 /etc/cloud/cloud.cfg
/etc/cloud/cloud.cfg
然后修改preserve_hostname
从false
到true
:命令:sudo nano /etc/cloud/cloud.cfg
# This will cause the set+update hostname module to not operate (if true) preserve_hostname: true
确认修改
确认已经修改成功,命令如下:
hostnamectl
查看里面的hostname是否已经改变。