Home FAQ How To Rename KVM Guest Virtual Machine

How To Rename KVM Guest Virtual Machine

By sk
362 Views

This tutorial will teach you how to rename KVM guest virtual machine from command line in Linux. I have a few virtual machines running with different Linux flavors in my KVM server. For the sake of easy reference, I always name each VM with the OS version or application that is currently running on that VM. For example, if a VM is running with CentOS 8 minimal edition, I would name it as "centos8". If Apache web server is installed on the CentOS 8 VM, then the name of the KVM guest would be "apache_centos8" and so on. Today, I'd like to test Nginx on CentOS 8 VM, so renamed it to "nginx_centos8" as described below.

Rename KVM Guest Virtual Machine In Linux

We can change the name of a VM on KVM using virsh command line utility in two ways:

  • Using "domrename" command,
  • Manually modifying VM's XML configuration file.

First, we will see how to rename a KVM VM using "virsh domrename" command.

1. Rename KVM virtual machine using "virsh domrename"

The "virsh domrename" command is used to rename a domain. This command changes current domain name to the new name without making any changes in the configuration files.

The typical usage of "virsh domrename" command is:

virsh domrename <old-name> <new-name>

Please note that the domain must be inactive and without snapshots or checkpoints.

First, make sure the KVM guest virtual machine is turned off.

$ virsh list --all

Sample output:

 Id   Name           State
-------------------------------
 -    centos8        shut off
 -    centos8-uefi   shut off

As you can see, I have two KVM guests and both of them are powered off.

If the VM is running, simply turn it off using command:

$ virsh shutdown centos8

Now, I am going to the rename the "centos8" guest machine to "nginx_centos8" using "domrename" command like below:

$ virsh domrename centos8 nginx_centos8

Upon successful rename, you should see an output like below:

Domain successfully renamed

Rename KVM virtual machine using virsh domrename command

Now, verify if the VM's name has really been changed or not:

$ virsh list --all

Sample output:

 Id   Name            State
--------------------------------
 -    centos8-uefi    shut off
 -    nginx_centos8   shut off

You can also verify it from Virt-manager application as well.

Open Virt-manager and double click on the VM and then click "Show virtual hardware details" icon in the menu bar. You will see the name of the VM under the Overview section.

View KVM virtual machine details in Virt-manager application

That's it. We successfully renamed a VM named "centos8" to ""nginx_centos8". It is just a one-liner command and very easy to remember.

Next, we will see the manually way which requires some editing in the VM's XML config file.

2. Rename KVM virtual machine by modifying its XML file

Make sure the VM you are about to rename is turned off.

$ virsh list --all

If the VM is currently running, simply turn it off using command:

$ virsh shutdown centos8

Now export the old domain (VM) configuration details to a new domain using command:

$ virsh dumpxml centos8 > nginx_centos8.xml

The above command will copy the old configuration to a new configuration file named "nginx_centos8.xml" and save it in the current directory.

Undefine the old domain i.e. delete existing old domain configuration:

$ virsh undefine centos8

Sample output:

Domain centos8 has been undefined

Edit the new configuration file using your preferred text editor:

$ nano nginx_centos8.xml

Go to the <name> </name> field and change it as per your wish:

domain type='kvm'>
  <name>nginx_centos8</name>
  <uuid>270437c4-8dfb-40d1-a2d1-c497015c0f22</uuid>
  <metadata>
[...]

Rename KVM virtual machine by modifying its XML file

Define the new KVM virtual guest machine with the new XML config file:

$ virsh define nginx_centos8.xml

Sample output:

Domain nginx_centos8 defined from nginx_centos8.xml

Finally, start the new guest machine with new XML configuration:

$ virsh start nginx_centos8

Sample output:

Domain nginx_centos8 started

Verify if the new KVM virtual machine is running:

$ virsh list --all

Sample output:

 Id   Name            State
--------------------------------
 2    nginx_centos8   running
 -    centos8-uefi    shut off

Rename KVM virtual machine manually by editing its XML file

You can also verify if from the Virt-manager graphical interface as described in the previous section.

Hope it helps.

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More