Linux virtual machine with KVM from the command line

If we want to raise virtual machines in a Linux environment that does not have a graphical environment, we can raise virtual machines using the command line with a XML template.

This article explains how the deployment performed with Ansible-libvirt at KVM, Ansible and how to deploy a test environment works internally

Install Qemu-KVM and Libvirt

Linux virtual machine with KVM from the command line 1

First: we must to install libvirt and Qemu-KVM. In Ubuntu / Debian is installed with:

$ sudo apt-get install -y libvirt-daemon-system python-libvirt python-lxml

And in CentOS / Redhat with:

$ sudo yum install -y libvirt-daemon-kvm python-lxml

To launch the service we must do: $ sudo systemctl enable libvirtd && sudo systemctl start libvirtd

Configure a network template

Libvirt provides us with a powerful tool for managing virtual machines called ‘virsh’, which we must use to be able to manage KVM virtual machines from the command line.

For a virtual machine we mainly need three elements, the first is a network configuration that provides IP to virtual machines via DHCP. To do this libvirt needs XML template like the next template (which we will designate “net.xml”):

  <forward mode='nat'>
      <port start='1' end='65535'/>
  <bridge name='BRIDGE_NAME' stp='on' delay='0'/>
  <ip address='IP_HOST' netmask='NETWORK_MASK'>
      <range start='BEGIN_DHCP_RANGE' end='END_DHCP_RANGE'/>

Whose main elements are:

  • NETWORK_NAME: Descriptive name that we are going to use to designate the network, for example, “test_net” or “production_net”.
  • BRIDGE_NAME: Each network creates an interface on the host server that will serve as gateway of the input/output packets of that previous network to the outside. Here we assign a descriptive name that let as identify the interface.
  • IP_HOST: The IP that such interface will have on the host server and that will be the gateway of the virtual machines.
  • NETWORK_MASK: Depends on the network, usually for testing must be use a class C (
  • BEGIN_DHCP_RANGE: To assign IPs to virtual machines using libvirt, there are an internal DHCP server (dnsmasq based), here we define the first IP of the range that we can serve to virtual machines.
  • END_DHCP_RANGE: And here we define the last IP that virtual machines can obtain.

Preparing the operating system image

The second element is the virtual machine image, this image can be created or downloaded, the second is recommended to reduce the deployment time. An image source for virtual machines with KVM / libvirt is Vagrant (, to obtain an image of the virtual machine that interests us, we must download from where APP_NAME is the name of the application we want to download (e.g. debian), APP_TAG is the distribution of this application (e.g. stretch64) and finally APP_VERSION is the version of the application (e.g. 9.9.0). Even so, there are specific image repositories such as CentOs that are at

Once the file is obtained, it must be decompressed with tar -zcf, which generates three files, one of which is the image of the virtual machine (box.img) that we will rename to NAME_DESCRIPTIVE.qcow2 and copy it to the standard folder for libvirt virtual machine images (/var/lib/libvirt/images) and we will give the libvirt user permissions so that he can manage that image (chown libvirt /var/lib/libvirt/images/NAME_DESCRIPTIVE.qcow2)

It is important to know that if we want to access the virtual machine we will need a password that we can download with wget -O insecure_private_key, and we must apply permissions for access only with the current user ($ chmod 600 insecure_private_key).

Virtual Machine Template

The third element to create the virtual machine once the last two are created is a virtual machine template, which for a recent libvirt release will have the following form:

<domain type='kvm'>
  <memory unit='MB'>VM_MEMORY</memory>
    <bootmenu enable='no'/>
    <boot dev='hd'/>
  <clock offset='utc'/>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='IMAGE_STORAGE_PATH/IMAGE_NAME.qcow2'/>
      <target dev='vda' bus='virtio'/>
    <interface type='network'>
      <source network='NETWORK_NAME'/>
      <model type='virtio'/>
    <console type='pty'>
      <target type='serial' port='0'/>
    <input type='mouse' bus='ps2'>
      <alias name='input0'/>
    <input type='keyboard' bus='ps2'>
      <alias name='input1'/>
    <graphics type='spice' port='5900' autoport='yes' listen=''>
      <listen type='address' address=''/>
      <image compression='off'/>
      <model type='cirrus' vram='16384' heads='1' primary='yes'/>
      <alias name='video0'/>
    <memballoon model='virtio'/>

Whose elements are:

  • VM_NAME: The descriptive name that will allow us to identify the virtual machine
  • VM_MEMORY: How much memory in MB we allocate to the virtual machine
  • CPU_NUMBER: How many virtual CPUs will the virtual machine have
  • IMAGE_STORAGE_PATH: The path where we have saved the virtual machine, the standard is /var/lib/libvirt/images
  • IMAGE_NAME: A descriptive name we have given to the image in the previous stage
  • NETWORK_NAME: A descriptive name have we given to the network that will manage the virtual machines.

Deploying Virtual Machines

Once the templates are created and the image is deployed, we execute:

# sudo virsh net-create plantilla_red.xml
# sudo virsh create plantilla_máquina_virtual.xml

Finally we must check that it works, first we identifyif the virtual machines are running with # sudo virsh list, second we need to get the ip in use with # sudo virsh net-dhcp-leases NETWORK_NAME and finally we access the assigned IP with # ssh -i insecure_private_key vagrant @IP

Scroll to top