Skip to main content
In modern software, virtualization allows you to create a virtual computer—or virtual machine (VM)—within your actual computer. This capability is particularly valuable in server environments because it lets a single physical machine serve multiple clients simultaneously. For instance, imagine a powerful server equipped with 64 CPU cores and 1024 GB of RAM. By creating 32 virtual machines, each VM can be allocated 2 virtual CPUs (vCPUs) and 32 GB of RAM. Rather than renting out one enormous server to a single client, you can offer 32 smaller virtual servers. This is the foundation of cloud compute services provided by DigitalOcean, Amazon Web Services, and Google Cloud.
The image illustrates a comparison of server resources, showing 32 virtual machines with 2 virtual CPUs and 32 GB RAM each, totaling 64 CPU cores and 1024 GB RAM, alongside logos of cloud service providers Digital Ocean, AWS, and Google Cloud.
Among many available tools for virtualization on Linux, QEMU-KVM has become the most popular. QEMU (Quick Emulator) emulates virtual computers, while KVM (Kernel-based Virtual Machine) integrates into the Linux kernel to leverage hardware acceleration for enhanced performance. In this guide, we focus on using a command-line tool called virsh (or VIRSH) to manage virtual machines. If you’ve used VirtualBox before, you might recall its graphical interface for VM creation, configuration, and management. Virsh, however, uses terminal commands to achieve similar tasks, making it a powerful choice for administrators.
The image shows a diagram with the Linux logo, labeled "Linux (QEMU-KVM)," and arrows pointing to "Quick Emulator" and "Kernel-based Virtual Machine."

Getting Started

To quickly begin, install the virt-manager package. Although virt-manager is designed for systems with a GUI, installing it will bring in many useful dependencies for headless or text-based environments. Run the following command:
When installing virt-manager, your package manager will fetch several dependency packages. The output may look similar to this:
Even though you may not use the virt-manager graphical interface, its installation provides several utilities essential for virtual machine management. Below is a snippet of further installation output:

Creating a Virtual Machine Configuration

Let’s proceed with creating a configuration file for a virtual machine. Begin by creating a directory to store your VM definitions and then create an XML file using your preferred text editor:
The XML file below defines a virtual machine named “TestMachine” running under QEMU. It allocates 1 GiB of RAM, 1 vCPU, and uses a 64-bit (x86_64) architecture with hardware-assisted virtualization (HVM):
In a production setting, a complete VM configuration will include additional parameters such as storage, network interfaces, and the operating system. For demonstration purposes, this basic setup is sufficient. Define the virtual machine using the following command:
You should see an output similar to:

Managing Virtual Machines with virsh

The virsh tool has an extensive help page. To display it, use:
By default, only active domains are listed. To view all defined domains (including inactive ones), run:
You should see “TestMachine” listed, albeit with the state “shut off”.

Starting and Managing VM States

To start your virtual machine, execute:
If your VM name includes spaces, enclose it in double quotes. Once started, verify its state with:
To reboot the virtual machine gracefully – allowing software applications to exit properly – run:
If the machine becomes unresponsive, you can force a reset, which is analogous to pressing a hardware reset button:

Shutting Down and Destroying VMs

To shut down the virtual machine gracefully (if the guest OS is present and active), use:
For instance:
Since this test VM may not have an operating system installed, the shutdown command might not work as expected. However, in real scenarios, it allows applications to close properly. If the VM is unresponsive, you can force a hard power off (equivalent to unplugging the machine) as follows:
Sample workflow:
The destroy command only powers off the VM abruptly—it does not remove the VM’s definition. To completely remove the VM, you must undefine it.
To remove the VM’s configuration, execute:
If you also want to remove any associated storage files, use:
After undefining, verify removal with:
If needed, you can recreate the VM by redefining the XML file:

Enabling Autostart

To ensure that your virtual machine automatically starts when the host system boots, enable autostart:
Conversely, disable autostart with:

Modifying VM Resources

To inspect the resources assigned to a VM, use:

Changing vCPU Count

Suppose you wish to increase the number of vCPUs from one to two. First, check the current allocation:
Use the setvcpus command to change the configuration. You can see command options by viewing:
The image shows a list of command-line options or functions, likely related to virtualization or system management, displayed in a terminal interface. The command "setvcpus" is highlighted.
The help output shows the syntax and options:
To permanently change the vCPU count (affecting the next boot), run:
If you encounter an error indicating that the requested vCPUs exceed the current maximum (e.g., “2 > 1”), update the maximum allowed vCPUs with:
Remember that modifying the vCPU count does not change a running VM. You must destroy (power off) the VM first:
Then, start it again:
Verify the changes with:

Changing Memory Allocation

To adjust memory allocation, first set the maximum allowed memory and then modify the allocation. For example, to change the memory to 2048 MB, run:
Then, shut down the machine gracefully (or force shutdown if necessary) and restart it:
Finally, confirm the configuration update:
A typical output might be:
This confirms that your resource modifications have taken effect.

Conclusion

This guide walked you through creating and managing virtual machines using virsh. By following these steps, you can leverage virtualization to efficiently allocate server resources and adapt configurations to meet your specific needs. For further details, consider exploring additional Kubernetes Documentation or the Terraform Registry for advanced deployment scenarios.

Watch Video