Setting hostname with cloud-init not working on Fedora

0

Hi, I am trying to use cloud-init to set my VMs hostname on first boot. As I understand it, this parameter can be used at the run-instances command:

--user-data I2Nsb3VkLWNvbmZpZwpob3N0bmFtZTogdGVzdDcK

This is a base64 encoded string for:

#cloud-config
hostname: test7

This is functioning correctly for Ubuntu, where the hostname is set at first boot, but on Fedora the hostname remains unset. I am using the latest official image from the AMI Catalog in both cases.

Any help with getting this working would be appreciated.

feita há 2 meses84 visualizações
1 Resposta
2
Resposta aceita

Hello.

Try changing your user data script to something like this:
I think you should set "preserve_hostname:true" in cloud-init and run the "hostnamectl" command to set the host name.

#!/bin/bash

sed -i '$a(preserve_hostname:true)' /etc/cloud/cloud.cfg
hostnamectl set-hostname test7

I actually tried running it with my AWS account, and I was able to confirm that the host name had been changed as shown below.

[fedora@test7 ~]$ cat /etc/hostname
test7
profile picture
ESPECIALISTA
respondido há 2 meses
profile picture
ESPECIALISTA
avaliado há 2 meses
profile picture
ESPECIALISTA
avaliado há 2 meses
  • When starting with AWS CLI, you can set the user data as follows.

    aws ec2 run-instances --image-id ami-04d91473d6ab0eb84 --key-name kobayashi --count 1 --instance-type t2.micro --user-data $'#!/bin/bash\nsed -i \'$a(preserve_hostname:true)\' /etc/cloud/cloud.cfg\nhostnamectl set-hostname test7'
    
  • Thank you, that does indeed work, but it seems like something of a workaround. Is there a reason why the conventional method which works for me on Ubuntu fails on Fedora?

  • As shown below, the hostname itself appears to be reflected, but since the FQDN is not reflected, it appears that the hostname is not reflected.

    cat /var/lib/cloud/data/set-hostname
    {
     "fqdn": "ip-172-31-45-133.ap-northeast-1.compute.internal",
     "hostname": "test7"
    }
    

    Here, when I set the hostname in fqdn format, it was reflected in the hostname.

    cat /var/lib/cloud/data/set-hostname
    {
     "fqdn": "test7.kobayashi.net",
     "hostname": "test7"
    }
    
    cat /etc/hostname
    test7.kobayashi.net
    

    It is unclear whether this is a cloud-init specification or a bug, but I found someone reporting a similar problem at the URL below. https://github.com/canonical/cloud-init/issues/4656
    I think the loading order and behavior of cloud-init are probably different between Ubuntu and Fedora. https://github.com/canonical/cloud-init/issues/4656#issuecomment-1854484271

  • Looking at the script below, it seems to work the same as RHEL because it imports rhel. https://github.com/canonical/cloud-init/blob/d29b744e742d12e41e9490fb05e74537b4b768d7/cloudinit/distros/fedora.py
    In other words, I think that the same thing as explained in the URL below is also happening in Fedora's cloud-init. https://github.com/canonical/cloud-init/issues/4656#issuecomment-1854484271

    The difference between Ubuntu and Almalinux is that Almalinux sets prefer_fqdn to True, this value determines whether fqdn is preferred over hostname when prefer_fqdn_over_hostname is not given in the user-data.

  • Thank you for looking into this issue Riku_Kobayashi, I will review the information supplied.

Você não está conectado. Fazer login para postar uma resposta.

Uma boa resposta responde claramente à pergunta, dá feedback construtivo e incentiva o crescimento profissional de quem perguntou.

Diretrizes para responder a perguntas