3 ways to create a network bridge in RHEL/CentOS 8

A bridge is a data link layer device that interconnects two or more network segments and provides communication between them. It creates a single network interface to build a single aggregate network from multiple networks or network segments. It forwards traffic based on the host's MAC address (stored in the MAC address table).

Linux operating systems such as RHEL (Red Hat Enterprise Linux) and CentOS 8 support the implementation of software-based bridges to simulate hardware bridges. The function of a network bridge is similar to that of a network switch. Its behavior is more or less similar to a virtual network switch.

There are several use cases for network bridging. One practical application is to create a virtual network switch in a virtualized environment, which is used to connect a virtual machine (VM) to the same network as the host.

This guide introduces many ways to set up a network bridge in RHEL/CentOS 8, and use it to set up a virtual network in bridge mode under Oracle VirtualBox and KVM, and connect the virtual machine to the same network as the host.

Use nmcli tool to set up network bridge

nmcli is a widely used, scriptable and powerful command line tool for controlling NetworkManager and reporting network status. It communicates directly with Network Manager and only controls system-wide connections. Importantly, it allows users to use acronyms as long as they are the only prefix in a set of possible options.

First, use the IP command to identify the network interfaces (physical and virtual) currently connected to your computer and the network they are connected to.

# ip add

From the output of the above command, the Ethernet interface is called enp2s0, and we will add this interface from the interface to the bridge.

Next, to list the active network connections on the test system, use the following nmcli command.

# nmcli conn show --active

Important note: If the libvirtd daemon (libvirtd) is installed and started, the default network interface on behalf of the network bridge (virtual network switch) is virbr0. As shown above, it is configured to run in NAT mode.

Next, use the following nmcli command to create a bridge interface, where conn or con represents the connection, the connection name is br0, and the interface name is also br0.

# nmcli conn add type bridge con-name br0 ifname br0

Note: In bridge mode, virtual machines can easily access the physical network. They are on the same subnet as the host and can access services such as DHCP.

To set a static IP address, run the following command to set the IPv4 address, netmask, default gateway, and DNS server for br0 connection (set values according to your environment).

# nmcli conn modify br0 ipv4.addresses '192.168.1.1/24'
# nmcli conn modify br0 ipv4.gateway '192.168.1.1'
# nmcli conn modify br0 ipv4.dns '192.168.1.1'
# nmcli conn modify br0 ipv4.method manual

Now, as shown in the figure, add the Ethernet interface (enp2s0) as a portable device to the bridge (br0) connection.

# nmcli conn add type ethernet slave-type bridge con-name bridge-br0 ifname enp2s0 master br0

Next, open or activate the bridge connection, you can use the connection name or UUID as shown below.

# nmcli conn up br0
OR
# nmcli conn up 2f03943b-6fb5-44b1-b714-a755660bf6eb

Then disable or close the Ethernet or wired connection.

# nmcli conn down Wired\ connection\ 1
OR
# nmcli conn down e1ffb0e0-8ebc-49d0-a690-2117ca5e2f42

Now, when you try to list the active network connections on the system, the bridge connection should appear in the list.

# nmcli conn show  --active

Next, use the following bridge command to display the current bridge port configuration and flags.

# bridge link show

To deactivate the bridge connection and delete it, run the following command. Please note that you must first activate the wired connection.

# nmcli conn up Wired\ connection\ 1
# nmcli conn down br0
# nmcli conn del br0
# nmcli conn del bridge-br0

For more information, see the nmcli man page.

# man nmcli

Create a bridge through the Cockpit web console

Cockpit is a lightweight, interactive, easy-to-use web-based server management interface. In order to interact with the network configuration of the system, cockpit uses NetworkManager and the DBus API it provides.

To add a network bridge, go to the network and click Add network bridge as shown in the figure below.

A pop-up window will appear with the option to add a new bridge. Set the bridge name and select the port as shown in the screenshot below. You can choose to enable STP (Spanning Tree Protocol) and click "Apply".

Under the "Interface" list, the new bridge should now appear and the Ethernet interface should be disabled.

To view it in detail, double-click it. There are some options to delete it, add new port devices to it, etc.

Create a bridge using nm-connection-editor application

nm-connection-editor is a graphical network connection editor for NetworkManager, which is used to add, delete and modify network connections stored by NetworkManager. Any modification can only take effect when NetworkManager is running.

To start it, run the nm-connection-editor command in the command line as the root user, or open it from the system menu.

#nm-connection-editor

After opening, click the plus sign to add a new connection as shown in the screenshot below.

In the pop-up window, select the connection type from the drop-down list, in this case, it is a network bridge, and then click Create.

Next, set the bridge connection and interface name, and then click "Add" to add the bridge port. Select Ethernet as the connection type and click Create.

Next, edit the port device connection details and click "Save".

Now, the bridge port should be added to the list of bridge connections, and then click Save.

In the main interface of the connection editor, you should be able to see the new bridge connection and bridge interface, as shown in the screenshot below.

Now, as shown before, use the nmcli tool to activate the bridge connection and deactivate the wired connection from the command line.

# nmcli conn up br0
# nmcli conn down Wired\ connection\ 1

How to use bridges in virtualization software

In this section, we will show how to connect a virtual machine to the host network using a bridge under Oracle VirtualBox and KVM, as described below.

Use bridge in Oracle VirtualBox

To configure a virtual machine to use a bridged adapter, select it from the VM list, then go to its settings, click the "Network" option and select the adapter (for example, adapter 1), and then make sure that the "Enable Network Adapter" option is selected, Then set it up. Attach it as a bridge adapter, then select the name of the bridge interface (br0), and click OK.

Use bridge in KVM

To use the above network bridge created under KVM, please use the --network=bridge=br0 option while using the virt-install command through the command line interface of the virtual machine.

# virt-install --virt-type=kvm --name Ubuntu18.04--ram 1536--vcpus=4--os-variant=ubuntu18.04--cdrom=/path/to/install.iso --network=bridge=br0,model=virtio --graphics vnc --disk path=/var/lib/libvirt/images/ubuntu18.04.qcow2,size=20,bus=virtio,format=qcow2

You can also use the virsh command-line tool to create other networks and configure them, and you can also edit the VM's XML configuration file to use one of these new bridged networks.

In this guide, we showed how to set up a network bridge in RHEL/CentOS 8, and how to use it under Oracle VirtualBox and KVM to connect the VM to the same network of the host.

——More exciting?——

Recommended Posts

3 ways to create a network bridge in RHEL/CentOS 8
How to create a CentOS virtual machine in VMware
How to create a CentOS virtual machine in VMware
How to create a Python virtual environment in Ubuntu 14.04
Linux-fork() to create a process
How to sort a dictionary in python
How to simulate gravity in a Python game
How to write a confession program in python
How to view detailed network routing table in Ubuntu
How to understand a list of numbers in python
Python implements FTP to upload files in a loop
What are the ways to open files in python