You can assign a static IP address to a guest virtual machine (VM) running on a KVM hypervisor using DHCP by defining a DHCP reservation for the VM’s MAC address. This is usually done in the DHCP server configuration file, which varies depending on the DHCP server software you are using.
Here’s an example of how you can do this with ISC DHCP server on a Linux host:
- Identify the MAC address of the virtual NIC of the guest VM:
virsh domiflist guest_vm_name
- Open the DHCP server configuration file, typically located at
/etc/dhcp/dhcpd.conf
, and add a host declaration for the guest VM, using its MAC address and the desired IP address:host guest_vm_name {
hardware ethernet MAC_address;
fixed-address desired_IP_address;
}
- Restart the DHCP server to apply the changes:
sudo service isc-dhcp-server restart
- Finally, configure the virtual NIC of the guest VM to use DHCP:
virsh net-update default add-last ip-dhcp-host "<host mac='MAC_address' name='guest_vm_name' ip='desired_IP_address' />" --live --config
Note: The exact steps may vary depending on the version of Linux, the DHCP server software and the setup of your KVM hypervisor.