09-30-2020, 05:14 PM
Use dnsmasq to provide DNS & DHCP services
Many tech enthusiasts find the ability to control their host name resolution important. Setting up servers and services usually requires some form of fixed address, and sometimes also requires special forms of resolution such as defining Kerberos or LDAP servers, mail servers, etc. All of this can be achieved with dnsmasq.
dnsmasq is a lightweight and simple program which enables issuing DHCP addresses on your network and registering the hostname & IP address in DNS. This configuration also allows external resolution, so your whole network will be able to speak to itself and find external sites too.
This article covers installing and configuring dnsmasq on either a virtual machine or small physical machine like a Raspberry Pi so it can provide these services in your home network or lab. If you have an existing setup and just need to adjust the settings for your local workstation, read the previous article which covers configuring the dnsmasq plugin in NetworkManager.
[b]Install dnsmasq[/b]
First, install the dnsmasq package:
Next, enable and start the dnsmasq service:
Configure dnsmasq
First, make a backup copy of the dnsmasq.conf file:
Next, edit the file and make changes to the following to reflect your network. In this example, mydomain.org is the domain name, 192.168.1.10 is the IP address of the dnsmasq server and 192.168.1.1 is the default gateway.
Insert the following contents:
Test the config to check for typos and syntax errors:
Now edit the hosts file, which can contain both statically- and dynamically-allocated hosts. Static addresses should lie outside the DHCP range you specified earlier. Hosts using DHCP but which need a fixed address should be entered here with an address within the DHCP range.
The first two lines should be there already. Add the remaining lines to configure the router, the dnsmasq server, and two additional servers.
Restart the dnsmasq service:
Next add the services to the firewall to allow the clients to connect:
Test name resolution
First, install bind-utils to get the nslookup and dig packages. These allow you to perform both forward and reverse lookups. You could use ping if you’d rather not install extra packages. but these tools are worth installing for the additional troubleshooting functionality they can provide.
Now test the resolution. First, test the forward (hostname to IP address) resolution:
Next, test the reverse (IP address to hostname) resolution:
Finally, test resolving hostnames outside of your network:
[b]Test DHCP leases[/b]
To test DHCP leases, you need to boot a machine which uses DHCP to obtain an IP address. Any Fedora variant will do that by default. Once you have booted the client machine, check that it has an address and that it corresponds to the lease file for dnsmasq.
From the machine running dnsmasq:
[b]Extending functionality[/b]
You can assign hosts a fixed IP address via DHCP by adding it to your hosts file with the address you want (within your DHCP range). Do this by adding into the dnsmasq.conf file the following line, which assigns the IP listed to any host that has that name:
Alternatively, you can specify a MAC address which should always be given a fixed IP address:
You can specify a PXE boot server if you need to automate machine builds
This should point to the actual URL of your TFTP server.
If you need to specify SRV or TXT records, for example for LDAP, Kerberos or similar, you can add these:
There are many other options in dnsmasq. The comments in the original config file describe most of them. For full details, read the man page, either locally or online.
Many tech enthusiasts find the ability to control their host name resolution important. Setting up servers and services usually requires some form of fixed address, and sometimes also requires special forms of resolution such as defining Kerberos or LDAP servers, mail servers, etc. All of this can be achieved with dnsmasq.
dnsmasq is a lightweight and simple program which enables issuing DHCP addresses on your network and registering the hostname & IP address in DNS. This configuration also allows external resolution, so your whole network will be able to speak to itself and find external sites too.
This article covers installing and configuring dnsmasq on either a virtual machine or small physical machine like a Raspberry Pi so it can provide these services in your home network or lab. If you have an existing setup and just need to adjust the settings for your local workstation, read the previous article which covers configuring the dnsmasq plugin in NetworkManager.
[b]Install dnsmasq[/b]
First, install the dnsmasq package:
Code:
sudo dnf install dnsmasqNext, enable and start the dnsmasq service:
Code:
sudo systemctl enable --now dnsmasqFirst, make a backup copy of the dnsmasq.conf file:
Code:
sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.origNext, edit the file and make changes to the following to reflect your network. In this example, mydomain.org is the domain name, 192.168.1.10 is the IP address of the dnsmasq server and 192.168.1.1 is the default gateway.
Code:
sudo vi /etc/dnsmasq.confInsert the following contents:
Code:
domain-needed
bogus-priv
no-resolv
server=8.8.8.8
server=8.8.4.4
local=/mydomain.org/
listen-address=::1,127.0.0.1,192.168.1.10
expand-hosts
domain=mydomain.org
dhcp-range=192.168.1.100,192.168.1.200,24h
dhcp-option=option:router,192.168.1.1
dhcp-authoritative
dhcp-leasefile=/var/lib/dnsmasq/dnsmasq.leasesTest the config to check for typos and syntax errors:
Code:
$ sudo dnsmasq --test
dnsmasq: syntax check OK.Now edit the hosts file, which can contain both statically- and dynamically-allocated hosts. Static addresses should lie outside the DHCP range you specified earlier. Hosts using DHCP but which need a fixed address should be entered here with an address within the DHCP range.
Code:
sudo vi /etc/hostsThe first two lines should be there already. Add the remaining lines to configure the router, the dnsmasq server, and two additional servers.
Code:
127.0.0.1 localhost localhost.localdomain
::1 localhost localhost.localdomain
192.168.1.1 router
192.168.1.10 dnsmasq
192.168.1.20 server1
192.168.1.30 server2Restart the dnsmasq service:
Code:
sudo systemctl restart dnsmasqNext add the services to the firewall to allow the clients to connect:
Code:
sudo firewall-cmd --add-service={dns,dhcp}sudo firewall-cmd --runtime-to-permanentFirst, install bind-utils to get the nslookup and dig packages. These allow you to perform both forward and reverse lookups. You could use ping if you’d rather not install extra packages. but these tools are worth installing for the additional troubleshooting functionality they can provide.
Code:
sudo dnf install bind-utilsNow test the resolution. First, test the forward (hostname to IP address) resolution:
Code:
$ nslookup server1
Server: 127.0.0.1
Address: 127.0.0.1#53
Name: server1.mydomain.org
Address: 192.168.1.20Next, test the reverse (IP address to hostname) resolution:
Code:
$ nslookup 192.168.1.20
20.1.168.192.in-addr.arpa name = server1.mydomain.org.Finally, test resolving hostnames outside of your network:
Code:
$ nslookup fedoramagazine.org
Server: 127.0.0.1
Address: 127.0.0.1#53
Non-authoritative answer:
Name: fedoramagazine.org
Address: 35.196.109.67To test DHCP leases, you need to boot a machine which uses DHCP to obtain an IP address. Any Fedora variant will do that by default. Once you have booted the client machine, check that it has an address and that it corresponds to the lease file for dnsmasq.
From the machine running dnsmasq:
Code:
$ sudo cat /var/lib/dnsmasq/dnsmasq.leases
1598023942 52:54:00:8e:d5:db 192.168.1.100 server3 01:52:54:00:8e:d5:db
1598019169 52:54:00:9c:5a:bb 192.168.1.101 server4 01:52:54:00:9c:5a:bbYou can assign hosts a fixed IP address via DHCP by adding it to your hosts file with the address you want (within your DHCP range). Do this by adding into the dnsmasq.conf file the following line, which assigns the IP listed to any host that has that name:
Code:
dhcp-host=myhostAlternatively, you can specify a MAC address which should always be given a fixed IP address:
Code:
dhcp-host=11:22:33:44:55:66,192.168.1.123You can specify a PXE boot server if you need to automate machine builds
Code:
tftp-root=/tftpbootdhcp-boot=/tftpboot/pxelinux.0,boothost,192.168.1.240This should point to the actual URL of your TFTP server.
If you need to specify SRV or TXT records, for example for LDAP, Kerberos or similar, you can add these:
Code:
srv-host=_ldap._tcp.mydomain.org,ldap-server.mydomain.org,389srv-host=_kerberos._udp.mydomain.org,krb-server.mydomain.org,88srv-host=_kerberos._tcp.mydomain.org,krb-server.mydomain.org,88srv-host=_kerberos-master._udp.mydomain.org,krb-server.mydomain.org,88srv-host=_kerberos-adm._tcp.mydomain.org,krb-server.mydomain.org,749srv-host=_kpasswd._udp.mydomain.org,krb-server.mydomain.org,464txt-record=_kerberos.mydomain.org,KRB-SERVER.MYDOMAIN.ORGThere are many other options in dnsmasq. The comments in the original config file describe most of them. For full details, read the man page, either locally or online.

