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

3

我希望在我的云配置文件中使用通用配置,手动设置部署操作系统的主机名。cloud-init 的 "hostname" 对于 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或其他操作系统不适用。

那么我们如何使用cloud-init实现这一点呢?

[版本信息: CentOS:7.8.2003 cloud-init:18.5]

1个回答

0

你应该改变

preserve_hostname: True

preserve_hostname: False

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接