![在 EC2 上运行的 CentOS 上通过 Cloud-Init 命名的主机名不起作用](https://linux22.com/image/620127/%E5%9C%A8%20EC2%20%E4%B8%8A%E8%BF%90%E8%A1%8C%E7%9A%84%20CentOS%20%E4%B8%8A%E9%80%9A%E8%BF%87%20Cloud-Init%20%E5%91%BD%E5%90%8D%E7%9A%84%E4%B8%BB%E6%9C%BA%E5%90%8D%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8.png)
我在 CentOS 6.4 的 EC2 实例的用户数据上设置了云配置,但看起来不适用。
#cloud-config
manage_etc_hosts: true
preserve_hostname: false
hostname: myserver01.example.com
在 cloud-init 日志中我看到:
cloud-init[DEBUG]: preserve_hostname is set. not managing hostname
我如何强制它设置主机名?
答案1
我还没有在 CentOS 6 AMI 上尝试过这个,但这适用于 CentOS 7:
NEWHOSTNAME="myserver01.example.com"
CLOUDTMP=`mktemp $TMPDIR/$(basename $0).XXXXXX 2>/dev/null` || exit 1
cat <<EOF > $CLOUDTMP
#!/bin/bash
hostnamectl set-hostname $NEWHOSTNAME
echo "preserve_hostname: true" > /etc/cloud/cloud.cfg.d/99_hostname.cfg
EOF
aws ec2 run-instances --user-data file://$CLOUDTMP --cli-input-json file://path/to/file.json