:bulb: How to configure the default network to use a Bridge in a Qemu/KVM environment, so each VM receives its IP directly from the upstream router’s DHCP server. The bridge is a user-created bridge attached to the host’s network.

[01] Install Bridge Package

1
2
3
4
5
6
# Install
sudo apt install bridge-utils -y

# (optional) disable NetworkManager
sudo systemctl disable NetworkManager.service
sudo systemctl stop NetworkManager.service

[02] Network Configuration (netplan)

  • Add a new bridges section
    • parameters.stp: enable Spanning Tree Protocol (loop prevention)
    • parameters.forward-delay: time STP waits before forwarding (after blocking to prevent loops)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# example /etc/netplan/xx.yaml

# This is the network config written by 'subiquity'
network:
  ethernets:
    enp0s31f6:
      dhcp4: true
  bridges:
    userbr0:
      interfaces: [enp0s31f6]
      dhcp4: true
      parameters:
        stp: true
        forward-delay: 4
  version: 2

Verify:

1
2
# Check bridge state
brctl show

[03] Change the VM Default Network

  • Instead of the default virbr0 interface in KVM/QEMU, use the user-created Bridge
    • When forward mode is bridge, DHCP configuration on the bridge itself is not possible
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Create a custom bridge network for the virtual machine
cd /etc/libvirt/qemu/networks
vim user-bridge0.xml

# user-bridge0.xml
<network>
    <name>user-bridge0</name>
    <forward mode="bridge" />
    <bridge name="user-bridge0" />
</network>

# Define user-bridge0 via virsh
virsh net-list --all
virsh net-define user-bridge0.xml
virsh net-start user-bridge0
virsh net-autostart user-bridge0

After setup, userbr0 should appear as active.

[04] Change Network on Existing VMs

  • Virtual network 'xxxxxx': Bridge network and Bridge device ...
    • Functionally identical
    • With the former, if device xxxx doesn’t exist, the VM fails to start
  • Device Model
    • For KVM, virtio and Hypervisor Default are equivalent

[05] Verify Network Connection

  • Confirm IP assignment in the VM
  • Confirm external access from outside the host server to an Nginx running inside the VM
1
sudo apt-get -y install nginx net-tools vim

[06] Note

  • When a Host bridge is created and the upstream router serves DHCP, you cannot see the VM’s IP from the Host system — check it through the router’s admin page.