DNS Server using dnsmasq

2 | Written on Tue 05 May 2020. Posted in Tutorials | Richard Walker

This is a quick guide on how to set up a Raspberry Pi to serve as a local DNS server. This is a requirement and first stage in a series for building a home lab environment.

Requirements

The following details my hardware and software version used in this guide:

  • Raspberry Pi 3 Model B Rev 1.2
  • 16GB Class 10 SanDisk MicroSD
  • CentOS-Userland-7-armv7hl-RaspberryPI-Minimal-2003-sda.raw.xz
  • Fedora 31 client

CentOS Linux on the Raspberry Pi 3

In this guide I'm setting up a local domain lab.home.com.

Prepare the MicroSD card

Install LZMA compression utilities:

dnf install xz

Extract the raw image:

unxz CentOS-Userland-7-armv7hl-RaspberryPI-Minimal-2003-sda.raw.xz

You will now have a 2.6GB raw file:

CentOS-Userland-7-armv7hl-RaspberryPI-Minimal-2003-sda.raw

Use fdisk to identify existing storage devices on your system, then insert the MicroSD card, using fdisk again to identify the card:

fdisk -l

[ ... output omitted ... ]
Disk /dev/sda: 14.86 GiB, 15931539456 bytes, 31116288 sectors
[ ... output omitted ... ]

Using dd copy the file to the card:

dd if=CentOS-Userland-7-armv7hl-RaspberryPI-Minimal-2003-sda.raw of=/dev/sda bs=8192; sync

The card can then be removed from you client and inserted into the Raspberry Pi.

Configure the Raspberry Pi

Power up the Raspberry Pi and log in as root with the default password of centos.

Expand the root partition

Check the current size:

df -h

Run rootfs-expand:

/usr/bin/rootfs-expand 

Check the new size:

df -h

Configure static IP address

Check the current DHCP IP address currently assigned to establish your local subnet:

ip a

[ ... output omitted ... ]
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether b8:27:eb:2d:d3:43 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.12/24 brd 192.168.0.255 scope global eth0
[ ... output omitted ... ]

In my case it 192.168.0.12.

Remove NetworkManger:

yum remove NetworkManager

Edit /etc/sysconfig/network-scripts/ifcfg-eth0 and configure it as a static IP, I'm setting it to 192.168.0.101:

DEVICE=eth0
TYPE=Ethernet
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.0.101
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
DNS1=192.168.0.1

The GATEWAY and DNS1 need to point to your main router.

Change the hostname:

hostnamectl set-hostname dns.lab.home.com

Reboot the Raspberry Pi and check everything is as expected and that networking is working.

Install and configure dnsmasq

Install dnsmasq

yum install dnsmasq -y

Enable the service:

systemctl enable dnsmasq

Start the service:

systemctl start dnsmasq

Make a backup of the existing dnsmasq.conf file for reference:

cp /etc/dnsmasq.conf /etc/dnsmasq.conf.orig

The following is a minimal example:

server=192.168.0.1
server=8.8.8.8
address=/lab.home.com/127.0.0.1
address=/lab.home.com/192.168.0.101
address=/ocp-cluster.lab.home.com/10.35.1.19
address=/apps.ocp-cluster.lab.home.com/10.35.1.20
interface=eth0
listen-address=::1,127.0.0.1,192.168.0.101
expand-hosts
domain=lab.home.com
dhcp-range=192.168.0.100,192.168.0.150
dhcp-leasefile=/var/lib/dnsmasq/dnsmasq.leases
dhcp-authoritative
conf-dir=/etc/dnsmasq.d,.rpmnew,.rpmsave,.rpmorig

The following demonstrates adding entries into /etc/hosts the specify hosts intended for an oVirt environment and and OpenShift deployment.

192.168.0.101   dnsmasq

192.168.0.110   engine-host
192.168.0.111   engine

192.168.0.121   ovirt-node1
192.168.0.122   ovirt-node2
192.168.0.123   ovirt-node3

192.168.0.131   nfs

10.35.1.19      api.ocp-cluster.lab.home.com
10.35.1.20      apps.ocp-cluster.lab.home.com

Hosts resolve.conf

Any host needed to resolve these domains will need to point to the dnsmasq server:

vi /etc/resolv.conf
nameserver 192.168.0.101

And to prevent NetworkManager undoing your change (use -i to revert):

chattr +i /etc/resolv.conf

COMMENTS