hostname是标识网络上一台机器的标签。你应该在同一个网络上在不一样的机器上使用不一样的hostname。node
在Ubuntu之前的16.04版本和大部分的Linux上,能够直接修改/etc/hostname文件便可。但在Ubuntu18.04,直接修改后,机器重启后又会恢复原始的设置,能够参照下面的办法设置:linux
显示当前的 hostname:ubuntu
hostnamectl
The following steps outline how to change the hostname in Ubuntu 18.04.bash
改变 hostname,使用hostnamectl
。网络
在Ubuntu 18.04,咱们改变 system hostname 和相关的设置使用命令 hostnamectl
。oop
设置hostname 为 linuxize,能够使用以下的命令:
post
sudo hostnamectl set-hostname linuxize
hostnamectl
命令不产生输出,返回0 标识成功, 非0表示失败。编辑/etc/hosts文件
。spa
打开 /etc/hosts
文件,修改旧的 hostname 为新的:code
命令: sudo nano /etc/hostsrouter
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是否已经改变。