Dnsmasq(as DHCP server) isn’t working in KVM+libvirt envirmont

dhcpdnsmasqkvm-virtualizationlibvirt

I'm using dnsmasq as DHCP server in VM environment.
But It didn't working. I disabled basic DHCP feature in libvirt.

<network>
  <name>default</name>
  <uuid>84da0678-e56d-8fc2-6f8b-e8eba784849a</uuid>
  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0' />
  <mac address='52:54:00:7B:64:0B'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
  </ip>
</network>

As you can see, I removed this tag!

<dhcp>
  <range start='192.168.122.2' end='192.168.122.254' />
</dhcp> 

And I installed dnsmasq in Host machine. During installation dnsmasq, there was an error message about 127.0.0.1.(dnsmasq: failed to create listening socket for 127.0.0.1) So I commented out listen-address option, and added dhcp-range/dhcp-option options, like this.

listen-address=127.0.0.1
dhcp-range=192.168.122.100,192.168.122.200,24h
dhcp-option=option:router,192.168.122.1

That's all I've done with dnsmasq. But guest VM couldn't get IP address from host which is dnsmasq server running.

After that , I installed isc-dhcp-server instead of dnsmasq…. and it works! But I still want to use dnsmasq instead of isc-dhcp-server. Are there any helping hands?

  • I disabled host machine's firewall.
  • I've heard that libvirt basically use dnsmasq. Is this the reason why I couldn't use dnsmasq in libvirt environment?

Best Answer

Libvirt does indeed use DNSMasq to handle DHCP. If you're trying to do some advanced configs with DNSMasq, you need to make sure Libvirt is not interfering (removing the DHCP option from your config would work).

Your actual issue appears to be caused by the 'listen-address' statement. DNSMasq requires a valid IP address in the subnet you're trying to assign to. You should configure it to use an IP in the 192.168.122.0/24 range. If you don't do this, it won't actually be able to assign any IP addresses.