Configuring dhcp static ip assignment to kvm host vms
Libvirtd by default uses dnsmasq to provide ip address for kvm host vms automatically when libvirtd daemon is started.
virtual network switch can be provided ip address from dhcp, it uses dnsmasq server.
use the below bash one liner to print all the kvm mac address and respective host name
#virsh list --all | awk '{print $2}' | grep -v Name | xargs -I {} virsh dumpxml {} | grep -i '<mac address' > /tmp/mac && virsh list --all | awk '{print $2}' | grep -v " "| grep -v Name | xargs -n1 > /tmp/hosts && pr -m -t /tmp/mac /tmp/hosts
52:54:00:d8:a0:f4 centosext
52:54:00:2d:07:d3 centosext1
52:54:00:17:1a:99 centosext2
52:54:00:7b:f9:f3 centosext3
52:54:00:df:5f:2f centosext4
52:54:00:d8:a0:f4 centosext5
52:54:00:82:18:49 chefnode1
52:54:00:8d:10:87 chefnode2
52:54:00:c6:41:eb chefnode3
52:54:00:7a:73:a0 chefnode4
52:54:00:e1:1b:06 cipher_default
finding the kvm bridge name
#virsh net-list
Name State Autostart Persistent
----------------------------------------------------------
default active yes yes
vagrant-libvirt active no yes
lets edit the default network used by libvirt.
#virsh net-edit default
<network>
<name>default</name>
<uuid>cdaebe20-c72d-4434-932e-58e00de5e837</uuid>
<forward mode='nat'/>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:d9:5b:b9'/>
<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>
The location of the xml configuration file will be located at /etc/libvirt/qemu/networks/default.xml
To reserve ip address on mac add entries similar to
<network>
<name>default</name>
<uuid>cdaebe20-c72d-4434-932e-58e00de5e837</uuid>
<forward mode='nat'/>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:d9:5b:b9'/>
<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.2' end='192.168.122.254'/>
<host mac='52:54:00:d8:a0:f4' name='centosext' ip='192.168.122.11'/>
<host mac='52:54:00:2d:07:d3' name='centosext1' ip='192.168.122.22'/>
<host mac='52:54:00:17:1a:99' name='centosext2' ip='192.168.122.33'/>
<host mac='52:54:00:7b:f9:f3' name='centosext3' ip='192.168.122.44'/>
<host mac='52:54:00:df:5f:2f' name='centosext4' ip='192.168.122.55'/>
<host mac='52:54:00:d8:a0:f4' name='centosext5' ip='192.168.122.66'/>
<host mac='52:54:00:82:18:49' name='chefnode1' ip='192.168.122.77'/>
<host mac='52:54:00:8d:10:87' name='chefnode2' ip='192.168.122.88'/>
<host mac='52:54:00:c6:41:eb' name='chefnode3' ip='192.168.122.99'/>
<host mac='52:54:00:7a:73:a0' name='chefnode4' ip='192.168.122.100'/>
</dhcp>
</ip>
</network>
Lets restart the network interface.
# virsh net-destroy default
Network default destroyed
#virsh net-start default
Network default started
# virsh net-info default
Name: default
UUID: cdaebe20-c72d-4434-932e-58e00de5e837
Active: yes
Persistent: yes
Autostart: yes
Bridge: virbr0
Related Articles
How We Set Up Our KVM Hypervisor: From Bare Metal to Production-Ready VM Host
Detailed walkthrough of building a dedicated KVM/libvirt hypervisor with XFS tuning, hugepages, 10GbE tuning, and automation.
Installing eve-ng on ubuntu 24.04 LTS
Step-by-step guide to installing EVE-NG on Ubuntu 24.04 LTS bare metal server for running network labs.
Setting default gateway from virsh-net command line
Generally when a network is configured from virsh-define for a particular range say 192.168.4.0 it gateway will be automatically assigned to 192.168.4.1.