Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created an image off the shut down VM. From new XML template I was able to create a new VM using virsh create newvm.xml. basically the image was created by copying the image of a shut down VM which was built from Ubuntu OS image - obviously during the install I had to specify the hostname. Now I shut down the VM and copied the image to use it as a base image for my new VM guests. The problem is that I dont know how to change the damn hostname automatically. Now eache newly created VM is starting with the same hostname as the machine from which the image was created. Is there any way how to handle this ?

   <domain type='kvm' id='10'>
    <type arch='x86_64' machine='pc-1.0'>hvm</type>
    <boot dev='hd'/>
  <clock offset='utc'/>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/kvm/sensu/tmpZ2yf6n.qcow2'/>
      <target dev='hda' bus='ide'/>
      <alias name='ide0-0-0'/>
      <address type='drive' controller='0' bus='0' unit='0'/>
    <controller type='ide' index='0'>
      <alias name='ide0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    <interface type='bridge'>
      <mac address='52:54:00:50:89:7b'/>
      <source bridge='br0'/>
      <target dev='vnet0'/>
      <model type='virtio'/>
      <alias name='net0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    <serial type='pty'>
      <source path='/dev/pts/0'/>
      <target port='0'/>
      <alias name='serial0'/>
    <console type='pty' tty='/dev/pts/0'>
      <source path='/dev/pts/0'/>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='5900' autoport='yes' listen=''>
      <listen type='address' address=''/>
      <model type='cirrus' vram='9216' heads='1'/>
      <alias name='video0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    <memballoon model='virtio'>
      <alias name='balloon0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
  <seclabel type='dynamic' model='apparmor' relabel='yes'>

I thought that by specifying hostname in a tag it would be changed automatically on VM creation. Is there any way how I can achieve what I'm trying to ? Cheers.

share|improve this question

You cannot change the hostname from the XML file of the VM, nor from KVM. This XML file list only the properties of the VM, the hostname is not one such property. The hostname is set in the VM OS (Ubuntu in your case) during the boot process.

To change the hostname you should look at the /etc/hostname file in your Ubuntu server (see detailed examples).

If you're using dhcp to set up the IP address of your servers (which I'd recommend), dhcp-client can use some hooks to run a script when it receives an IP address. That way you can dynamically change the hostname during the boot process. Here is an example on how to do it.

Another option, like suggested by pedro, is to set the MAC address of the server in each new XML file (which you can do, as it is a VM property) and have a script running at boot time that will assign an IP address and a hostname derived from this MAC address.

share|improve this answer

I do this setting the MAC address on VM creation and tweaking the /etc/rc.local to look at the MAC and set IPs/hostname depending on its value. Regards, Pedro

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.