Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to persist hostname change on Azure Ubuntu

I've created a base image in azure that i named "LinuxBase" .. I've deployed this image about 10 times now with names "Linux1" -> "Linux10". However, every time the machine is rebooted, the name reverts back to "LinuxBase".

I have

  • set /etc/hostname
  • updated /etc/hosts
  • run hostnamectl set-hostname

but none of these things persist across reboots.

like image 874
Richie Lai Avatar asked Sep 14 '14 12:09

Richie Lai


People also ask

How do I change the hostname on Azure VM Linux?

By default, the hostname is the same as the VM name when you create a new virtual machine in Azure. To run a cloud-init script to change this default hostname when you create a VM in Azure with az vm create, specify the cloud-init file with the --custom-data switch.

How do I find my Azure VM hostname?

You can download the service configuration file for a deployed service from the Configure blade of the service in the Azure portal. You can then look for the vmName attribute for the Role name element to see the host name. Keep in mind that this host name is used as a base for the host name of each role instance.


2 Answers

As stated in the docs, one should be able to change the hostname if desired. To make this change persist there's one more step tough.

You need to install the Azure Linux Agent http://azure.microsoft.com/en-us/documentation/articles/virtual-machines-linux-agent-user-guide/ and modify the configuration to monitor hostname changes and update the network. This can be done editing /etc/waagent.conf and setting the entry:

Provisioning.MonitorHostName=y

Once done, install the service with

sudo waagent -install

and then change the hostname and everything will be updated, both locally an on the network.

You can check how to change the hostname here in the docs http://azure.microsoft.com/en-us/documentation/articles/virtual-machines-linux-introduction/#hostnamechanges where it states that simply running

sudo hostname <newname>

Will do the trick.

Edit: Also you may need to edit the file /var/lib/waagent/ovf-env.xml and change the hostname there too

like image 55
Javi Romero Avatar answered Sep 22 '22 10:09

Javi Romero


Gooler's fix works, but the root cause of this issue is creating an image without running 'sudo waagent -deprovision' first - see https://azure.microsoft.com/en-gb/documentation/articles/virtual-machines-linux-capture-image/

'sudo waagent -deprovision' should only be run on a machine that was purpose built to create an image from; as it will be deprovisioned after the image is captured.

like image 22
jalike Avatar answered Sep 26 '22 10:09

jalike