How to create and configure a bridge network for KVM in Linux

In today’s guide we will look at how to create and configure a bridge network for KVM in Linux-RHEL / CentOS / Ubuntu / Debian / Arch Linuxe.t.c. When using Linux bridges in KVM, virtual machines can access external networks and services outside the virtual environment.

There are multiple ways to configure bridged networks for KVM in Linux. The default network used by virtual machines launched in KVM is NAT networkThrough the NAT network, a virtual network is created for the guest computers and then mapped to the host network to provide Internet connectivity.

When you configure and use a bridged network, the guest operating system accesses an external network that is directly connected to the host. You can use Virtual Machine Manager, Wilsh Command line tool, you can directly edit network scripts or use Linux network management tools.

Method 1: Use the Virtual Machine Manager to create a bridge network.

Follow these steps to create a Linux bridge from Virtual Machine Manager (GUI). You need to install KVM on your system.

How to install KVM on RHEL / CentOS 8, Fedora, Arch Linux, CentOS, Ubuntu / Debian, SLES

Open the virtual machine manager and go to Edit> Connection Details> Virtual Network

Configure new network interface with one click + At the bottom of the window. Give the virtual network a name.

How to create and configure a bridge network for KVM in Linux

Click the “Forward” button and in the next window provide the virtual network information.

How to create and configure a bridge network for KVM in Linux

Click Forward and choose whether to enable IPv6.

How to create and configure a bridge network for KVM in Linux

Select the network type and forwarding strategy.

How to create and configure a bridge network for KVM in Linux

Complete the settings and save the configuration. The new virtual network should appear on the overview page.

How to create and configure a bridge network for KVM in Linux

Bridges on the host system are automatically created for the network.

$ brctl show virbr4      
bridge name	bridge id		STP enabled	interfaces
virbr4		8000.525400c2410a	yes		virbr4-nic

Method 2: Use the virsh command to create a KVM bridge.

Create a new bridge XML file.

vim br10.xml

Add bridge details to the file.


  br10
  
    
      
    
  
  
  
    
      
    
  

To define a network from an XML file without launching it, use:

$ sudo virsh net-define  br10.xmlNetwork br1 defined from br10.xml

To start a (previously defined) inactive network, use:

$ sudo virsh net-start br10Network br10 started

To set the network to start automatically when the service starts:

$ sudo virsh net-autostart br10
Network br10 marked as autostarted

Check to see if the autostart flag is set to yes – Persistence should also be read as “yes”.

$ sudo virsh net-list --all
 Name              State    Autostart   Persistent
----------------------------------------------------
 br10              active   yes         yes
 default           active   yes         yes
 docker-machines   active   yes         yes
 fed290            active   no          yes
 vagrant-libvirt   active   no          yes

Confirm the bridge creation and IP address.

$ ip addr show dev br10
28: br10:  mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:94:00:f5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.30.1/24 brd 192.168.30.255 scope global br10
       valid_lft forever preferred_lft forever

Method 3: Create the bridge by editing the network script (CentOS / RHEL / Fedora):

The following script will create a bridge named br10.

$ sudo vim /etc/sysconfig/network-scripts/ifcfg-br10

With:

DEVICE=br10
STP=no
TYPE=Bridge
BOOTPROTO=none
DEFROUTE=yes
NAME=br10
ONBOOT=yes
DNS1=8.8.8.8
DNS2=192.168.30.1
IPADDR=192.168.30.3
PREFIX=24
GATEWAY=192.168.30.1

The configuration of the eth0 interface I want to bridge to will be:

$ cat /etc/sysconfig/network-scripts/ifcfg-eno1 
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BRIDGE=br10

Restart the network daemon.

sudo systemctl disable NetworkManager && sudo systemctl stop NetworkManagersudo systemctl restart network.service

Method 4: Create a bridge by editing a network script (Debian / Ubuntu):

Configure the bridge interface:

$ sudo vim  /etc/network/interfaces
auto br10 
iface br10 inet static
address 192.168.1.10
network 192.168.1.1
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.1
dns-nameservers 192.168.1.1
bridge_ports eth0
bridge_stp off

Disable all lines in the eth0 interface section as follows:

auto eth0
iface eth0 inet manual

Restart the network service.

 sudo systemctl restart networking.service

Method 5: Use the Nmcli tool

Use nmcli Network management command line tool for creating Linux bridges on the required interface. List all available connections first.

$ sudo nmcli connection show NAME                UUID                                  TYPE      DEVICE enp1s0              498869bb-0d88-4a4c-a83a-c491d1040b0b  ethernet  enp1s0 Wired connection 1  0977f29f-fa2e-3d7f-831c-6f41f8782be3  ethernet  enp7s0 

Since my bridge will be created on the second device enp7s0I will delete the existing connection and create a bridge with this device.

$ sudo nmcli connection delete 0977f29f-fa2e-3d7f-831c-6f41f8782be3Connection 'Wired connection 1' (0977f29f-fa2e-3d7f-831c-6f41f8782be3) successfully deleted.

1. Save bridge-related information to variables.

BR_NAME="br10"BR_INT="enp7s0"SUBNET_IP="192.168.30.10/24"GW="192.168.30.1"DNS1="8.8.8.8"DNS2="8.8.4.4"

where:

  • BR_NAME: The name of the bridge to be created.
  • BR_INT: A physical network device used as a bridge slave.
  • SUBNET_IP: The IP address and subnet assigned to the created bridge.
  • Gross weight: IP address of the default gateway
  • DNS1 with DNS2: The IP address of the DNS server to be used.

2. Define a new bridge connection.

sudo nmcli connection add type bridge autoconnect yes con-name ${BR_NAME} ifname ${BR_NAME}

Output:

Connection 'br0' (be6d4520-0257-49c6-97c2-f515d6554980) successfully added.

3. Modify the bridge to add IP address, gateway and DNS

sudo nmcli connection modify ${BR_NAME} ipv4.addresses ${SUBNET_IP} ipv4.method manualsudo nmcli connection modify ${BR_NAME} ipv4.gateway ${GW}sudo nmcli connection modify ${BR_NAME} ipv4.dns ${DNS1} +ipv4.dns ${DNS2}

4. Add the network device as a bridge slave.

sudo nmcli connection delete ${BR_INT}sudo nmcli connection add type bridge-slave autoconnect yes con-name ${BR_INT} ifname ${BR_INT} master ${BR_NAME}

Sample output.

Connection 'enp7s0' (f033dbc9-a90e-4d4c-83a9-63fd7ec1cdc1) successfully added.

Check the connection.

$ sudo nmcli connection show NAME    UUID                                  TYPE      DEVICE br0     be6d4520-0257-49c6-97c2-f515d6554980  bridge    br0    enp1s0  498869bb-0d88-4a4c-a83a-c491d1040b0b  ethernet  enp1s0 enp7s0  f033dbc9-a90e-4d4c-83a9-63fd7ec1cdc1  ethernet  enp7s0 

Step 2: Set up the bridge

After the bridge connection is created, start it.

$ sudo nmcli connection up br10Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)

View bridge details by running.

sudo nmcli connection show br10

of ip addr The command should provide output similar to the following.

$ ip ad3: enp7s0:  mtu 1500 qdisc fq_codel master br10 state UP group default qlen 1000     link/ether 52:54:00:a2:f6:a8 brd ff:ff:ff:ff:ff:ff 4: br10:  mtu 1500 qdisc noqueue state UP group default qlen 1000     link/ether 52:54:00:a2:f6:a8 brd ff:ff:ff:ff:ff:ff     inet 192.168.122.10/24 brd 192.168.122.255 scope global noprefixroute br10        valid_lft forever preferred_lft forever     inet6 fe80::4f2f:ce6d:dc6b:2101/64 scope link noprefixroute         valid_lft forever preferred_lft forever

Congratulations! You have successfully created and configured a KVM bridge network on your Linux system. Check out KVM related articles below.

How to expand / increase KVM virtual machine (VM) disk size

virsh command cheat sheet to manage KVM guest virtual machines

How to configure a VM on KVM using Terraform

How to create a CentOS / Fedora / RHEL VM template on KVM

Sidebar