Creating Virtual Machines on KVM with virt-install
virt-install is a tool that lets you create virtual machines using the command line. In this tutorial, we’ll learn how to provision a Ubuntu VM on KVM.
Before we begin, you’ll need to have access to a Linux machine that supports KVM. This tutorial shows you how to check if your hardware is compatible and how to install KVM on Ubuntu.
Install virt‑install on Ubuntu
Run the following command to install virt-install.
sudo apt install virtinst -y
Creating a Ubuntu 18.04 Virtual Machine with virt-install
The following command will create a new virtual machine called falcon-1 with 1 GB of RAM and an 8 GB hard disk. The type of VM is Ubuntu 18.04 as you can see from the os-variant option and bionic in the URL of the location option.
virt-install \ --name falcon-1 \ --ram 1024 \ --disk path=/var/lib/libvirt/images/falcon1.img,size=8 \ --vcpus 1 \ --virt-type kvm \ --os-type linux \ --os-variant ubuntu18.04 \ --graphics none \ --location 'http://archive.ubuntu.com/ubuntu/dists/bionic/main/installer-amd64/' \ --extra-args "console=tty0 console=ttyS0,115200n8"
After issuing the above command, the VM will be created and the installation media for Ubuntu will be downloaded. Since we specified no graphics and console in the extra-args we will be presented with a text based UI of the installation wizard as you can see in the image below.
Walk through the installation as you would on any other hardware, giving it a name, user, password, and installing programs etc. I give mine the name, falcon-1 and the username cap with a password of qwerty as you can see in the images below.
Make sure you enable Open-SSH server at the end of the installer when selecting software so that we can connect to the machine.
After the installation is complete, the machine will reboot and you will be stuck in the console. Press Ctrl+] to disconnect from the console.
Using virsh to find out the IP address of a Virtual Machine
Before we can connect to the new virtual machine named falcon‑1, we need to determine its IP address. We can do that using virsh by running the following command.
virsh domifaddr falcon-1
$ virsh domifaddr falcon-1 Name MAC address Protocol Address ------------------------------------------------------------------------------- vnet0 52:54:00:d9:1c:b5 ipv4 192.168.122.169/24
As you can see the IP address is 192.168.122.169. Let’s end this tutorial by connecting to the virtual machine.
We now have a Ubuntu 18.04 virtual machine running on KVM and we’ve been able to test it works by connecting via SSH.
You’ll notice when we created the VM with virt-install, we did not specify any networking options. This means we can only communicate with the VM from the host its running on because its default option is to share a private network with the host.
In order to communicate with the VM from the LAN, we need to configure it to use bridged networking, which we’ll explore in the next post.