How to install and configure KVM and Open vSwitch on Ubuntu or Debian

In today's multi-tenant data centers, the concept of virtualization is being extended from hypervisor-driven server virtualization to network virtualization. In this environment, software-based virtual switches are being installed on servers along with hypervisor, bridging traffic among different virtual machines (VMs).

In this tutorial, I am going to demonstrate how to install and configure KVM and Open vSwitch (OVS) on Ubuntu or Debian. KVM and Open vSwitch are, respectively, the most popular open-source hypervisor and virtual switch used in today's data centers.

Install Open vSwitch on Ubuntu or Debian

While OVS comes as a package on Ubuntu or Debian, here I am going to build it from the source, which will have the latest features and bug fixes.

First, install dependencies for building OVS.

$ sudo apt-get install build-essential libssl-dev linux-headers-$(uname -r)

Build OVS from the source as follows. The steps below will build OVS user-space tools as well as its kernel module.

$ wget
$ tar xvfvz openvswitch-1.9.3.tar.gz
$ cd openvswitch-1.9.3
$ ./configure --with-linux=/lib/modules/`uname -r`/build
$ make

Go ahead and install OVS user-space components under /usr/local/share/

$ sudo make install

The next step is to test the OVS kernel module (before installing it). For that, load the kernel module in the kernel first.

$ sudo insmod ./datapath/linux/openvswitch.ko

Verify that the OVS kernel module is loaded successfully.

$ lsmod | grep openvswitch
openvswitch            97934  0

Once you verify that openvswitch.ko is successfully loaded in the kernel, go ahead and install the kernel module as follows.

$ sudo make modules_install

Configure and Start Open vSwitch

Create a skeleton OVS configuration database.

$ sudo mkdir /etc/openvswitch
$ sudo ovsdb-tool create /etc/openvswitch/conf.db ./vswitchd/vswitch.ovsschema

Start OVS database server.

$ sudo ovsdb-server --remote=punix:/usr/local/var/run/openvswitch/db.sock - remote=db:Open_vSwitch,manager_options --pidfile --detach

Initialize OVS configuration database.

$ sudo ovs-vsctl --no-wait init

Finally, start OVS daemon.

$ sudo ovs-vswitchd --pidfile --detach

Install KVM on Ubuntu or Debian

Install KVM and necessary user-space tools with apt-get command.

$ sudo apt-get install qemu-kvm libvirt-bin bridge-utils virt-manager

Add a non-root regular user (e.g., alice) to libvirtd group, so that the user can launch VMs without root privilege.

$ sudo adduser alice libvirtd

Log out and log back in as the user to make the group membership change effective.

Run the following command. If you should see an empty list of VMs, that means KVM is set up successfully.

$ virsh -c qemu:///system list
 Id    Name                           State

Configure Open vSwitch for KVM

Now it is time to create OVS bridge startup scripts, so that OVS can be automatically configured when a VM is started or stopped.

Install dependencies (user-mode Linux utilities), which will be used to handle Linux bridging modes.

$ sudo apt-get install uml-utilities

Create bridge startup scripts as follows.

$ sudo vi /etc/openvswitch/ovs-ifup

/sbin/ifconfig $1 up
ovs-vsctl add-port ${switch} $1
$ sudo vi /etc/openvswitch/ovs-ifdown

/sbin/ifconfig $1 down
ovs-vsctl del-port ${switch} $1
$ sudo chmod +x /etc/openvswitch/ovs-if*

Then, create a default bridge br0, and add a physical network interface via which VMs will communicate with external networks. In this tutorial, I assume such a network interface is eth5.

$ sudo ovs-vsctl add-br br0
$ sudo ovs-vsctl add-port br0 eth5

Launch a VM with KVM

Now you are ready to launch a guest VM.

I assume that you already prepared a guest VM image (e.g., ubuntu-client.img). Use the following command to launch a guest VM.

$ sudo kvm -m 1024 -net nic,macaddr=11:11:11:EE:EE:EE -net tap,script=/etc/openvswitch/ovs-ifup,downscript=/etc/openvswitch/ovs-ifdown -vnc :1 -drive file=/home/dev/images/ubuntu-client.img,boot=on

This will create and launch a guest VM whose virtual interface is automatically added to OVS bridge br0 upon launch.

You can verify the OVS status by using ovs-vsctl command as follows.

This is the remote desktop session for the launched VM.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

6 thoughts on “How to install and configure KVM and Open vSwitch on Ubuntu or Debian

  1. Great article! Some notes... newer versions of OpenVSwitch do not require the kernel module, but then again these will require the newer kernels. Also, you can name your bridge whatever you'd like. Br0 is used for historical reasons.

  2. When i give the last command "kvm -m ...", the cursor just keeps on blinking in the terminal and nothing happens. Even the '#' prompt doesn't appear until I press Ctrl+C. Is this an error? How to rectify it?

  3. If you are having trouble running this command:
    sudo ovsdb-server --remote=punix:/usr/local/var/run/openvswitch/db.sock --remote=db:Open_vSwitch,manager_options --pidfile --detach

    and you get the following error:
    "ovsdb-server: "db:Open_vSwitch,manager_options": Invalid Syntax",
    note that for openvswitch-2.3.1 version, you need to write instead the following:
    sudo ovsdb-server --remote=punix:/usr/local/var/run/openvswitch/db.sock --remote=db:Open_vSwitch,Open_vSwitch,manager_options --pidfile --detach

    Hope it helps

  4. Great article. Thanks
    OVS build was not good for my 3.2.0-4-amd64. I managed to download the latest ovs, e.g. 2.3.1, then do "sudo modprobe libcrc32c" and "sudo modprobe gre" to get ride of the issue of " could not insert module ./datapath/linux/openvswitch.ko: Unknown symbol in module".

    • Just do modinfo openvswitch.ko and you'll find the modules on which openvswitch.ko is dependent. For example:
      root@sha09-virtual-machine:~# modinfo /home/sha-09/openvswitch-2.3.2/datapath/linux/openvswitch.ko
      filename: /home/sha-09/openvswitch-2.3.2/datapath/linux/openvswitch.ko
      version: 2.3.2
      license: GPL
      description: Open vSwitch switching datapath
      srcversion: B1D969EA9250888C91A6F31
      depends: libcrc32c,vxlan,gre
      vermagic: 3.13.0-24-generic SMP mod_unload modversions

      So, try to add the modules which are mentioned in the "depends" field. For eg: modprobe gre

Leave a comment

Your email address will not be published. Required fields are marked *