ubuntu18.04修改主机名

337 阅读1分钟

因业务需要,需要将ubuntu18.04上的hostname进行修改,特此记录下修改过程,仅供参考。

需求

ubuntu18.04修改hostname

步骤

  • 本地安装cloud-init
sudo apt install cloud-init
  • 修改/etc/cloud/cloud.cfg

将 'preserve_hostname'默认的false修改为true

sudo vim /etc/cloud/cloud.cfg

# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true
  • 修改主机名
# 修改主机名
sudo vim /etc/hostname

# 配置映射关系
sudo vim /etc/hosts
  • 重启系统,检查hostname是否生效