Install Libvirt/KVM on Ubuntu 18.04 LTS and setup a bridge (br0) using NetPlan

Install Libvirt/KVM/Qemu/Virt-Manager

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

Add your user to the libvirt and libvirt-qemu groups so you can connect to libvirt using virt-manager without being root

adduser devon libvirt
adduser devon libvirt-qemu

Configure the ethernet adapter and setup a bridge using netplan

  • The Ethernet adapter will be joined to a bridge device named br0
  • When setting up a bridge, the bridge (br0) gets the IP address.
  • All of our virtual machines will use the bridge (br0) as their network interface.
##
## Remove all existing netplans
##

mkdir /etc/netplan.old
mv /etc/netplan/* /etc/netplan.old/

##
## Create a new netplan with a static IP and bridge
##

cat << EOF > /etc/netplan/00-network.yaml
network:
  version:  2
  renderer: networkd

  ethernets:
    ens33:
      match:
        macaddress: 18:a9:05:25:47:48
      set-name: ens33

  bridges:
    br0:
      dhcp4: no
      dhcp6: no
      addresses:
        - 10.10.30.30/16
      gateway4: 10.10.10.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4
      interfaces:
        - ens33

EOF

##
## Apply the netplan configuration
##
## WARNING: If the IP address changes and you are connected via ssh
##          you will lose access to the host and need to reconnect
##          using the new IP address.
##
netplan apply

OPTIONAL: Remove the NAT network

  • Libvirt comes with a default network (192.168.122.0/24) which provides a NAT gateway.
  • However, it may be decided that the NAT network is unwanted overhead and that all of the VM’s should have direct network access using the bridge (br0), in which case the default NAT network is unwanted.
virsh net-list
mkdir -p /var/lib/libvirt
virsh net-dumpxml default > /var/lib/libvirt/net-default-backup.xml
virsh net-destroy default
virsh net-undefine default
service libvirtd restart

iptables -nL
iptables -nL -t nat

Restore the default NAT network

  • If you decide that you made a huge mistake by removing that default NAT network, it can easily be restored from the previously created backup.
virsh net-create /var/lib/libvirt/net-default-backup.xml

OPTIONAL: Create a host-only network

  • Many virtualization systems create host-only networks.
  • If you are looking to setup a trust-zone, you will need a host-only network.

cat << EOF > hostonly.xml
<network>
  <name>hostonly</name>
  <bridge name='virbr99' stp='off' delay='0'/>
  <mac address='52:54:00:ef:f7:e5'/>
  <ip address='192.168.123.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.123.2' end='192.168.123.254'/>
    </dhcp>
  </ip>
</network>
EOF

virsh net-create hostonly.xml

ifconfig virbr99
categories: ubuntu | ubuntu1804lts | netplan | kvm | linux | virsh | qemu | libvirt |