Skip to content

Instantly share code, notes, and snippets.

@plembo
Last active August 29, 2022 14:49
Show Gist options
  • Save plembo/3704d6596bd507803dce9bc72832e3b1 to your computer and use it in GitHub Desktop.
Save plembo/3704d6596bd507803dce9bc72832e3b1 to your computer and use it in GitHub Desktop.
Setting the search domain in DHCP for KVM

Setting the search domain in DHCP for KVM

The built-in DHCP server (based on dnsmasq) included with KVM (libvirtd) has limited configuration options compared to other DHCP servers like the one published by ISC, but it can be set up to specify a search domain for clients.

Edit the network configuration using virsh. The examples below assume configuration will be of a network named "default" (by default this is the first network set up by libvirtd).

$ virsh net-edit default

To specifiy the search domain, add the following line before the DHCP address range, then save.

<domain name='example.com' localOnly='no'/>

Effect the change by restarting the network updated:

$ virsh net-destroy default
$ virsh net-start default

In the above example the domain directive includes "localOnly='no'", which instructs the server to pass DNS requests through to the host machine's resolver rather than handling the query itself. This allows the guests to get results from any internal DNS server on the local network.

An example of a completed file follows:

<network>
  <name>default</name>
  <uuid>xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx</uuid>
  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='xx:xx:xx:xx:xx:xx'/>
  <domain name='example.com' localOnly='no'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.100' end='192.168.122.254'/>
      <host mac='xx:xx:xx:xx:xx:xx' name='guest1' ip='192.168.122.23'/>
      <host mac='xx:xx:xx:xx:xx:xx' name='guest2' ip='192.168.122.32'/>
    </dhcp>
  </ip>
</network>

The example also shows how to specify reserved IP addresses for specific guests, using their MAC addresses. This makes those addresses predictable over time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment