Essential KVM CLI Tools for Administrators – virt-edit, virt-df, virt-clone

It takes more than starting and stopping them to manage virtual machines efficiently. KVM is a set of command-line tools that allow administrators to control VM configuration, storage, and duplication. The tools like virt-edit, virt-df, and virt-clone are useful as they help you edit guest configuration files without needing to boot up the VM as well as monitor how much disk space is used across virtual machines. It’s also possible to quickly create a perfect duplicate of an existing instance.
In this guide, you will see how these tools help you administer VMs. There are many tools here that you can use to make your life easier.

🔥 TRENDING - Our #1 Selling eBook

Mastering KVM Virtualization - The Ultimate eBook

From home labs to production clouds - master KVM Host management, automating KVM administration using Terraform, Vagrant, and cloud automation. This eBook will enable you to build scalable virtual infrastructure that works whether you're learning at home or deploying enterprise solutions. Get your full copy today

Only $10 $20
Get Instant Access →

1. virt-edit – Edit a file in a virtual machine

Usevirt-edit command to edit files within virtual machine disk images. With this utility files can be modified in a guest VM without booting it. It is a useful tool for quick file changes, such as updating configuration files or fixing errors in a guest VM.

To get usage help guide, run:

virt-edit --help

Commonly used options:

  • -d or --domain – Specify guest name to edit
  • -a or --add – Specify file or remote disk URI
  • -c or --connect – Connect using given Libvirt URI
  • --mount -m dev[:mountpoint[:options[:fstype]]] – Mount the named partition or logical volume on the given mountpoint.
  • -e EXPR – non-interactive edit using perl expressions
  • -b or --backup – Create a backup of the original file in the guest disk image. The backup will have original filename with “extension” added.
  • -a or --add – Specify file or remote disk URI

We can begin by setting VM name as variable:

VM_NAME=deb12

Always perform edit operations in an offline guest (not in running state).

Example 1: Edit a configuration file in VM

To edit /etc/fstab in the VM:

sudo virt-edit -d $VM_NAME /etc/fstab
Example 2: Edit a configuration file in VM

To edit /etc/hostsfile inside a QCOW2 disk image:

DISK_IMAGE=/var/lib/libvirt/images/deb12.qcow2
sudo virt-edit -a $DISK_IMAGE /etc/hosts
Example 3: Edit file non-interactively

If Perl is installed, you can also edit file in a non-interactive manner. In this example we disabel ssh password authentication.

sudo virt-edit -d $VM_NAME \
-e 's/^#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
Example 4: Mount partition or logical volume before edit

If the mountpoint is omitted, it defaults to /. If you don’t know the filesystems in the disk image, refer to guestfish usage section for how-to guidance using list-partitions.

sudo virt-edit -d $VM_NAME \
-m /dev/sdc1:/mnt /mnt/filetoedit.txt

5. virt-df – Display free space on VM filesystems

The virt-df is useful when you want to display the disk usage statistics of virtual machine disk images. It works similar to the Unix df command but specifically targets guest VM’s filesystems.

Usage help and manuals can be checked using the following command:

virt-df --help
man virt-df

Useful command options:

  • -d or --domain – Specify guest name to edit
  • -a or --add – Specify file or remote disk URI
  • -c or --connect – Connect using given Libvirt URI. This is useful when working on remote KVM hosts.
  • -h or --human-readable – Print sizes in human-readable format.
  • -i --inodes – Print inodes instead of blocks.

Here are few examples on how to use virt-df

Example 1: Show disk usage for a single guest

The command below will print the disk usage for a single guest set in $VM_NAME. This can be replaced with a VM name.

sudo virt-df -h -d $VM_NAME

Output:

Filesystem                                Size       Used  Available  Use%
deb12-clone:/dev/sdb                      376K       376K          0  100%
deb12-clone:/dev/sda1                      20G       1.3G        17G    7%
deb12-clone:/dev/sda15                    124M        12M       112M   10%
Example 2: Show disk usage for disk image file

Show disk usage for a disk image file:

DISK_IMAGE=/var/lib/libvirt/images/deb12.qcow2
sudo virt-df -a $DISK_IMAGE

If a single guest has multiple disks, use the -a option repeatedly.

sudo virt-df -i -a <image1> -a <image2>

6. Virt-clone – Cloning Virtual Machines

The virt-clone command is used to create a copy of a virtual machine, changing all the unique host side configuration like MAC address, name, etc.

Available command options:

  • --connect URI – Connect to hypervisor with libvirt URI
  • --auto-clone – Automatically generate clone name and storage paths from the original guest configuration.
  • --name NEW_NAME – Name for the new guest
  • --file NEW_DISKFILE – New file to use as the disk image for the new guest
  • --nvram NEW_NVRAM – New file to use as storage for nvram VARS
  • --mac NEW_MAC – New fixed MAC address for the clone guest. Default is a randomly generated MAC
  • --print-xml – Print the generated domain XML rather than create the guest.
  • --quiet – Suppress non-error output
VM cloning usage examples:
  • Clone an existing VM basic standard example
# List running or stopped vms in the KVM host
sudo virsh list --all

# Shutdown VM before cloning
sudo virsh shutdown <vm-name>

# Clone an existing VM basic standard example
sudo virt-clone --original <vm-name> --name <cloned-vm-name> --auto-clone
  • Specify a new disk location for the cloned VM
sudo virt-clone --original <vm-name> \
--name <cloned-vm-name> --file /var/lib/libvirt/images/vmNameCloned.qcow2
  • Clone a VM with Custom MAC Address
sudo virsh clone --mac 52:54:00:12:34:56 vm1 vm2
  • Reset a cloned virtual machine.
sudo virt-sysprep --domain <cloned-vm-name> --hostname <hostname>
  • Destroy cloned VM
sudo virsh shutdown <cloned-vm-name>
sudo virsh undefine <cloned-vm-name>

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Unlock the Right Solutions with Confidence

At CloudSpinx, we don’t just offer services - we deliver clarity, direction, and results. Whether you're navigating cloud adoption, scaling infrastructure, or solving DevOps challenges, our seasoned experts help you make smart, strategic decisions with total confidence. Let us turn complexity into opportunity and bring your vision to life.

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

Proxmox is a specialized Linux distribution build from Debian GNU / Linux sources. It is created for users who want […]

Proxmox VE has support for multiple authentication methods – Linux PAM, integrated Proxmox VE authentication server, LDAP, Microsoft Active Directory […]

While tools like Virt-Manager make KVM virtualization easy to access, administrators often go to command line tools due to speed […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.