Score:0

Bridged networking with libvirt and KVM with physical ethernet interface attached to the bridge

uy flag

I followed this guide to set network between virtual machines How to use bridged networking with libvirt and KVM.

There are two problems emerged:

  1. network settings does not persist after system reboot;
  2. on virtual guest machine the second NIC can't connect to the router, no network connection shown

I created new bridge on the host

sudo ip link add hostbr0 type bridge
sudo ip link show type bridge
# 4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
#     link/ether 52:54:00:8a:2e:73 brd ff:ff:ff:ff:ff:ff
# 8: mpqemubr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
#     link/ether 52:54:00:87:65:df brd ff:ff:ff:ff:ff:ff
# 9: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
#     link/ether 02:42:e9:8e:81:fc brd ff:ff:ff:ff:ff:ff
# 10: hostbr0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
#     link/ether 18:31:bf:cc:08:61 brd ff:ff:ff:ff:ff:ff

I have two network interfaces on the host, both connected to the router with Internet access.

name -v
#44~20.04.2-Ubuntu SMP Tue Oct 26 18:07:44 UTC 2021

ls -l /sys/class/net/ | grep pci
# enp4s0 -> ../../devices/pci0000:00/0000:00:1c.5/0000:04:00.0/net/enp4s0
# enp5s0 -> ../../devices/pci0000:00/0000:00:1c.7/0000:05:00.0/net/enp5s0

DHCP server on my router connected to Internet configuration:

IP Adress Pool: 192.168.0.1--192.168.0.254

Default gateway: 192.168.0.1

host enp4s0: 192.168.0.11

hostbr0 enp5s0: 192.168.0.10

I attached enp5s0 physical device to the bridge:

sudo ip link set enp5s0 up
sudo ip link set enp5s0 master hostbr0
sudo ip address add dev hostbr0 192.168.2.2/24
ip addr show hostbr0
#10: hostbr0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN #group default qlen 1000
#    link/ether 18:31:bf:cc:08:61 brd ff:ff:ff:ff:ff:ff
#    inet 192.168.2.2/24 scope global hostbr0
#       valid_lft forever preferred_lft forever

To make configuration persistent I modified /etc/network/interfaces. As you can see I have already several configured interfaces and added new one enp5s0

# /etc/network/interfaces
auto lo
iface lo inet loopback

mapping hotplug
        script grep
        map eth1

iface eth1 inet dhcp

auto eth0
iface eth0 inet static
    address 10.152.187.1
    netmask 255.255.255.0

auto wlan0
  iface wlan0 inet static
    address 192.168.1.1
    netmask 255.255.255.0
    up     /sbin/iwconfig wlan0 mode TTTTTT && /sbin/iwconfig wlan0 enc
restricted && /sbin/iwconfig wlan0 key [Y] XXXXXXXX && /sbin/iwconfig
wlan0 essid SSSSSSSS

auto eth1


# Specify that the physical interface that should be connected to the bridge
# should be configured manually, to avoid conflicts with NetworkManager
iface enp5s0 inet manual
# The hostbr0 bridge settings
auto hostbr0
iface hostbr0 inet static
    bridge_ports enp5s0
        address 192.168.2.11
        broadcast 192.168.2.255
        netmask 255.255.255.0
        gateway 192.168.0.11

It is recommended to disable netfilter:

sudo nano /etc/sysctl.d/99-netfilter-bridge.conf
# net.bridge.bridge-nf-call-ip6tables = 0
# net.bridge.bridge-nf-call-iptables = 0
# net.bridge.bridge-nf-call-arptables = 0

# load the settings written in the file
sudo modprobe br_netfilter

# to load the module automatically at boot
sudo nano /etc/modules-load.d/br_netfilter.conf
# br_netfilter

# load the settings in the 99-netfilter-bridge.conf
sudo sysctl -p /etc/sysctl.d/99-netfilter-bridge.conf

Than I created new virtual network in libvirt

# create a new virtual network
sudo nano /tmp/host-bridged-network.xml
# <network>
#     <name>host-bridged-network</name>
#     <forward mode="bridge" />
#     <bridge name="hostbr0" />
# </network>

sudo virsh net-define /tmp/host-bridged-network.xml
sudo virsh net-start host-bridged-network
sudo virsh net-autostart host-bridged-network
sudo virsh net-list
#  Name                   State    Autostart   Persistent
# ---------------------------------------------
#  default                active   yes         yes
#  host-bridged-network   active   yes         yes

virsh net-edit default
# <network>
#   <name>default</name>
#   <uuid>96dc6685-2006-4d88-9239-20f0b263f14b</uuid>
#   <forward mode='nat'/>
#   <bridge name='virbr0' stp='on' delay='0'/>
#   <mac address='52:54:00:8a:2e:73'/>
#   <ip address='192.168.122.1' netmask='255.255.255.0'>
#     <dhcp>
#       <range start='192.168.122.2' end='192.168.122.254'/>
#     </dhcp>
#   </ip>
# </network>

virsh net-edit host-bridged-network
# <network>
#   <name>host-bridged-network</name>
#   <uuid>762ec061-5a3d-417e-81ae-e06a920e0b13</uuid>
#   <forward mode='bridge'/>
#   <bridge name='hostbr0'/>
# </network>

Some information about my host final configuration:

# on the host
ip route
# default via 192.168.0.1 dev enp4s0 proto dhcp metric 100 
# 10.139.212.0/24 dev mpqemubr0 proto kernel scope link src 10.139.212.1 linkdown 
# 169.254.0.0/16 dev virbr0 scope link metric 1000 linkdown 
# 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown 
# 192.168.0.0/24 dev enp4s0 proto kernel scope link src 192.168.0.11 metric 100 
# 192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown 

ip link
# 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
#     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
# 2: enp4s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
#     link/ether 50:3e:aa:0c:97:02 brd ff:ff:ff:ff:ff:ff
# 3: enp5s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master hostbr0 state UP mode DEFAULT group default qlen 1000
#     link/ether 18:31:bf:cc:08:61 brd ff:ff:ff:ff:ff:ff
# 4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
#     link/ether 52:54:00:8a:2e:73 brd ff:ff:ff:ff:ff:ff
# 5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN mode DEFAULT group default qlen 1000
#     link/ether 52:54:00:8a:2e:73 brd ff:ff:ff:ff:ff:ff
# 8: mpqemubr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
#     link/ether 52:54:00:87:65:df brd ff:ff:ff:ff:ff:ff
# 9: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
#     link/ether 02:42:e9:8e:81:fc brd ff:ff:ff:ff:ff:ff
# 10: hostbr0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
#     link/ether 18:31:bf:cc:08:61 brd ff:ff:ff:ff:ff:ff

Than I added to the ubuntu 20.04 guest virtual machine two network interfaces: default and host-bridged-network. Guest starts as usual, I can see Network environment in the Nemo file manager (guest node only). The enp5s0 interface can't connect, Ubuntu tries to establish connection, but always fails. System message: "Activation of network connection failed" for enp5s0 interface. Default libvirt network is shown as enp9s0 and works correctly.

On the host this interface is shown as unmanaged.

My questions are:

Which address ranges should I assign to the virtual networks?

The hostbr0 is not created after system reboot, so there is some problem in configuration?

I also have DHCP server initialized on the host, it is currently not used. Maybe it is better to delete it?

Should I add both networks to the guest machine: default, host-bridged-network or only host-bridged-network?

The bridged physical network interface must be plugged in to the router? Or it serves all the functions on the host and the second NIC provides Internet via NAT networking?

Please, suggest improvements on address assignment, I suspect there is something wrong in my configuration

Doug Smythies avatar
gn flag
which version of Ubuntu is your host computer? I ask because you are still using `/etc/network/interfaces`. Creating a virtual network does not make sense to me, just put everything on your LAN via the host bridge.
Alexander avatar
uy flag
My ubuntu has the following version 44~20.04.2-Ubuntu SMP Tue Oct 26 18:07:44 UTC 2021
Doug Smythies avatar
gn flag
I mean Ubuntu distro version, i.e. `lsb_release -a`. Anyway, if it is 20.04.3, then I do not understand why you are using `/etc/network/interfaces` file as it is not used anymore.
Alexander avatar
uy flag
No LSB modules are available, Distributor ID: Ubuntu, Description: Ubuntu, 20.04.3 LTS, Release: 20.04, Codename: focal. I use it to make bridge persistent between reboots according to mentioned instruction. How to create host bridge based on physical NIC?
Doug Smythies avatar
gn flag
does [this](https://netplan.io/examples/#configuring-network-bridges) help? I can try to write an answer, but not just now. Your reference has some mistakes, in my opinion.
Doug Smythies avatar
gn flag
[Here](https://ubuntuforums.org/showthread.php?t=2461631&p=14036896#post14036896) is how I did it on my 20.04.3 Ubuntu server.
Alexander avatar
uy flag
I managed to set simple bridge looking at your references. Once I do the whole configuration I will place it here as the response to my own answer (it will be soon)
Score:0
uy flag

This is my final configuration, I set DHCP server on the router which assigns ip addresses to all virtual machines as well to the host. Samba share works fine with this configuration.

I prefer to set permanent IP addresses to the host and virtual machines via DHCP server on the router which is connected to the Internet.

  # /etc/netplan/01-network-manager-all.yaml
  network:
      version: 2
      renderer: networkd
      ethernets:
          eth0:
            match:
              macaddress: mac1
            set-name: tplink-usb3.0
            dhcp4: true
            dhcp6: false
            optional: true
            mtu: 1500
            nameservers:
              addresses: [8.8.8.8]
          eth-host:
            match:
              macaddress: mac2
            dhcp4: false
            dhcp6: false
      bridges:
          br0:
            interfaces: [eth-host]
            addresses: [192.168.0.9/24]
            gateway4: 192.168.0.1
            mtu: 1500
            nameservers:
              addresses: [8.8.8.8]
            parameters:
              stp: true
              forward-delay: 4
            dhcp4: true
            dhcp6: false
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.