Creating and Managing Virtual Machines with KVM on Ubuntu

May 30, 2023 - 10:08
Sep 14, 2024 - 23:01
 0  205
Creating and Managing Virtual Machines with KVM on Ubuntu

Virtualization has become an essential technology in today's IT landscape, enabling more efficient use of hardware resources and providing flexibility in managing and deploying applications. One powerful tool for virtualization on Linux systems is KVM (Kernel-based Virtual Machine). This blog post will guide you through setting up and using KVM on Ubuntu for creating and managing virtual machines.

What is KVM?

KVM is an open-source virtualization technology built into the Linux kernel. It allows you to turn your Linux machine into a hypervisor, capable of running multiple virtual machines (VMs) simultaneously. Each VM has its own virtualized hardware, including CPU, memory, disk, and network interfaces.

Prerequisites

Before setting up KVM, ensure your system meets the following requirements:

1. Hardware Support: Your CPU must support hardware virtualization (Intel VT-x or AMD-V). You can check this by running:

   egrep -c '(vmx|svm)' /proc/cpuinfo


   A non-zero output indicates that your CPU supports hardware virtualization.

2. Operating System: This guide uses Ubuntu 20.04, but it should work with other versions as well.

Step 1: Install KVM and Related Packages

First, update your package list and install the necessary packages for KVM and virtualization management:

sudo apt update
sudo apt install -y qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager

Step 2: Verify Installation

Check if the KVM kernel modules are loaded:

lsmod | grep kvm

You should see `kvm_intel` (or `kvm_amd` for AMD processors) and `kvm` listed.

Ensure the `libvirtd` service is running:

sudo systemctl status libvirtd

Step 3: Add Your User to the `libvirt` and `kvm` Groups

Add your user to the `libvirt` and `kvm` groups to manage virtualization without root privileges:

sudo usermod -aG libvirt,kvm $USER

Log out and log back in for the group changes to take effect.

Step 4: Create a Virtual Network

KVM uses virtual networks to manage VM networking. You can use the default network or create a custom one using `virt-manager`.

1. Open `virt-manager`:

sudo virt-manager

2. Go to Edit > Connection Details.

3. Under the "Virtual Networks" tab, click the "+" button to create a new network.

4. Follow the wizard to configure your virtual network settings.

Step 5: Create a Virtual Machine

You can create and manage VMs using `virt-manager` or the command line.

Using `virt-manager`:

1. Open `virt-manager`:

 sudo virt-manager

2. Click the "Create a new virtual machine" button.

3. Follow the wizard to configure your VM settings, including choosing an installation method (e.g., ISO file or network install), setting up storage, and configuring CPU and memory.

Using the command line:

You can also create VMs using `virt-install`. Here's an example command:

sudo virt-install \
  --name ubuntu-vm \
  --ram 2048 \
  --vcpus 2 \
  --disk path=/var/lib/libvirt/images/ubuntu-vm.qcow2,size=20 \
  --os-type linux \
  --os-variant ubuntu20.04 \
  --network bridge=virbr0 \
  --graphics none \
  --console pty,target_type=serial \
  --cdrom /path/to/ubuntu-20.04.iso

Step 6: Managing Virtual Machines

You can manage your VMs using `virt-manager` or the command line tools `virsh` and `virt-clone`.

Using `virt-manager`:

Start/Stop VMs: Right-click on a VM and select Start or Shut Down.
Edit VM Configuration: Right-click on a VM and select Open to change settings.

Using the command line:

List VMs:

  virsh list --all

Start a VM:

  virsh start vm-name


  

Shutdown a VM:

 virsh shutdown vm-name

Delete a VM:

  virsh undefine vm-name
  rm -f /var/lib/libvirt/images/vm-name.qcow2

Conclusion

Setting up and managing virtual machines with KVM on Ubuntu provides a powerful and flexible virtualization solution. Whether you prefer a graphical interface with `virt-manager` or the command line with `virsh` and `virt-install`, KVM offers the tools you need to efficiently create and manage VMs. With KVM, you can leverage your hardware resources to their fullest potential, enabling a wide range of use cases from development and testing to production deployments.

Feel free to share your experiences or ask questions in the comments below!

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow