loading...

Ubuntu Server 18.04 – Bridging the virtual machine network

File Management Commands in Linux

Your KVM virtual machines will use their own network, unless you configure bridged networking. This means your virtual machines will get an IP address in their own network, instead of yours. By default, each machine will be a member of the 192.168.122.0/24 network, with an IP address in the range of 192.168.122.2 to 192.168.122.254. If you’re utilizing KVM VMs on your personal laptop or desktop, this behavior might be adequate. You’ll be able to SSH into your virtual machines by their IP address if you’re connecting from the same machine the VMs are running on. If this satisfies your use case, there’s no further configuration you’ll need to do.

Bridged networking allows your VMs to receive an IP address from the DHCP server on your network instead of its internal one, which will allow you to communicate with your VMs from any other machine on your network. This use case is preferable if you’re setting up a central virtual machine server to power infrastructure for your small office or organization. With a bridged network on your VM server, each VM will be treated as any other network device. All you’ll need is a wired network interface, as wireless cards typically don’t work with bridged networking.

That last point is very important. Some network cards don’t support bridging, and if yours doesn’t, you won’t be able to use a bridge with your VM server unless you replace the network card. Before continuing, you may want to ensure your network card supports bridging by reading the documentation. In my experience, most wired cards made by Intel support bridging, and most wireless cards do NOT. Make sure you back up the Netplan configuration file before changing it, so you can revert back to the original version if you find that bridging doesn’t work for you.

To set up bridged networking, we’ll need to create a new interface on our server. Open up the /etc/netplan/01-netcfg.yaml file in your text editor with sudo. We already talked about this file in Chapter 4, Connecting to Networks, so I won’t go into too much detail about it here. Basically, this file includes configuration for each of our network interfaces, and this is where we’ll add our new bridged interface.

Make sure you make a backup of the original Netplan configuration file, and then replace its contents with the following. Be sure to replace enp0s3 (the interface name) with your actual wired interface name if it’s different. There are two occurrences of it in the file. Take your time while configuring this file. If you make a single mistake, you will likely not have network access to the machine once it restarts.

If you’re reading the digital version of this tutorial, it’s highly recommended that you refrain from copying and pasting the following code, but rather type it manually or copy it from the GitHub URL for the tutorials code bundle. Reason being, the YAML format is extremely picky about spaces, and if you end up with a mix of spaces and tabs, the file might not work. When Netplan errors, it can be very hard to figure out exactly what it’s complaining about, but spacing is quite often the culprit even if the error output doesn’t lead you to believe so.
network: 
  version: 2 
  renderer: networkd 
  ethernets: 
    enp0s3: 
      dhcp4: false 
  bridges: 
    br0: 
      interfaces: [enp0s3] 
      dhcp4: true 
      parameters: 
        stp: false 
        forward-delay: 0 

After you make the change, you can apply the new settings immediately, or simply reboot the server. If you have a monitor and keyboard hooked up to the server, the following command is the easiest way to activate the new configuration:

sudo netplan apply 

If you’re connected to the server via SSH, restarting networking will likely result in the server becoming inaccessible because the SSH connection will likely drop as soon as the network stops. This will disrupt the connection and prevent networking from starting back up. If you know how to use screen or tmux, you can run the restart command from within either; otherwise, it may just be simpler for you to reboot the server.

After networking restarts or the server reboots, check whether you can still access network resources, such as pinging websites and accessing other network nodes from it. If you can, you’re all set. If you’re having any trouble, make sure you edited the /etc/netplan/01-netcfg.yaml file properly.

Now, you should see an additional network interface listed when you run ip addr show. The interface will be called br0. The br0 interface should have an IP address from your DHCP server, in place of your enp0s3 interface (or whatever it may be named on your system). From this point onwards, you’ll be able to use br0 for your virtual machine’s networking, instead of the internal network. The internal KVM network will still be available, but you can select br0 to be used instead when you create new virtual machines.

If you have a virtual machine you’ve already created that you’d like to switch to utilize your bridged networking, you can use the following steps to convert it:

  1. First, open virt-manager and double-click on your virtual machine. A new window with a graphical console of your VM will open.
  2. The second button along the top will open the Virtual Hardware Details tab, which will allow you to configure many different settings for the VM, such as the CPU count, RAM amount, boot device order, and more.
  3. Among the options on the left-hand side of the screen, there will be one that reads NIC and shows part of the virtual machine’s network card’s MAC address. If you click on this, you can configure the virtual machine to use your new bridge.
  4. Under the Network source drop-down menu, select the option Specify shared device name. This will allow you to type an interface name in the Bridge name text box; type br0 into that text box. Make sure that the Device model is set to virtio.
  1. Finally, click on Apply. You may have to restart the virtual machine for the changes to take affect:
Configuring a virtual machine to use bridge br0

While creating a brand new virtual machine, there’s an additional step you’ll need to do in order to configure the VM to use bridged networking. On the last step of the process, where you set a name for the VM, you’ll also see Advanced options listed near the bottom of the window. Expand this, and you’ll be able to set your network name. Change the dropdown in this section to Specify shared device name and set the bridge Name to br0. Now, you can click on Finish to finalize the VM as before, and it should use your bridge whenever it starts up:

Selecting a bridge for a newly created VM

From this point onwards, you should have not only a fully-configured KVM server or instance, but also a solution that can be treated as a full citizen of your network. Your VMs will be able to receive an IP address from a DHCP server and communicate with other network nodes directly. If you have a very beefy KVM server, you may even be able to consolidate other network appliances into VMs to save space, which is basically the entire purpose of virtualization.

Comments are closed.

loading...