RPi as a DHCP + DNS server

Some time ago I installed the arch linux distribution to my Raspberry Pi device. My plan was to eventually start using the RPi as a local DHCP + DNS server. I decided to use dnsmasq which combines both the DHCP and the DNS server functionalities. One of the nice things about dnsmasq is that after it has provided an IP address and a name to a DHCP client it will store that information to the local DNS database. Then we can ask the local DNS server to resolve any DHCP client name to the corresponding IP address.

Restoring the time during reboots

The RPi does not contain a real-time clock. This means that during reboot the time is lost until it is reset manually or by using NTP. It also means that the journal is messed up as the message time stamps are not in a chronological order. However, we can easily resynchronize the time by NTP or by the simpler systemd-timesyncd service. Edit the configuration file in /etc/systemd/timesyncd.conf and start the service with:

#  timedatectl set-ntp true

Setting up a static IP address for the server

I already setup RPi to use a static IP address using the netctl-ifplugd unit. However, after some experimenting with dnsmasq I decided to select the basic network configuration approach with the systemd.networkd unit.

So the first thing to do is just to stop and disable the netctl-ifplugd unit.

# systemctl stop netctl-ifplugd@eth0
# systemctl disable netctl-ifplugd@eth0

Then we can enable the networkd and resolved units.

# systemctl enable systemd-networkd.service
# systemctl enable systemd-resolved.service

The resolv.conf file under /etc should be a symlink to the runtime configuration file.

# cd /etc
# mv resolv.conf resolv.conf.orig
# ln -s /run/systemd/resolve/resolv.conf

The network configuration file is /etc/systemd/network/wired.network.

[Match]
Name=eth0
[Network]
DNS=192.168.1.1
[Address]
Address=192.168.1.6/24
[Route]
Gateway=192.168.1.1

Finally start the network services.

# systemctl start systemd-networkd.service
# systemctl start systemd-resolved.service

 Installing dnsmasq

The dnsmasq app can be installed with pacman.

# pacman -S dnsmasq
resolving dependencies...
looking for conflicting packages...Packages (1) dnsmasq-2.72-2Total Download Size:   0.16 MiB
Total Installed Size:  0.38 MiB:: Proceed with installation? [Y/n] y
:: Retrieving packages ...
dnsmasq-2.72-2-armv6h    161.8 KiB   539K/s 00:00 [######################] 100%
(1/1) checking keys in keyring                     [######################] 100%
(1/1) checking package integrity                   [######################] 100%
(1/1) loading package files                        [######################] 100%
(1/1) checking for file conflicts                  [######################] 100%
(1/1) checking available disk space                [######################] 100%
(1/1) installing dnsmasq                           [######################] 100%

Configuring dnsmasq

Edit /etc/hosts and add all the static IP addresses defined for the LAN hosts there. E.g. the DHCP host itself and the router could be added (with an optional LAN domain suffix).

192.168.1.1    gateway.home    gateway
192.168.1.6    arch-linux.home arch-linux

Starting the DHCP and DNS servers

The dnsmasq app takes care of both the DHCP and caching DNS server duties. So let’s enable it and let’s fire it up!

# systemctl enable dnsmasq
# systemctl start dnsmasq

 

Leave a Reply

Your email address will not be published. Required fields are marked *