LFCS - Virtual Networking

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
344
Reaction score
377
Credits
11,920
When you use virtual machines, not VirtualBox, on Linux, you will also have a Virtual Network. Let's look at these a little close before we create Virtual Machines.

I will cover Virtual Machines in the next article, but we should look at the networks we can create or remove.

Virtual Bridge

To have a network, we need a Virtual Bridge (virbr). So, to check if one exists, run 'ip a' in a terminal.

By default, you should have the loopback (lo) and any Network Interface Cards (NIC), usually labeled as 'enp0s#'.

If you selected certain add-ons during the installation, you may also have a Virtual Bridge (virbr0). The OS usually numbers them starting with '0'.

On either of my systems, CentOS and Ubuntu, I do not have a Virtual Bridge. Let's set it up.

To create a Virtual Bridge, we need to install the libraries, or collection of virtualization tools for managing Virtual Networks and Machines, necessary for the Virtual Networking.

On CentOS:

sudo yum install libvirt -y

On Ubuntu:

sudo apt install libvirt-daemon-system -y

Once it installs the packages, you should be able to run 'ip a' to see that the network bridge is active, as shown in Figure 1.

Figure 1.JPG

FIGURE 1

The system starts the daemon and set to auto-start on Ubuntu, but on CentOS we need to set this all up to start.

For CentOS and Ubuntu, it stores the information in '/etc/libvirt/qemu/networks/'. The system stores the network information in 'default.xml'.

NOTE: You cannot simply edit the file to make changes. There is a special command to edit the file, which is covered later.

In the folder is a directory called 'autostart'. Within this folder is a symbolic link to the 'default.xml' file. The link is used to auto-start the network that is set up.

The service name for both Operating Systems (OS) is 'libvirtd'. On CentOS, we need to start and enable the service to auto-start at boot with the commands:

sudo systemctl start libvirtd
sudo systemctl enable libvirtd


Once started, you can run 'ip a' to see that there is a bridge (virbr0) and a network card (virbr0-nic). You may notice that the Bridge has an IP Address (192.168.122.1/24), but the NIC does not. The Bridge operates on Layer 2, or the Data Link Layer, so it sends packets of information based on the Media Access Control (MAC) Address. The bridge receives packets from the network and must have an address to receive the packets and filter them.

On Ubuntu, you can the 'systemctl' command to manage the 'libvirtd' daemon.

NOTE: The Virtual Network is controlled by the 'libvirt' service. If the system does not install it, then there isn't a bridge.

Now that we have the Network Bridge operating, we manage the Virtual Network.

Using 'virsh'

The Virtual Shell (virsh) is a command-line tool used to manage Virtual Networks and Machines.

You can type 'virsh' to enter the shell and use commands within the Virtual Shell, or you can use 'virsh' with the command as a standalone command.

NOTE: To run 'virsh' you need to have elevated privileges as root.

You can use 'sudo' at the beginning of a 'virsh' command or to enter the shell. It may be best if you want to run multiple commands to just enter the shell.

Enter 'virsh', at which point you will be at a shell prompt 'virsh #'. From here, you can type 'help' to get a list of commands. For help on a specific command, use 'help <command>'.

To start, let's look at the network that is for the virtual machines. Use the command 'net-list'. You should get the output similar to Figure 2.

Figure 2.JPG

FIGURE 2

The output shows that there is a network called 'default' that is 'active' and set to 'autostart' and is 'persistent'.

We can turn this network off, remove it, replace it or add to it.

To get more information on a network, use the command 'net-info <network name>'. Here, use the command 'net-info default' and it shows the results in Figure 3.

Figure 3.JPG

FIGURE 3

Here, we can see the name UUID, whether it is 'active', 'persistent' and 'autostart' as well as the name of the virtual bridge.

To turn off the network called 'default', we use the command 'net-destroy <network name>'. So, using the command 'net-destroy default', we cause 'net-list' to see the network is gone. If you perform 'net-info default', you'll see that the network is just not 'active'. To re-activate it, you issue the command 'net-start <network name>'.

If you 'net-destroy' a network, then you can use 'net-list --inactive' to see the networks that still exist, but are not active.

NOTE: Before removing the 'default' network, then make a copy of '/etc/libvirt/qemu/networks/default.xml'. The following command will remove the file.

To remove the network named 'default', we can stop it as we did and also remove the auto-start. To disable the 'autostart' on a network, use the command 'net-autostart --disable default'. If you look in the folder '/etc/libvirt/qemu/networks/autostart/' it should be empty. Issue the command 'net-info default' and you should see that it is no longer 'active' or set for 'autostart'. Now, to remove the 'persistent' option, use the command 'net-undefine default'. If you issue the command 'net-list', then the 'default' network is gone. The 'default.xml' file will be gone.

Performing the command 'ip a' at a regular command prompt, not in a 'virsh' shell, you’ll see that the bridge (virbr) no longer exists. The 'libvirt' daemon is still running, but no networks exist.

If you completely lose the XML file, 'default.xml', the contents are:

<network>
<name>default</name>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:16:4c:80'/>
<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>


Make an XML file and name the file what you want. It doesn't have to be 'default'.

You can change the network name, bridge name, MAC Address. The IP Address should not conflict with an existing device. Neither should the DHCP range conflict with another device or DHCP Server. The MAC Address should also be unique.

In my example, I will use the 'default' and add another XML file named 'bridge.xml'. The contents of the file will be:

<network>
<name>bridge-net</name>
<bridge name='virbr1' stp='on' delay='0'/>
<mac address='52:54:00:16:4c:81'/>
<ip address='192.168.123.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.123.2' end='192.168.123.254'/>
</dhcp>
</ip>
</network>


From the folder '/etc/libvirt/qemu/networks/', issue the command 'virsh net-define default.xml'. The XML file will create an 'inactive' network. To see the inactive network, use the command 'virsh net-list --inactive'. You’ll enable the 'Persistent' flag, but it will not 'autostart' nor be 'active'.

To get the 'default' network running, use the commands:

net-start default
net-autostart default


The network 'default' should go now and the 'virbr0' is running again.

Now, let's add another network. Use the commands with the second XML file called 'bridge.xml'.

virsh net-define /etc/libvirt/qemu/networks/bridge.xml
virsh net-start bridge-net
virsh net-autostart bridge-net


We ran these commands in a terminal shell and not in the 'virsh' shell, since we start each command with ‘virsh’.

If you run 'ip a' then you should now see 'virbr1' as we entered in the 'bridge.xml' file.

To exit the 'virsh' shell, use the command 'quit'.

Conclusion

This article is the start of Virtualization within Linux without using VirtualBox.

Before continuing, make sure you understand the basics of managing a Virtual Network.
 


Latest posts

Top