Skip to content

Libvirt DNS and DHCP without Avahi

Pau Garcia Quiles edited this page Feb 21, 2020 · 2 revisions

DNS and DHCP without Avahi

If you do not want or cannot use Avahi (e. g. Windows minions), the easiest DHCP and DNS alternative is libvirt's own dnsmasq.

Initial preparation

Sumaform

Assuming you want to use the home.lab domain, add this to your main.tf:

use_avahi = false
domain = "home.lab"

Any other case

Assuming you want to use the home.lab domain, add this to your /etc/resolv.conf:

search home.lab

Configuring the libvirt network

If you are using the defaultvirtual network in the 192.168.122.1 network, you will have this interface:

$ virsh net-list --all
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes

You can edit the XML with virt-manager (do not forget to stop the interface before making changes, or they will be lost!) or with virsh:

$ sudo virsh net-edit default

It will look like this:

<network connections='3'>
<name>default</name>
<uuid>366c6da3-f7e3-413c-93ca-c4c89ef02ac4</uuid>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:78:04:82'/>
<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>

You can now add the MACs and desired IPs in the ip block, below the range entry. Please note I have changed the range start address to allocate space for static address leases. Your XML will look like this:

<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.10' end='192.168.122.254'/>
<host mac='52:54:00:09:af:bf' ip='192.168.122.2'/>
<host mac='52:54:00:76:78:dc' ip='192.168.122.3'/>
<host mac='52:54:00:90:15:99' ip='192.168.122.4'/>
</dhcp>
</ip>

We could add the hostnames to the XML too but in that case, name resolution would only work across virtual guest. As we want name resolution to work also between host and guest, we will now add the domain name to the XML, right after the mac address block. In the end, your XML will look like this:

<network connections='3'>
<name>default</name>
<uuid>366c6da3-f7e3-413c-93ca-c4c89ef02ac4</uuid>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:78:04:82'/>
<domain name='home.lab' localOnly='yes'/>
<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.10' end='192.168.122.254'/>
<host mac='52:54:00:09:af:bf' ip='192.168.122.2'/>
<host mac='52:54:00:76:78:dc' ip='192.168.122.3'/>
<host mac='52:54:00:90:15:99' ip='192.168.122.4'/>
</dhcp>
</ip>
</network>

Now edit /etc/hosts and add your guests:

192.168.122.2 uyuniserver.home.lab
192.168.122.3 leap151.home.lab
192.168.122.4 win10.home.lab

And now you will need to destroy and start again your network:

$ sudo virsh net-destroy default && virsh net-start default

Configuring NetworkManager

If you are using NetworkManager, tell it to control dnsmasq:

$ sudo vi /etc/NetworkManager/conf.d/localdns.conf
[main]
plugins=keyfile
dns=dnsmasq

But only for the home.lab domain:

$ sudo vi /etc/NetworkManager/dnsmasq.d/libvirt_dnsmasq.conf 
server=/home.lab/192.168.122.1

Alternative: no NetworkManager

If you are not using NetworkManager or do not want dnsmasq to be controlled by NetworkManager, use this configuration:

$ sudo vi /etc/NetworkManager/NetworkManager.conf
[main]
plugins=keyfile
dns=none

Tell your local dnsmasq to manage only home.lab:

$ sudo vi /etc/dnsmasq.conf
listen-address=127.0.0.1
interface=lo
bind-interfaces
server=<yourUpstreamDNS>
log-queries

# does not go upstream to resolve addresses ending in 'home.lab'
local=/home.lab/

And add localhost to your /etc/resolv.conf:

$ sudo vi /etc/resolv.conf
# This should be the first nameserver entry in resolv.conf!
search home.lab
nameserver 127.0.0.1

Finalize and test everything works as expected

Finally, restart all services: libvirtd, dnsmasq and NetworkManager:

$ sudo systemctl restart NetworkManager.service NetworkManager-dispatcher.service dnsmasq.service libvirtd.service libvirt-guests.service

And test name resolution from the host:

$ nslookup uyuniserver.home.lab 127.0.0.1
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   uyuniserver.home.lab
Address: 127.0.0.1

$ nslookup uyuniserver.home.lab 192.168.122.1
Server:         192.168.122.1
Address:        192.168.122.1#53

Name:   uyuniserver.home.lab
Address: 192.168.122.2

$ nslookup 192.168.122.2 192.168.122.1
2.122.168.192.in-addr.arpa      name = uyuniserver.home.lab.

and from the guests:

$ nslookup uyuniserver.home.lab 192.168.122.1
Server:         192.168.122.1
Address:        192.168.122.1#53

Name:   uyuniserver.home.lab
Address: 192.168.122.2

$ nslookup 192.168.122.2 192.168.122.1
2.122.168.192.in-addr.arpa      name = uyuniserver.home.lab.
Clone this wiki locally