<p><a href="https://bugs.launchpad.net/cloud-init/+bug/1966533" rel="nofollow noreferrer"><strong>This is a bug in <code>cloud-init</code>, I've filed it upstream as Bug #1966533</strong></a></p>
<p>I was getting silent failures because the hostname was</p>
<pre><code>host-10-2-65-89.openstack.build.
</code></pre>
<p>Instead of</p>
<pre><code>host-10-2-65-89.openstack.build
</code></pre>
<p>What actually happens is,</p>
<pre class="lang-shell prettyprint-override"><code>$ sudo hostname host-10-2-65-89.openstack.build.
hostname: the specified hostname is invalid
</code></pre>
<hr />
<p>Background, what I was actually doing was using Terraform,</p>
<pre><code>fqdn: &quot;${fqdn}&quot;
</code></pre>
<p>And then setting the <code>fqdn</code> like this,</p>
<pre><code>fqdn = openstack_networking_port_v2.port_company.dns_assignment[0].fqdn
</code></pre>
<p>But the problem is the terraform was return the actual FQDN,</p>
<pre><code>host-10-2-65-89.openstack.build.
</code></pre>
<p>But you can't provide that to hostname. My solution was to remove the dot (<code>.</code>) with <a href="https://www.terraform.io/language/functions/trimsuffix" rel="nofollow noreferrer"><code>trimsuffix</code></a></p>
<pre><code>fqdn = trimsuffix(
  openstack_networking_port_v2.port_company.dns_assignment[0].fqdn,
  &quot;.&quot;
)
</code></pre>

CC BY-SA 4.0
2022-03-26T01:11:45.407Z
2022-06-16T06:25:16.843Z
2022-06-16T06:25:16.843Z
Evan Carroll
<p><a href="https://cloudinit.readthedocs.io/en/latest/topics/modules.html#set-hostname" rel="nofollow noreferrer"><code>prefer_fqdn_over_hostname</code> is documented as</a>,</p>
<blockquote>
<p>If both <code>fqdn</code> and <code>hostname</code> are set, it is distro dependent whether <code>hostname</code> or <code>fqdn</code> is used, unless the <strong><code>prefer_fqdn_over_hostname</code> option is <code>true</code> and <code>fqdn</code> is set it will force the use of FQDN in all distros</strong>, and if false then it will force the <code>hostname</code> use.</p>
</blockquote>
<p>In my <code>cloud-init.cfg</code> I have,</p>
<pre><code>hostname: &quot;host-10-2-65-89&quot;
fqdn: &quot;host-10-2-65-89.openstack.build.&quot;
prefer_fqdn_over_hostname: true
</code></pre>
<p>But when I run this my hostname is set to <code>host-10-2-65-89</code>. Here is the log, and you can see it explicitly says <code>host-10-2-65-89.openstack.build.</code></p>
<pre><code>249 - cc_set_hostname.py[DEBUG]: Setting the hostname to host-10-2-65-89.openstack.build. (host-10-2-65-89)
250 - util.py[DEBUG]: Reading from /etc/hostname (quiet=False)
250 - util.py[DEBUG]: Read 7 bytes from /etc/hostname
250 - util.py[DEBUG]: Writing to /etc/hostname - wb: [644] 16 bytes
250 - __init__.py[DEBUG]: Non-persistently setting the system hostname to host-10-2-65-89
250 - subp.py[DEBUG]: Running command ['hostname', 'host-10-2-65-89'] with allowed return codes [0] (shell=False, capture=True)
256 - atomic_helper.py[DEBUG]: Atomically writing to file /var/lib/cloud/data/set-hostname (via temporary file /var/lib/cloud/data/tmpaxqv667c) - w: [644] 80 bytes/chars
</code></pre>
<p>The contents of <code>/var/lib/cloud/data/set-hostname</code></p>
<pre class="lang-json prettyprint-override"><code>{
 &quot;fqdn&quot;: &quot;host-10-2-65-89.openstack.build.&quot;,
 &quot;hostname&quot;: &quot;host-10-2-65-89&quot;
}
</code></pre>
<p>The contents of <code>/var/lib/cloud/data/previous-hostname</code></p>
<pre><code>host-10-2-65-89.openstack.build.
</code></pre>
<p>I'm using <code>Cloud-init v. 21.4-0ubuntu1~20.04.1</code></p>
<hr />
<p>I've tried deleting all but the FQDN and it still doesn't set the <code>/etc/hostname</code> to the fqdn.</p>

<ubuntu><cloud-init>
How come my hostname isn't set to the FQDN with cloud-init when using `prefer_fqdn_over_hostname`?
Evan Carroll
# How come my hostname isn't set to the FQDN with cloud-init when using `prefer_fqdn_over_hostname`?

(Asked by: username_0 on 2022-03-25)

prefer_fqdn_over_hostname is documented as,

If both fqdn and hostname are set, it is distro dependent whether hostname or fqdn is used, unless the prefer_fqdn_over_hostname option is true and fqdn is set it will force the use of FQDN in all distros, and if false then it will force the hostname use.

In my cloud-init.cfg I have,
hostname: "host-10-2-65-89"
fqdn: "host-10-2-65-89.openstack.build."
prefer_fqdn_over_hostname: true

But when I run this my hostname is set to host-10-2-65-89. Here is the log, and you can see it explicitly says host-10-2-65-89.openstack.build.
249 - cc_set_hostname.py[DEBUG]: Setting the hostname to host-10-2-65-89.openstack.build. (host-10-2-65-89)
250 - util.py[DEBUG]: Reading from /etc/hostname (quiet=False)
250 - util.py[DEBUG]: Read 7 bytes from /etc/hostname
250 - util.py[DEBUG]: Writing to /etc/hostname - wb: [644] 16 bytes
250 - __init__.py[DEBUG]: Non-persistently setting the system hostname to host-10-2-65-89
250 - subp.py[DEBUG]: Running command ['hostname', 'host-10-2-65-89'] with allowed return codes [0] (shell=False, capture=True)
256 - atomic_helper.py[DEBUG]: Atomically writing to file /var/lib/cloud/data/set-hostname (via temporary file /var/lib/cloud/data/tmpaxqv667c) - w: [644] 80 bytes/chars

The contents of /var/lib/cloud/data/set-hostname
{
 "fqdn": "host-10-2-65-89.openstack.build.",
 "hostname": "host-10-2-65-89"
}

The contents of /var/lib/cloud/data/previous-hostname
host-10-2-65-89.openstack.build.

I'm using Cloud-init v. 21.4-0ubuntu1~20.04.1

I've tried deleting all but the FQDN and it still doesn't set the /etc/hostname to the fqdn.

**Tags:** ubuntu, cloud-init


---

## 1 answers

### Answer by username_0 on 2022-03-26. Score: 3 | Accepted answer

This is a bug in cloud-init, I've filed it upstream as Bug #1966533
I was getting silent failures because the hostname was
host-10-2-65-89.openstack.build.

Instead of
host-10-2-65-89.openstack.build

What actually happens is,
$ sudo hostname host-10-2-65-89.openstack.build.
hostname: the specified hostname is invalid


Background, what I was actually doing was using Terraform,
fqdn: "${fqdn}"

And then setting the fqdn like this,
fqdn = openstack_networking_port_v2.port_company.dns_assignment[0].fqdn

But the problem is the terraform was return the actual FQDN,
host-10-2-65-89.openstack.build.

But you can't provide that to hostname. My solution was to remove the dot (.) with trimsuffix
fqdn = trimsuffix(
  openstack_networking_port_v2.port_company.dns_assignment[0].fqdn,
  "."
)


---

