In this article, we will be discussing how to set up an isolated network with static IPs and a custom gateway using Libvirt. This is a great way to create a secure and private network for testing or development purposes. We will be going over the steps in detail, so even if you are new to networking and virtualization, you should be able to follow along.
Prerequisites
Before we begin, there are a few things you will need to have installed on your system:
- Libvirt, which is a virtualization toolkit for Linux.
- A virtual machine running a Linux distribution.
- Basic knowledge of Linux and networking.
Setting Up the Network
The first step in setting up an isolated network is to create a new network in Libvirt. This can be done using the virsh command-line tool. To create a new network, use the following command:
virsh net-define isolated.xml
The isolated.xml file should contain the following XML configuration:
<network>
<name>isolated</name>
<forward mode='none'/>
<bridge name='virbr2' stp='on' delay='0'/>
<ip address='192.168.100.1' netmask='255.255.255.0'/>
<ip family='ipv6' address='2001:db8:1234:5678::1'/>
<domain name='localdomain'/>
<dns enable='yes'/>
<dns-search>
<dns-search domain='localdomain'/>
</dns-search>
</network>
This will create a new network named isolated with the IP address 192.168.100.1 and the netmask 255.255.255.0. It will also create a new bridge interface named virbr2 and enable DNS resolution.
Next, we need to start the network. This can be done using the following command:
virsh net-start isolated
This will start the network and bring up the virbr2 interface.
Configuring the Virtual Machine
Now that we have our isolated network set up, we need to configure our virtual machine to use it. This can be done by editing the virtual machine's XML configuration file.
First, we need to add a new network interface to the virtual machine. This can be done using the following XML configuration:
<interface type='bridge'>
<mac address='52:54:00:12:34:56'/>
<source bridge='virbr2'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
This will add a new virtio network interface to the virtual machine that is bridged to the virbr2 interface.
Next, we need to configure the virtual machine's IP address. This can be done by adding the following XML configuration:
<interface type='bridge'>
...
<ip address='192.168.100.2' netmask='255.255.255.0'/>
</interface>
This will set the virtual machine's IP address to 192.168.100.2 with the netmask 255.255.255.0.
Finally, we need to set the virtual machine's gateway. This can be done by adding the following XML configuration:
<interface type='bridge'>
...
<route gateway='192.168.100.1'/>
</interface>
This will set the virtual machine's gateway to the IP address of the isolated network, which is 192.168.100.1.
Testing the Configuration
Now that we have our isolated network and virtual machine configured, it's time to test the configuration. The first thing we should do is ping the virtual machine from the host machine.
ping 192.168.100.2
This should result in a successful ping, indicating that the network is functioning properly.
Next, we should test the virtual machine's connectivity to the internet. This can be done by using the following command:
ping 8.8.8.8
This should also result in a successful ping, indicating that the virtual machine is able to reach the internet through the isolated network.
In this article, we have discussed how to set up an isolated network with static IPs and a custom gateway using Libvirt. This is a great way to create a secure and private network for testing or development purposes. By following the steps outlined in this article, you should be able to create your own isolated network and configure a virtual machine to use it.
References
| Title | Author | URL |
|---|---|---|
| Libvirt Network XML Configuration | Libvirt Project | https://libvirt.org/formatnetwork.html |
| Libvirt Network Configuration Example | Libvirt Project | https://libvirt.org/formatnetwork.html#examples-isolated |
| Libvirt Interface XML Configuration | Libvirt Project | https://libvirt.org/formatdomain.html#elementsInterfaces |