Hostname IP resolution for KVM guests
The libvirt manages the DHCP of each KVM guest. Wouldn't it be nice if the DHCP IP address mappings were correlated with the host DNS lookup so IP addresses of the guests were available via VM host name?[1]
This is how to setup you KVM host so VM guest IP addresses are available by DNS host lookup:
- install libnss-libvirt
apt install libnss-libvirt
- configure /etc/nsswitch.conf to search libvirt and libvirt_guest for DNS records.
hosts: files libvirt libvirt_guest mdns4_minimal dns myhostname mymachines [NOTFOUND=return]
🚩 Note:Make sure you name your VM guests hostname the same as their virsh domain-name.
- Obtain the list of active KVM domain (hosts if you follow the above note):
virst list
Now you can confirm what guest VM hosts are running and use those hostnames to confirm that the host name lookup resloves to the guest's NIC IP address. It has been so painful up to this date (2023-05-30) maintaining guest host names in /etc/hostname; I am glad that is all automatic now.