60

This may be related to running 18.04 LTS as a virtual machine (Host is Win 2016 DC) but I can't figure it out how. I have tried every method of I know of to change the hostname but it always reverts to the hostname I entered when I built the machine on reboot. I have tried the following:

  1. hostnamectl set-hostname xxx.
  2. Editing hostname directly.
  3. Adding --static to hostnamectl.
  4. Editing hosts file and adding desired hostname.
  5. Searching drive for references to old hostname before reboot.
  6. A few other weird suggestions I found on the internet not worth mentioning.

I have no DNS for this server at this time.

Example:

XXXX@iwrxmail:~# hostnamectl
Static hostname: iwrxmail
Pretty hostname: Interwrx Ubuntu Mail Server
Transient hostname: ctl
     Icon name: computer-vm
       Chassis: vm
    Machine ID: 01ef0d836d2c4945b51a4fab8e506381
       Boot ID: e6608fe238d843f883cde52af7631a79
Virtualization: microsoft
Operating System: Ubuntu 18.04 LTS
        Kernel: Linux 4.15.0-20-generic
  Architecture: x86-64
XXXX@iwrxmail:~# hostnamectl set-hostname test
XXXX@iwrxmail:~# hostnamectl
Static hostname: test
     Icon name: computer-vm
       Chassis: vm
    Machine ID: 01ef0d836d2c4945b51a4fab8e506381
       Boot ID: e6608fe238d843f883cde52af7631a79
Virtualization: microsoft
Operating System: Ubuntu 18.04 LTS
        Kernel: Linux 4.15.0-20-generic
  Architecture: x86-64
root@iwrxmail:~# cat /etc/hostname
test
root@iwrxmail:~# cat /etc/hosts
127.0.0.1       localhost.localdomain   localhost
::1             localhost6.localdomain6 localhost6
# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts 

However on reboot I get this again ....

XXXX@iwrxmail:~# hostnamectl
Static hostname: iwrxmail
     Icon name: computer-vm
       Chassis: vm
    Machine ID: 01ef0d836d2c4945b51a4fab8e506381
       Boot ID: 25a00676b22048eb8d43492c9de4f147
Virtualization: microsoft
Operating System: Ubuntu 18.04 LTS
        Kernel: Linux 4.15.0-20-generic
Architecture: x86-64

I had had this issue with the beta of 18.04, but I figured I would wait for the release copy. I also considered it might be a problem with the fact I had cloned the VMs, but this was a brand new version built from scratch with the just released distro.

What am I missing?

Thanks in advance.

7 Answers7

83

First edit /etc/cloud/cloud.cfg and set the parameter "preserve_hostname" from "false" to "true" and then edit /etc/hostname.

  • 6
    Works like a charm. I would have never gotten that from the comment line above it: " # This will cause the set+update hostname module to not operate (if true) ". Thanks so much, so many incorrect methods out on the web. – Andy Wylde Apr 27 '18 at 14:39
  • This file does not exists in my os. Not even /etc/cloud folder – Sky Dec 19 '19 at 12:02
  • I think it would be more correct to edit where the old hostname is explicitly left (see Levi H's proposal further down), namely in '/var/lib/cloud/seed/nocloud-net/user-data', than to simply disable reading that file by editing '/etc/cloud/cloud.cfg'. – longman Jul 14 '21 at 19:33
  • @longman Except I here (a Debian in a vserver) don't see your path, but I do see /etc/cloud. – Jürgen A. Erhard May 10 '22 at 18:14
  • it works in Ubuntu 22.04.3 LTS – mahfuz Sep 06 '23 at 14:04
  • This ruined my afternoon - I wanted to change hostnames with a setup script and this dumb cloud-init thing kept reverting the - thanks – bsautner Sep 15 '23 at 19:04
28

The hostname is being reset by cloud-init which can either be disabled as follows (after which you can set the hostname in the normal way e.g. using hostnamectl):

sudo touch /etc/cloud/cloud-init.disabled

Or you can use cloud-init and create/modify the user-data file (usually found at: /var/lib/cloud/seed/nocloud-net/user-data) so that the hostname: entry is set to the desired hostname (provided preserve_hostname: is not set). Firstly you'll need to clean the existing config:

sudo cloud-init clean

And then reinitialise cloud-init's config from the new/modified user-data file:

sudo cloud-init init

Then reboot. See the cloud-init docs for more details.

Pierz
  • 3,063
  • 1
  • 25
  • 15
  • 4
    Be carful with running cloud-init clean or cloud-init init it can rewrite your /etc/netplan/ yaml files. – Michael D. Sep 11 '18 at 13:37
  • 5
    It will also update your SSH host key which can be potentially harmful (clients need to accept the new host key before they can reconnect) – Per Lundberg Jun 06 '19 at 10:23
11

For the "lazy guys" like me, a copy-paste solution :)

sudo sed -i '/preserve_hostname: false/c\preserve_hostname: true' /etc/cloud/cloud.cfg && sudo hostnamectl set-hostname ReplaceThisWithTheHostnamePreferred

First command allows the new hostname to be remembered by the OS.

The second part (after the &&) will only run if the first part has finished successfully and will set the hostname to the desired value.

Regards! L

linux64kb
  • 1,119
6

1- Edit vi /etc/cloud/cloud.cfg and change

preserve_hostname: false

To

preserve_hostname: true

Save and exit.

2- Edit vi /etc/hostname and replace your new name in this file or you can do this step with bellow command.

hostnamectl set-hostname NEWNAME

Enjoy it :)

Milad Norouzi
  • 61
  • 1
  • 3
4

If you're not happy with leaving an older version somewhere, then simply open the file at /var/lib/cloud/seed/nocloud-net/user-data, and change your hostname at the line:

hostname: cm-lc-nc

Then run:

cloud-init clean
cloud-init init

It will then set /etc/hostname to the new value and will remain consistent across reboots. This will remove all traces of the previous hostname and in the event that preserve_hostname is either reset or ignored for some reason, you still won't lose your new hostname.

Levi H
  • 151
  • 1
    Your proposal is absolutely right as you have pointed out where the old hostname is explicitly left! And of course, it is more correct to edit there ( /var/lib/cloud/seed/nocloud-net/user-data ) than to simply disable reading that file by editing '/etc/cloud/cloud.cfg' as suggested in the high voted answer on top. – longman Jul 14 '21 at 19:23
1

I had the same issue and found that, after removing the cloud packages, you can change your hostname.

apt remove cloud-init cloud-initramfs-copymods cloud-initramfs-dyn-netconf
Eliah Kagan
  • 117,780
Maddin
  • 19
  • 1
  • 1
    I'm sure this will work but I think I prefer the answer above. Thanks for responding though – Andy Wylde Apr 27 '18 at 14:43
  • I suggest to refrain from this solution if you are using ubuntu-server package. Why? Because the above mentioned packages are prerequisites of it. Description of the ubuntu-server package 'Description-en: The Ubuntu Server system This package depends on all of the packages in the Ubuntu Server system . It is also used to help ensure proper upgrades, so it is recommended that it not be removed.' – linux64kb May 30 '18 at 17:29
  • This fixed it for me. Like an idiot I'd installed clount-init into the root Proxmox cluster. Nothing went wrong until the first reboot, then proxmox was down because hostname kept changing. – paul_h Oct 12 '20 at 17:10
1

For those arriving here on attempting to set the hostname on a read-only OS (such as a Live Ubuntu 20.04 USB drive with persistence) the following worked:

sudo crontab -e

The last line add a @reboot command as follows. Change myhostname to the name you want to use:

@reboot sudo hostnamectl set-hostname myhostname

Big thanks to @hs.chandra for the crontab trick

tresf
  • 922