Search code examples
amazon-web-servicesamazon-ec2cloud-init

set userdefined hostname in cloudinit


I want to know how to set hostname with current local ipv4 in it.

For example :

   "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
      "#cloud-config\n\n",
      "hostname: service-test-ASGTesting-$private_ipv4\n",
      "fqdn: service-test-ASGTesting-$private_ipv4.cloud-dev.test.com\n",

Solution

  • For Linux hosts I use the "hostname" command as well as echo the hostname into the hosts and hostname files in /etc.

    ex:

    hostname myhostname
    echo 'myhostname' > /etc/hostname
    echo '127.0.0.1 myhostname' >> /etc/hosts
    service hostname restart