使用cloud init在CentOS上设置主机名

使用cloud init在CentOS上设置主机名,cloud,centos7,cloud-init,Cloud,Centos7,Cloud Init,我想在我的云配置文件中使用通用配置,手动设置已部署操作系统的主机名。cloud init的“主机名”对于ubuntu图像来说效果很好,但具体来说,它无法用于Centos云图像(Centos 7.8)。我看到这些条目是在/etc/hosts文件中创建的,但“hostname”命令仍然返回“localhost.localdomain” 这是我的云配置文件: preserve_hostname: true hostname: worker1 fqdn: worker1.local manage_etc

我想在我的云配置文件中使用通用配置,手动设置已部署操作系统的主机名。cloud init的“主机名”对于ubuntu图像来说效果很好,但具体来说,它无法用于Centos云图像(Centos 7.8)。我看到这些条目是在/etc/hosts文件中创建的,但“hostname”命令仍然返回“localhost.localdomain”

这是我的云配置文件:

preserve_hostname: true
hostname: worker1
fqdn: worker1.local
manage_etc_hosts: true
以下是“hostname”命令的输出:

[root@localhost ~]# hostname
localhost.localdomain
以下是/etc/hosts的内容:

[root@localhost ~]# cat /etc/hosts
# Your system has configured 'manage_etc_hosts' as True.
# As a result, if you wish for changes to this file to persist
# then you will need to either
# a.) make changes to the master file in /etc/cloud/templates/hosts.redhat.tmpl
# b.) change or remove the value of 'manage_etc_hosts' in
#     /etc/cloud/cloud.cfg or cloud-config from user-data
#
# The following lines are desirable for IPv4 capable hosts
127.0.0.1 worker1.local worker1
127.0.0.1 localhost.localdomain localhost
127.0.0.1 localhost4.localdomain4 localhost4
我不想采用“hostnamectl”来设置主机名,因为它不适用于ubuntu/其他操作系统

那么,我们如何使用cloudinit实现这一点呢

[版本信息: CentOS:7.8.2003 云初始化:18.5]