How to Rename QEMU Virtual Machines

How to Rename QEMU Virtual Machines
QEMU Logo - Licensed Under Creative Commons

I never intended this website to be a how-to site, but I had to find out how to rename a QEMU virtual machine, as I skipped the step in the wizard. So I thought I would put it here for future reference.


Let's say you were creating a VM running Ubuntu Server 24.04 to run Plex (not what I was doing) and left the name as default ubuntu24.01. This guide will show you how to rename it to plex. Replace the two VM names with your own.
I will also assume a default configuration (according to virt-manger's set-up wizard).

Switch to the image directory

This is optional, but it makes the commands much clear later on as you won't have to specify directories. Run cd /var/lib/libvirt/images.

Switch to the root account

This may not be nessecary depending on where your VMs are located, and sudo can be used with the commands, but it is simplier to run sudo su to elevate privileges, as the default path requires root access.

Use the virsh dumpxml command to dump the VM definition in XML

You can run the virsh dumpxml command to print out the VM configuration to the terminal. Run the following command to print and pipe into a file:
virsh dumpxml ubuntu24.04 > plex.xml

Undefine the VM

Undefining a VM will essentially remove the VM from libvirt, while keeping the image in tact. Run the following:
virsh undefine ubuntu24.04

(Optional) Rename the image

I want to rename the image from ubuntu24.04.qcow2 to plex.qcow2. Simply run the command:
mv ubuntu24.04.qcow2 plex.qcow2

Edit plex.xml

Open the xml file in your text editor of choice (nano is easiest, nano plex.xml). Find the <name> tag (should be the second line) and change the value to plex.

If you renamed the qcow2 image, you are going to have to edit the disk source file. Look for a block of code in the XML which looks like the following:

 <devices>
    <emulator>/usr/bin/qemu-system-x86_64</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' discard='unmap'/>
      <source file='/var/lib/libvirt/images/ubuntu24.04.qcow2'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x04' slot='0x00' function='0x0'/>

Change <source file='/var/lib/libvirt/images/ubuntu24.04.qcow2'/> to <source file='/var/lib/libvirt/images/plex.qcow2'/>. This will point to the newly renamed file.

Save and quit.

Define the VM

Run virsh define plex.xml. This will make the VM available to libvirt, with a new name!

Exit the root session

If you ran sudo su at the beginning, run exit to de-elevate from the root.