DHCP Reservations with KVM/QEMU

You can set static IP addresses (DHCP reservations) for your virtual machines in KVM/QEMU using libvirt’s built-in DNS/DHCP server. This is done by modifying the network definition XML file. I’ll use Arch Linux for this example.

!! Make sure to backup any important data or configurations before making changes, as an error can potentially disrupt your network. !!

Get the MAC address of the virtual machine for which you want to set a static IP. You can do this by checking the details of the virtual machine in virt-manager or by running the following command:

virsh dumpxml vmname | grep 'mac address'

Replace vmname with the name of your virtual machine. This will return something like: <mac address='52:54:00:6d:90:02'/>. You will use this MAC address to set the static IP.

Get the name of the network for which you want to set the static IP. If you only have the default network, its name is likely “default”. You can list all your networks with”

virsh net-list --all

Dump the configuration of the network to an XML file:

virsh net-dumpxml default > default.xml

Open default.xml file in a text editor, and add a <dhcp> section, if it doesn’t already exist, inside the <ip> section. Inside the <dhcp> section add a <host> entry for your VM. Here’s an example:

  <ip address='' netmask=''>
      <range start='' end='' />
      <host mac='52:54:00:6d:90:02' name='vmname' ip='' />

Obviously, replace the MAC address, vmname, and IP with the static IP you want to set.

After you’ve made these changes, you need to redefine the network with the modified XML:

virsh net-destroy default
virsh net-define default.xml
virsh net-start default
virsh net-autostart default

This destroys the current network (stopping it), redefines it with your new XML, starts the network, and sets it to autostart with libvirt.

Check if the IP was successfully assigned:

virsh net-dhcp-leases default

This will show all DHCP leases on the “default” network.


Leave a Reply

Your email address will not be published. Required fields are marked *