How to rename KVM virtual machine (VM) domain with virsh command

How do I rename KVM-powered virtual machine from foo to bar using virsh command line option on Linux?

There are two ways to rename a domain/VM. Let us see how to change the name of a domain or vm under KVM.

Method 1 – Rename KVM VM with virsh

The syntax is:
# virsh domrename {domain} {new-name}
To rename VM from foo to bar, type:
# virsh shutdown foo
Domain foo is being shutdown

Now rename a VM, run:
# virsh domrename foo bar
Domain successfully renamed

Start a VM/domain, enter:
# virsh start bar
Domain bar started

Method 2 – Rename KVM domain with virsh

First get domain information in XML and save it to a new file:
# virsh dumpxml foo > bar.xml
Edit the XML file and change the name between the <name></name>
# vi bar.xml
Update it as follows from foo to bar:

<name>bar</name>

Shutdown the foo domain/vm:
# virsh shutdown foo
Domain foo is being shutdown

You need to undefine the old VM name:
# virsh undefine foo
Domain foo has been undefined

Finally, import the edited XML file to define the VM bar:
# virsh define bar.xml
Domain bar defined from bar.xml

Start the domain bar, enter:
# virsh start bar
Domain bar started


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 4 comments so far... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
4 comments… add one
  • Programster Feb 18, 2017 @ 8:10

    Thanks for the post. I wouldn’t have discovered “virsh domrename” otherwise which is soooo much easier!

    • Davinken Oct 3, 2017 @ 18:30

      Apparently this useful domrename is not always available – at least not in the CentOS 6 or 7 platform so far, where I have KVM environments. But the alternate procedure works quite fine.
      (Or may I be looking in the wrong place for it?)
      Thanks!

  • Aram Iskenderian Mar 13, 2017 @ 2:52

    “Edit the XML file and change the name between the ”

    I would not recommend this.
    It is much safer to

    virsh edit

    That will land you in vi too, but the difference is, if you have a typing error, no changes will be saved.

  • chris Oct 29, 2020 @ 16:55

    I get error. How can I fix?

    sudo virsh list

     Id   Name          State
    -----------------------------
     4    vm-x86_64     running
     7    vm-x86_64-2   running
    

    virsh domrename vm-x86_64 kali

    error: failed to get domain 'vm-x86_64'

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ webmaster@cyberciti.biz