- 1 Introduction
- 2 Comparison Between KVM and Other Virtualization Technologies
- 3 Virtual Machine Creation and Basic Operations
- 4 Network Configuration and Extensions
- 5 Storage Management and Disk Operations
- 6 Guest OS Installation and Operation
- 7 Practical Use Cases and Automation Tips
- 8 Troubleshooting and Common Error Solutions
- 9 Security and Performance Optimization
- 10 Conclusion and Learning Resources
- 11 Command Cheat Sheet and Common Configuration Examples
- 12 FAQ
- 12.1 Q1: What is the difference between KVM, VirtualBox, and VMware?
- 12.2 Q2: How can I back up and restore virtual machines?
- 12.3 Q3: Can I use USB devices inside a virtual machine?
- 12.4 Q4: How can I automatically start virtual machines when the host boots?
- 12.5 Q5: What should I do if a virtual machine is running slowly?
- 12.6 Q6: Network configuration does not work correctly. What should I check?
- 12.7 Q7: Can I build clusters or high-availability (HA) configurations with KVM?
Introduction
Ubuntu is one of the most widely used Linux distributions in the world, trusted across a wide range of environments from individual users to enterprise systems. It is commonly used for server construction and development environments. When you want to implement full-scale virtualization on Ubuntu, one of the most powerful solutions available is KVM (Kernel-based Virtual Machine).
KVM is a hypervisor-based virtualization technology built directly into the Linux kernel. By leveraging hardware-assisted virtualization features such as Intel VT and AMD-V, it delivers high performance and stability. As an open-source solution, KVM allows you to build a robust virtualization infrastructure while keeping costs low.
Virtualization technologies may seem complex at first. However, with the combination of Ubuntu and KVM, even beginners can relatively easily create and operate virtual machines. It is also highly recommended for intermediate users who want to efficiently utilize existing physical server resources, as well as engineers who aim to virtualize production systems.
This article provides a thorough explanation of how to build a KVM-based virtualization environment on Ubuntu, covering basic usage, practical operational techniques, and solutions to common issues. If you are considering introducing KVM or want to make better use of virtualization on Ubuntu, be sure to read through to the end.
Comparison Between KVM and Other Virtualization Technologies
There are many types of virtualization technologies available. In Ubuntu environments, commonly used options include KVM, VirtualBox, and VMware. This section explains the characteristics and differences of each, along with the advantages and disadvantages of choosing KVM.
Main Types of Virtualization Technologies
- KVM (Kernel-based Virtual Machine)
KVM is a virtualization feature built into the Linux kernel and requires Linux as the host OS. By utilizing hardware virtualization support such as Intel VT and AMD-V, it offers high virtual machine performance suitable for production use. Flexible operations are possible using command-line tools and management utilities such as virt-manager. - VirtualBox
VirtualBox is a desktop-oriented virtualization software provided by Oracle that runs on Windows, macOS, and Linux. It features an intuitive interface and is often used for personal or learning purposes. Compared to KVM, however, it is less suitable for high-load or commercial environments. - VMware (VMware Workstation / ESXi, etc.)
VMware is a commercial virtualization product widely used in enterprise environments. It offers rich features and strong support but requires paid licenses, which increases operational costs, especially in large-scale deployments.
Advantages of KVM
- High Performance and Stability
Because KVM operates as part of the Linux kernel, it offers efficient resource usage and fine-grained control over CPU and memory allocation. Its stability makes it suitable for real-world production systems. - Open Source and Cost-Free
KVM is fully open source and does not require licensing fees, making it ideal for building a virtualization platform on a limited budget. - Rich Management Tools and Automation
KVM provides both command-line tools (virsh, virt-install) and GUI tools (virt-manager), making it easy to automate operations and integrate with DevOps and infrastructure management workflows.
Disadvantages of KVM
- Linux-Only Host OS
KVM requires the Linux kernel and cannot be used as a host on Windows or macOS. - Linux Knowledge Required for Setup and Troubleshooting
Although GUI tools exist, advanced configuration and troubleshooting often require command-line operations and Linux-specific knowledge.
When Should You Choose KVM?
- When you want to efficiently utilize physical server resources
- When virtualization is needed for production-grade systems or servers
- When you want to build a full-featured virtualization platform at low cost
- When you want to automate Linux server operations and infrastructure management
KVM is recommended not only for experienced Linux users and engineers, but also for those planning to move toward serious server operations in the future.
Virtual Machine Creation and Basic Operations
Once the KVM environment is ready, you can start creating and operating virtual machines. KVM allows you to choose between a graphical management tool (virt-manager) and command-line operations (virt-install and virsh), depending on your use case. This section introduces both approaches.
Creating Virtual Machines Using virt-manager (GUI)
virt-manager is a tool that allows you to create and manage virtual machines through an intuitive graphical interface. If you are using a Linux desktop environment, virt-manager is especially convenient.
- Launch
virt-managerfrom the application menu or the command line. - Click the “New” button in the upper-left corner.
- Select the installation media location (such as an ISO image) and choose the guest OS type (e.g., Ubuntu or Windows).
- Configure CPU count, memory size, and disk capacity for the virtual machine.
- Review the settings and click “Finish” to create the virtual machine and start the installation process.
With virt-manager, you can easily start, stop, and restart virtual machines, take snapshots, and add network interfaces or storage devices.
Creating Virtual Machines Using virt-install (CLI)
For server use cases or remote environments, creating virtual machines from the command line is often more practical.
Below is an example of basic usage of the virt-install command.
sudo virt-install \
--name ubuntu-vm \
--memory 2048 \
--vcpus 2 \
--disk size=20 \
--cdrom /path/to/ubuntu.iso \
--os-type linux \
--os-variant ubuntu20.04 \
--network network=default \
--graphics vnc--name: Name of the virtual machine--memory: Allocated memory (in MB)--vcpus: Number of virtual CPU cores--disk size=20: Virtual disk size (in GB)--cdrom: Path to the ISO image used for installation--os-type,--os-variant: OS type and version--network: Network configuration--graphics: Graphics mode (e.g., VNC)
After executing the command, the virtual machine will automatically start and display the installation screen via VNC or another graphical interface.
Starting, Stopping, Deleting, and Taking Snapshots of Virtual Machines
KVM makes it easy to manage virtual machines using simple commands. Common operations include the following.
- Start a virtual machine
virsh start <virtual-machine-name>- Shut down a virtual machine
virsh shutdown <virtual-machine-name>- Force stop a virtual machine
virsh destroy <virtual-machine-name>- Delete a virtual machine (be careful if removing disks)
virsh undefine <virtual-machine-name>- Create a snapshot
virsh snapshot-create-as <virtual-machine-name> <snapshot-name>All of these operations can also be performed using the virt-manager GUI.
Network Configuration and Extensions
Network configuration is a critical aspect of operating virtual machines with KVM. While the default configuration is sufficient for many use cases, production environments often require customization. This section explains the basics of KVM networking and commonly used advanced configurations.
Differences Between Default NAT (virbr0) and Bridge Networking
When KVM is installed, a virtual network bridge called virbr0 is automatically created. This is a NAT-based network with the following characteristics.
- Characteristics of virbr0 (NAT mode)
- Virtual machines can access the external internet
- Direct access from the host or other networks requires port forwarding
- Ideal for home use, development, and testing environments
In contrast, bridge networking provides virtual machines with access equivalent to the physical network.
- Characteristics of Bridge Networking
- Virtual machines join the same network segment as the host
- Direct access from physical PCs and other servers is possible
- Ideal for production servers and publicly accessible services
Creating and Configuring a Custom Bridge (For LAN Access)
If you want to allow direct access to virtual machines from production servers or other PCs, you should configure a bridge network. Below is a general procedure example where the physical NIC on the host is eth0.
- Install bridge-utils (skip if already installed)
sudo apt install bridge-utils- Edit the network configuration file
Ubuntu 18.04 and later use Netplan.
Edit a configuration file such as/etc/netplan/01-netcfg.yaml. Example:
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
bridges:
br0:
interfaces: [eth0]
dhcp4: yes- Apply the configuration
sudo netplan apply- Assign the new bridge (br0) to the virtual machine network
Change the network adapter to br0 using virt-manager or virsh.
Static IP Assignment and Port Forwarding
- Static IP Assignment
You can configure a static IP address inside the guest OS or assign a fixed IP via the DHCP server based on the MAC address. - Port Forwarding (When Using NAT)
Using the virsh command or libvirt XML configuration, you can forward specific ports (such as SSH or web services) from the host to the virtual machine. Example (forwarding SSH port 22):
virsh nat-forward --network default --add-port tcp:2222:22Note: More advanced configurations are possible by editing libvirt configuration files or controlling access with firewalld.
Storage Management and Disk Operations
When operating virtual machines with KVM, storage design and disk management are extremely important. This section explains the types of virtual disks, how to create them, how to manage storage pools, and how to expand disks and use snapshots effectively.
Virtual Disk Types (qcow2 and raw) and Use Cases
KVM mainly supports the following two types of virtual disks.
- qcow2 format
- The standard virtual disk format for KVM
- Supports snapshots, disk compression, and space-efficient allocation
- Ideal for flexible operations and testing environments
- raw format
- A simple format with no compression or conversion
- Best suited for maximum performance and high I/O workloads
In most cases, qcow2 is recommended, but you should choose the format based on your specific requirements.
Creating and Managing Storage Pools
KVM manages disk resources used by virtual machines through a concept called storage pools.
- Default Storage Pool
After installation,/var/lib/libvirt/images/is used as the default storage pool.
Virtual disks are created inside this directory. - Creating a New Storage Pool (Example)
- Create a directory
sudo mkdir /data/kvm-images sudo chown libvirt-qemu:kvm /data/kvm-images - Add a new pool using virt-manager or virsh
Using virsh:
virsh pool-define-as --name mypool --type dir --target /data/kvm-images
virsh pool-autostart mypool
virsh pool-start mypoolExpanding Virtual Disks and Using Snapshots
- Expanding Virtual Disks
To increase disk capacity, use theqemu-imgcommand (supported for both qcow2 and raw).
sudo qemu-img resize /var/lib/libvirt/images/ubuntu-vm.qcow2 +10GAfter resizing, you must also expand the partition and filesystem inside the guest OS.
- Using Snapshots
With qcow2 disks, you can create snapshots to save the state of a virtual machine at a specific point in time.
virsh snapshot-create-as <virtual-machine-name> <snapshot-name>Snapshots are extremely useful for backups before configuration changes, system updates, or testing rollback scenarios.
Guest OS Installation and Operation
This section explains how to install and efficiently operate operating systems inside KVM virtual machines. It covers typical guest OS installation procedures, ISO image handling, and performance optimization tips.
Installing Common Guest Operating Systems
KVM supports a wide variety of guest operating systems, including Ubuntu, CentOS, and Windows. Below are example installation procedures for Ubuntu and Windows.
- Ubuntu Installation
- Download the latest ISO image from the official Ubuntu website.
- In virt-manager, select “Local install media” when creating a new virtual machine and specify the downloaded ISO file.
- Configure CPU, memory, and disk capacity for the virtual machine.
- Follow the on-screen instructions to complete the standard Ubuntu installation.
- Windows Installation
- Download a Windows evaluation ISO image from the official Microsoft website.
- Create a new virtual machine using virt-manager or virt-install and specify the ISO file.
- For Windows guests, installing virtio drivers significantly improves disk and network performance. Mount the virtio ISO as a virtual CD drive and apply it during setup.
Obtaining and Mounting ISO Images
- Guest OS installation requires ISO images downloaded from official sources.
- By specifying the ISO file path during VM creation, it will be mounted as a virtual CD/DVD.
- Multiple ISO images (OS and drivers) can be mounted simultaneously if needed.
Tips for Guest OS Operation and Performance Optimization
- Optimize Resource Allocation
Allocate CPU and memory appropriately. Over-allocating resources can negatively affect the host OS and other virtual machines. - Install virtio Drivers
Installing virtio drivers in Windows or older Linux guests greatly improves disk and network performance. - Disable Unnecessary Services
Disabling unused services and background processes inside the guest OS helps conserve system resources. - Use KVM Guest Tools
Installing the QEMU Guest Agent enables more accurate VM information retrieval and controlled shutdown operations.
Practical Use Cases and Automation Tips
KVM is not limited to simple virtual machine creation and operation. It is widely used in various business and development scenarios, and its management efficiency can be greatly improved by combining it with scripts and automation tools. This section introduces practical use cases and automation ideas.
Server Use Case Examples
- Separation of Development and Testing Environments
By preparing separate virtual machines for each project, you can freely switch software versions and configurations. This allows you to test new OS versions or applications without affecting production systems. - Building Internal Services
Running file servers, web servers, and database servers in separate virtual machines helps limit the impact of failures. Backups and snapshots can also be managed easily on a per-VM basis.
Automated Provisioning Using CLI and Ansible
- Automated VM Creation with CLI
By scriptingvirt-installandvirshcommands, you can automatically create and manage multiple virtual machines. Example: Shell script for batch VM creation.
for i in {1..5}
do
virt-install --name test-vm-$i --memory 1024 --vcpus 1 \
--disk size=10 --cdrom /path/to/ubuntu.iso \
--os-type linux --os-variant ubuntu20.04 \
--graphics none --network network=default --noautoconsole
done- Infrastructure Automation with Ansible
Using Ansible, you can automate virtual machine creation, initial configuration, and application deployment using playbooks alone. This is especially effective for environments with many servers or where configuration consistency is critical.
Tips for Cloud-Like Operations
- Creating VM Templates
Saving frequently used base virtual machines as templates allows you to deploy new servers almost instantly. - API Integration and Web-Based Management Tools
libvirt provides APIs that can be integrated with custom tools or other management systems. In large-scale environments, web-based management tools such as Cockpit enable visual and centralized control.
Troubleshooting and Common Error Solutions
When operating a KVM environment, you may encounter issues such as virtual machines failing to start or network connectivity problems. This section covers common problems, troubleshooting steps, and how to effectively use logs and support resources.

Common Issues in KVM
- Virtual machine fails to start or cannot be created
- Hardware virtualization (Intel VT or AMD-V) is disabled
- Insufficient allocated memory or disk space
- Incorrect storage pool or ISO image path
- Network connectivity issues
- Incorrect virtual network adapter configuration
- Misconfigured bridge or NAT, or DHCP assignment failure
- Firewall or security restrictions
- Severely degraded performance
- Over-allocation or under-allocation of resources
- Missing virtio drivers (especially for Windows guests)
- Disk I/O bottlenecks
Log Inspection and Basic Troubleshooting Flow
When issues occur, checking logs should be your first step.
- Check system logs
sudo journalctl -xeLook for error messages related to KVM or libvirt.
- Check libvirt logs
Log files under/var/log/libvirt/(such aslibvirtd.log) provide valuable information. - Check VM-specific logs
Use virt-manager or virsh to inspect VM status and review error details in the logs or details view. - Check network status
ip a
brctl show
virsh net-list --allThese commands help verify network connections and bridge configurations.
Basic Troubleshooting Workflow
- Identify when the issue started and what changed
- Review logs and configurations
- Modify settings if necessary and restart services or recreate the virtual machine
Using Official Documentation and Community Resources
The following resources are extremely helpful when troubleshooting.
- Official Documentation
Ubuntu Official KVM Documentation
libvirt Official Documentation - Community Forums and Q&A Sites
- Ubuntu Forums
- Ask Ubuntu
- Stack Overflow
- Search Using Error Messages
Searching for exact error messages in both English and your local language often reveals relevant solutions.
Security and Performance Optimization
To operate a KVM environment safely and efficiently, security measures and performance optimization are essential. This section introduces practical techniques for protecting your virtualization platform and maximizing resource efficiency.
Strengthening Virtualization Security
- Disable Unnecessary Services and Minimize Configuration
Disable unused services in both guest and host systems to reduce the attack surface. - Firewall and Access Control
Configure firewalls (such as ufw or firewalld) appropriately on both host and guest systems. Protect SSH access using non-default ports, key-based authentication, and tools like fail2ban. - Isolation Between Virtual Machines
Separate critical servers using different virtual networks or physical network segmentation to limit damage in case of compromise. - Regular Updates
Keep both host and guest systems up to date with security patches and software updates.
Optimizing Resource Allocation (CPU, Memory, Disk I/O)
- Basic Resource Management
Allocate CPU and memory carefully to each virtual machine while leaving sufficient resources for the host system. - Disk I/O Optimization
Use high-speed SSD storage for critical virtual machines. Be cautious with excessive snapshot usage in qcow2 format, as it may impact performance. - Use virtio Drivers
Installing virtio drivers significantly improves disk and network performance in guest operating systems.
Automating Backups and Snapshots
- Regular Snapshot Creation
Periodic snapshots allow rapid recovery in case of system failure. - Backing Up Disk Images and Configuration Files
Regularly back up virtual disk images (qcow2 or raw) and libvirt XML configuration files to external storage. - Integration with Automation Tools
Use cron or Ansible to automate backup and snapshot tasks.
Conclusion and Learning Resources
This article has covered everything from building a KVM virtualization environment on Ubuntu to daily operations, practical use cases, troubleshooting, security, and performance optimization. Below is a summary and recommended resources for further learning.
Summary
- Overview and Benefits of KVM
KVM is an open-source, high-performance virtualization technology widely used in Ubuntu server environments. - From Installation to Operation
We walked through CPU virtualization checks, package installation, permission setup, VM creation, and basic operations. - Networking, Storage, and Operational Best Practices
You learned how to configure NAT and bridge networking, manage storage pools, expand disks, and use snapshots effectively. - Troubleshooting and Security
Common error solutions, log inspection methods, and best practices for secure and efficient operations were introduced.
Next Steps and Recommended Resources
- Official Documentation
- Ubuntu Official KVM Guide
- libvirt Official Documentation
- Ask Ubuntu
- Qiita KVM Tag
- Stack Overflow (English)
KVM is a powerful virtualization platform suitable for both individual and enterprise use. Use this guide as a foundation to further deepen your understanding and tailor KVM to your specific requirements.
Command Cheat Sheet and Common Configuration Examples
Having frequently used commands and configuration examples readily available can significantly improve daily VM management efficiency. This section provides a concise reference.
Common KVM / virsh / virt-manager Commands
- List virtual machines
virsh list --all- Start a virtual machine
virsh start <virtual-machine-name>- Shut down a virtual machine
virsh shutdown <virtual-machine-name>- Force stop a virtual machine
virsh destroy <virtual-machine-name>- Create a virtual machine (virt-install)
virt-install --name <name> --memory <MB> --vcpus <cores> \
--disk size=<GB> --cdrom <ISO-path> \
--os-type linux --os-variant ubuntu20.04- Remove a virtual machine (definition only)
virsh undefine <virtual-machine-name>Network and Bridge Configuration Examples
- List networks
virsh net-list --allStorage Management and Disk Operations
- List storage pools
virsh pool-list --all- Resize a virtual disk
sudo qemu-img resize /path/to/disk.qcow2 +10G- Create a snapshot
virsh snapshot-create-as <virtual-machine-name> <snapshot-name>Other Useful Tips
- Enable VM autostart
virsh autostart <virtual-machine-name>- Display detailed VM information
virsh dominfo <virtual-machine-name>- Launch virt-manager (GUI)
virt-managerFAQ
Q1: What is the difference between KVM, VirtualBox, and VMware?
A1: KVM is a high-performance virtualization platform integrated into the Linux kernel and is well suited for production servers. VirtualBox is mainly designed for desktop use, while VMware offers rich enterprise features with commercial support. KVM is ideal if you want high performance at low cost.
Q2: How can I back up and restore virtual machines?
A2: You can back up virtual machines by copying disk image files (qcow2 or raw). It is also recommended to back up libvirt XML configuration files and snapshots for easier restoration.
Q3: Can I use USB devices inside a virtual machine?
A3: Yes. You can configure USB passthrough using virt-manager’s hardware settings or via virsh, allowing USB storage devices, external drives, and printers to be used inside virtual machines.
Q4: How can I automatically start virtual machines when the host boots?
A4: Run the virsh autostart <virtual-machine-name> command to enable automatic startup when the host system boots.
Q5: What should I do if a virtual machine is running slowly?
A5: Review CPU and memory allocation, optimize disk I/O (use faster storage or switch from qcow2 to raw if appropriate), and install virtio drivers in the guest OS.
Q6: Network configuration does not work correctly. What should I check?
A6: Verify network status using virsh and brctl commands. Review bridge and Netplan or NetworkManager configuration files and recreate virtual networks if necessary.
Q7: Can I build clusters or high-availability (HA) configurations with KVM?
A7: Yes. By combining KVM with Pacemaker, Corosync, and shared storage (NFS or iSCSI), you can implement HA configurations and live migration. Advanced knowledge is required.


