Linux KVM. KVM is a virtualization technology for Linux that allows you
to create and run virtual machines (VMs) on a Linux host.
Before we begin, make sure that you have a Linux machine with KVM
installed. You can install KVM on Ubuntu or Debian with the following
command:
sudo apt-get install qemu-kvm libvirt-bin bridge-utils virt-manager
For Red Hat-based systems, you can use the following command:
sudo yum install qemu-kvm libvirt bridge-utils virt-manager
Once you have KVM installed, you can start creating VMs.
Creating a Virtual Machine
You can create a VM using the virt-install
command. Here is an example command:
sudo virt-install \
--name my-vm \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/my-vm.qcow2,size=10 \
--os-type linux \
--os-variant ubuntu20.04 \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://archive.ubuntu.com/ubuntu/dists/focal/main/installer-amd64/'
This command creates a VM named my-vm
with 2 vCPUs and 2GB of RAM. The VM has a 10GB disk and uses the Ubuntu 20.04 operating system. The VM is connected to a virtual bridge named virbr0
and has no graphics output. The console output is redirected to a serial port. Finally, the VM is installed from an Ubuntu 20.04 installation ISO available at the given URL.
You can adjust the parameters of this command to suit your needs. For example, you can change the name of the VM, the amount of RAM and vCPUs, the disk size and location, and the network settings.
Starting and Stopping a Virtual Machine
To start a VM, use the virsh start
command followed by the name of the VM:
To stop a VM, use the virsh shutdown
command followed by the name of the VM:
sudo virsh shutdown my-vm
Listing Virtual Machines
To list all the VMs on your system, use the virsh list
command:
This command lists all the running and stopped VMs on your system.
Managing Virtual Machines with Virt-Manager
Virt-Manager is a graphical user interface for managing virtual
machines. To start Virt-Manager, type the following command in a
terminal:
This command opens the Virt-Manager window, where you can view and manage your VMs.
To create a new VM using Virt-Manager, click the Create a new virtual machine
button in the toolbar. This opens a wizard that guides you through the process of creating a new VM.
Conclusion
In this tutorial, we have covered the basics of creating, starting, and stopping virtual machines using KVM on Linux. We have also covered some of the CLI commands and configuration examples for managing virtual machines. With this knowledge, you can start using KVM to create and manage virtual machines on your Linux host.