How to Install and Configure KVM on Ubuntu 18.04

Requirements

  • A fresh Alibaba Cloud instance with Ubuntu 18.04 desktop installed.
  • A static IP address 192.168.0.100 is set up to your Ubuntu 18.04 instance.
  • A root password is set up to your instance.

Launch Alibaba Cloud ECS Instance

Create a new ECS instance, choosing Ubuntu 18.04 as the operating system with at least 4GB RAM, and connect to your instance as the root user.

apt-get update -y

Verify Hardware Support

KVM is a virtualization software, so you will need to verify whether your CPU supports virtualization or not.

egrep -c '(vmx|svm)' /proc/cpuinfo
2
apt-get install cpu-checker
kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used

Install KVM

Next, you will need to install KVM and other required dependencies to your system. You can install all of them with the following command:

apt-get install qemu qemu-kvm libvirt-bin bridge-utils virt-manager virt-viewer  -y
systemctl start libvirtd
systemctl enable libvirtd
service libvirtd status
¡ñ libvirtd.service - Virtualization daemon
Loaded: loaded (/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2019-01-16 06:24:18 UTC; 8min ago
Docs: man:libvirtd(8)
https://libvirt.org
Main PID: 5047 (libvirtd)
Tasks: 19 (limit: 32768)
CGroup: /system.slice/libvirtd.service
©À©€5047 /usr/sbin/libvirtd
©À©€5586 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper
©ž©€5587 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper
Jan 16 06:32:22 ubuntu1804 dnsmasq[5586]: reading /etc/resolv.conf
Jan 16 06:32:22 ubuntu1804 dnsmasq[5586]: using nameserver 127.0.0.53#53
Jan 16 06:32:22 ubuntu1804 dnsmasq[5586]: reading /etc/resolv.conf
Jan 16 06:32:22 ubuntu1804 dnsmasq[5586]: using nameserver 127.0.0.53#53
Jan 16 06:32:22 ubuntu1804 dnsmasq[5586]: reading /etc/resolv.conf
Jan 16 06:32:22 ubuntu1804 dnsmasq[5586]: using nameserver 127.0.0.53#53
Jan 16 06:32:24 ubuntu1804 dnsmasq[5586]: reading /etc/resolv.conf
Jan 16 06:32:24 ubuntu1804 dnsmasq[5586]: using nameserver 127.0.0.53#53
Jan 16 06:32:24 ubuntu1804 dnsmasq[5586]: reading /etc/resolv.conf
Jan 16 06:32:24 ubuntu1804 dnsmasq[5586]: using nameserver 127.0.0.53#53

Configure Bridge Networking

A bridged network is a dedicated network card to a virtual machine that allows guest machines to connect outside the network. You can configure Network bridge by editing /etc/netplan/50-cloud-init.yaml file:

nano /etc/netplan/50-cloud-init.yaml
network:
version: 2
ethernets:
enp0s3:
dhcp4: no
dhcp6: no
bridges:
br0:
interfaces: [enp0s3]
dhcp4: no
addresses: [192.168.0.100/24]
gateway4: 192.168.0.1
nameservers:
addresses: [192.168.0.1]
netplan apply
networkctl status -a
¡ñ 1: lo
Link File: /lib/systemd/network/99-default.link
Network File: n/a
Type: loopback
State: carrier (unmanaged)
Address: 127.0.0.1
::1
¡ñ 2: enp0s3
Link File: /lib/systemd/network/99-default.link
Network File: /run/systemd/network/10-netplan-enp0s3.network
Type: ether
State: carrier (configured)
Path: pci-0000:00:03.0
Driver: e1000
Vendor: Intel Corporation
Model: 82540EM Gigabit Ethernet Controller (PRO/1000 MT Desktop Adapter)
HW Address: 08:00:27:58:d6:9b (PCS Systemtechnik GmbH)
¡ñ 3: virbr0
Link File: /lib/systemd/network/99-default.link
Network File: n/a
Type: ether
State: no-carrier (unmanaged)
Driver: bridge
HW Address: 52:54:00:e0:0b:f0
Address: 192.168.122.1
¡ñ 4: virbr0-nic
Link File: /lib/systemd/network/99-default.link
Network File: n/a
Type: ether
State: off (unmanaged)
Driver: tun
HW Address: 52:54:00:e0:0b:f0
¡ñ 5: br0
Link File: /lib/systemd/network/99-default.link
Network File: /run/systemd/network/10-netplan-br0.network
Type: ether
State: routable (configured)
Driver: bridge
HW Address: 3a:26:a7:35:e0:af
Address: 192.168.0.100
fe80::3826:a7ff:fe35:e0af
Gateway: 192.168.0.1
fe80::225:5eff:fe60:65c (Shanghai Dare Technologies Co.,Ltd.)
DNS: 192.168.0.1

Create Virtual Machine

You can create virtual machine using virt-manager utility.

virt-manager

Manage KVM Virtual Machines Using CLI

KVM provides virt-install CLI tool to create and configure virtual machines, virsh CLI tool to start, stop, reset, delete and clone the running virtual machines.

virsh list --all
Id    Name                           State
----------------------------------------------------
- Alibaba-Ubuntu14.04 shut off
virsh start Alibaba-Ubuntu14.04
Domain Alibaba-Ubuntu14.04 started
virsh list --all
Id    Name                           State
----------------------------------------------------
6 Alibaba-Ubuntu14.04 running
virsh suspend Alibaba-Ubuntu14.04
Domain Alibaba-Ubuntu14.04- suspended
virsh resume Alibaba-Ubuntu14.04
Domain Alibaba-Ubuntu14.04 resumed
virsh shutdown Alibaba-Ubuntu14.04
Domain Alibaba-Ubuntu14.04 is being shutdown
virt-clone --original=Alibaba-Ubuntu14.04 --name=Alibaba-Ubuntu14.04-Clone --file=/var/lib/libvirt/images/Alibaba-Ubuntu14.04-clone.img
Allocating 'Alibaba-Ubuntu14.04-clone.img'             | 8.0 GB     00:02     Clone 'Alibaba-Ubuntu14.04-Clone' created successfully.
virsh list --all
Id    Name                           State
----------------------------------------------------
- Alibaba-Ubuntu14.04 shut off
- Alibaba-Ubuntu14.04-Clone shut off
virsh undefine Alibaba-Ubuntu14.04-Clone
Domain Alibaba-Ubuntu14.04-Clone has been undefined
virt-install --name=Ubuntu-16.10 --vcpus=1 --ram=1024 --cdrom=/Data/ISO/ubuntu-16.10-desktop-amd64.iso --disk path=/var/lib/libvirt/images/ubuntu-16.10.img,size=10 --os-type=linux

Original Source

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com