Hostname keep changing when reboot

Hi,
Does anyone know why my hostname keeps being changed when I reboot it? I’m using this command:

sudo hostnamectl set-hostname aml-s905x-comp1

The change is supposed to be permanent, I’m using Ubuntu 22.04.
Thanks!

Hostname are set by different components on different images. On Ubuntu server, it is set by cloud-init so it should be modified in /etc/cloud.d/. On Raspbian, it should be /etc/hostname and /etc/hosts.

1 Like

I ended up modifying /etc/cloud/cloud.cfg and I’ve set preserve_hostname: true and it worked :slight_smile:

3 Likes

That’s the only way I could get it to “stick”, too: editing /etc/cloud/cloud.cfg - at first. Then, I edited user-data in /boot/efi and that stuck, too.

This is Ubuntu’s Server’s default behavior. We don’t like how it’s done but modifying their process would be changing distro default behavior and we want to avoid that when possible.

1 Like

@Keith_Weston same here. I did deep-dive into cloud-init but still no luck except to modify /etc/cloud/cloud.cfg.

@librecomputer That is not Ubuntu Server’s default behavior on Ubuntu’s downloadable official image for Raspberry Pi. “sudo hostnamectl set-hostname newname” persistently changes the hostname on Ubuntu’s official image after cloud-init finishes first boot (‘preserve_hostname’ is set to false in that image too).


HYPOTHESIS: Libre’s user-data file (see /var/lib/cloud/instance/user-data.txt) has the hostname explicitly set. Ubuntu’s official Raspberry Pi image does not. I noticed this because I dug up my ancient Raspberry Pi 2 to get to the bottom on this.

I think that’s the key difference? I only started looking into cloud-init today, so I think you know more.

The cloud-init docs are not very clear on what modules get run when, especially about hostnames… I think their expectation is that hostnames will be provided by a DHCP server so the setting ‘preserve_hostname: false’ makes some sense.

Doing ‘cloud-init clean’ and rebooting did not work: other inits happened, but hostname was same.

I added a custom rule to set the hostname (/etc/cloud/cloud.cfg.d/70_custom_hostname.cfg
containing "hostname: libre") and rebooted. I thought it might because the 'set_hostname'
module runs on init.

That’s what finally made me think it was something even lower level than files in /etc/cloud that were causing the problem.


Hope this helps.

Being unable to change the hostname is a pretty big ergonomic problem.

—> Especially since I just ordered three more lepotato’s :slight_smile: <-----

Other than a couple issues like this, I’m really enjoying working with your Ubuntu image on my Lepotatos. Great work, keep it up!

@Keith_Weston Yep, the key was commenting out the explicit setting of hostname in lepotato’s user-data in /boot/efi/user-data. (I kept /etc/cloud/cloud.cfg.d/70_custom_hostname.cfg with contents “hostname: newname”)

IMPORTANT: EDIT THE USER-DATA FILE IN /BOOT/EFI (not /var/lib/cloud/…). Comment out ‘hostname: aml-s905x-cc’ in /boot/efi/user-data, not the file in /var/lib/cloud/instance. On first boot/clean+reboot, the file from /boot/efi gets copied over into /var/lib/cloud.

After I do that things work as expected:

  1. when I do ‘cloud-init clean’ and reboot, the hostname specified in my 70_custom_hostname.cfg file is set.

  2. After step 1, if I just do ‘sudo hostnamectl set-hostname newname2’ and reboot, the name is not changed – this is as expected. Because I still have the 70_custom_hostname file that sets it to ‘newname’.

  3. After step 2, if I remove the the 70_custom_hostname file, then use hostnamectl to set the new name, the new name does persist.

@librecomputer hope this helps. I’m pretty confident now that the procedure I described above is how Ubuntu wants this to be done [1] – might be a weird edge case in ubuntu cloud-init. If I were you, I would:

a) set the aml-s905x-cc hostname rule in /etc/cloud/cloud.cfg.d so advanced users will notice the file. Or maybe even not do that? :slight_smile:

b) add a note on the download page explaining how to set the hostname. Anyone that buys more than one lepotato will immediately run into this, so I would prioritize this.

Again, thanks for all the work!

[1] The line ‘#hostname: ubuntu’ is explicitly commented out in Ubuntu’s downloadable image for Raspberry Pi.

This is mentioned in the release notes for the server image: Ubuntu 22.04.1 Jammy LTS Server Release Notes under Configuring System Settings such as First User or Hostname

1 Like