Configure a Libvirt network to automatically assign IP addresses by defining a DHCP range within the virtual network XML configuration. This setup allows your single virtual machine to receive a valid address and route packets between the host and the guest without manual intervention or static IP assignment errors.
Understanding the Default Network Configuration
When you install Libvirt, it typically creates a default network named
default. This network is configured in NAT mode, which means it provides internet access to guests while isolating them from the external physical network. The primary goal here is to ensure that this network correctly hands out IP addresses via DHCP so your VM can communicate effectively with the host system. Often, users encounter issues because the DHCP range is too small or misconfigured, leading to conflicts or failures in address assignment.
To inspect the current state of your networks, you should list all defined networks on your host. Use the command
virsh net-list --all to see both active and inactive networks. If the
default network is not running, you must start it using
virsh net-start default. However, starting the network alone does not guarantee proper IP assignment; you must verify the underlying configuration file to ensure the DHCP parameters are correct.
Editing the Network XML Definition
The core of the solution lies in editing the XML definition of the Libvirt network. You can open the configuration for the default network by running
virsh net-edit default. This command opens the XML file in your default text editor. Look for the
<ip> tag within the
<network> block. This section defines the subnet mask and the gateway address for the virtual network.
Inside the
<ip> tag, you will find a
<dhcp> section. This is where the automatic IP assignment happens. The
<dhcp> tag contains one or more
<range> tags. Each range specifies the start and end IP addresses that the DHCP server will hand out. For a single VM, you do not need a large pool, but you must ensure the range is valid and does not overlap with other reserved addresses.
A typical correct configuration might look like this:
<ip address="192.168.122.1" netmask="255.255.255.0">
<dhcp>
<range start="192.168.122.100" end="192.168.122.254"/>
</dhcp>
</ip>
In this example, the host acts as the gateway at .1, and the DHCP server assigns addresses from .100 to .254. This provides ample space for future expansion while ensuring the single VM gets an address reliably. If your existing range is too narrow or invalid, update these values accordingly. Remember to save the file after making changes.
Restarting the Network Service
After modifying the XML configuration, you must restart the network for the changes to take effect. Simply reloading the configuration is not enough; the DHCP daemon needs to be restarted to pick up the new ranges. Run the following commands in sequence:
- Destroy the network: Use
virsh net-destroy default. This stops the network immediately.
- Define the updated network: Use
virsh net-define /etc/libvirt/qemu/networks/default.xml. This ensures the running instance uses the latest XML definition.
- Start the network: Use
virsh net-start default. This brings the network back online with the new DHCP settings.
It is crucial to follow this order. If you only redefine the network without destroying and restarting, the old DHCP leases might persist, causing confusion. Once the network is active, you can verify its status with
virsh net-info default.
Configuring the Virtual Machine
Now that the network infrastructure is ready, you need to ensure your VM is attached to this specific network. When creating or modifying a VM, specify the network interface to use the
default network. In the VM's XML definition, the interface section should reference the default network name. If you are using a GUI tool like Virt-Manager, simply select "Virtual Network: default (NAT)" in the network device settings.
Once the VM starts, it should automatically request an IP address via DHCP. Inside the VM, check the assigned address using standard tools like
ip addr show or
ifconfig. You should see an address within the range you defined in the Libvirt configuration. To test connectivity, try pinging the host gateway address (usually .1) from inside the VM. Successful ping responses confirm that routing is working correctly between the host and the guest.
If you face issues where the VM cannot obtain an IP, check the Libvirt logs for errors. Common problems include syntax errors in the XML file or conflicting DHCP services on the host. Ensure no other service is listening on the same port or interfering with the Libvirt DHCP process. For more complex networking scenarios, such as isolated networks, refer to our guide on
Setting Up an Isolated Network with Static IPs and Custom Gateway using Libvirt.
Troubleshooting Connectivity Issues
Even with correct DHCP configuration, connectivity issues may arise due to firewall rules or DNS misconfigurations. Ensure that your host firewall allows traffic on the virtual network interface. Typically, Libvirt manages its own iptables rules, but custom firewall configurations might block this traffic. You can check the current rules with
iptables -L -n to see if the
libvirt chains are present and active.
Another common issue is DNS resolution. The Libvirt DHCP server usually forwards DNS queries to the host's resolvers. If the VM cannot resolve domain names, check the
/etc/resolv.conf inside the VM. It should point to the Libvirt gateway address. If it points elsewhere, you may need to adjust the DHCP options in the network XML to specify custom DNS servers.
For cases where multiple bridges exist with similar subnets, ensure there are no routing loops or overlapping CIDR blocks. This can cause unpredictable packet loss. See our article on
Same IP Range and Subnet in 2 Different Bridges for details on avoiding these conflicts.
Frequently Asked Questions
How do I change the DHCP range in Libvirt?
You can change the range by editing the network XML file using
virsh net-edit. Modify the
start and
end attributes within the
<range> tag under the
<dhcp> section. After saving, you must destroy and restart the network for changes to apply.
Why can't my VM ping the host after IP assignment?
This is often caused by host firewall rules blocking ICMP traffic. Check your iptables or firewalld configuration to ensure the virtual network interface is trusted. Additionally, verify that the VM's default gateway is correctly set to the Libvirt bridge IP address.
Can I assign a static IP to a VM in a DHCP network?
Yes, you can reserve a specific IP for a MAC address in the Libvirt DHCP configuration. Add a
<host mac="..." name="..." ip="..."/> entry within the
<dhcp> block. This ensures the VM always receives the same address while remaining part of the dynamic pool.
What happens if the DHCP range is exhausted?
If all addresses in the defined range are leased, new VMs will fail to get an IP address. They will either time out waiting for a lease or receive a link-local address. Expand the range in the XML configuration to accommodate more devices if you plan to run multiple VMs simultaneously.
How do I verify the current DHCP leases?
You can view active leases by checking the Libvirt DHCP lease file, typically located at
/var/lib/libvirt/dnsmasq/default.leases. This file shows the MAC address, IP address, expiration time, and hostname for each connected client.