Monday, November 05, 2012

DHCP on Synology NAS (dhcpd and dnsmasq)

As an adjunct to my home network DNS configuration, I decided to move DHCP duties across to my always-on Synology NAS (DSM4.1) and disable the DHCP services on my routers. This was driven by a number of (not particularly compelling) factors, however my experience follows. This is as much a story about making mistakes as getting it right, so read through to the end before you start cutting and pasting commands into your live environment.

As Synology offers a DHCP server package, I installed this on the base installation via the DSM Package Centre utility. A new icon will appear in your apps list and the Control Panel->Networks applet gets a new 'DHCP Server' button. A form asks you to configure key details for your DHCP server, including primary and secondary DNS, domain name, lease time and so forth. You can also configure subnets, static IP mappings and other details. Neither of my routers support a particularly sophisticated DHCP management interface so this is one somewhat compelling reason to move away from allowing a vanilla consumer router to provide DHCP services on your network.

So the moment of truth arrives. Knowing that my DNS configuration on the NAS works I switch on DNS on the NAS and disable it on the routers. Needless to say, there were issues. The DHCP server on the NAS was broken but it wasn't obvious why, and now my devices were having issues getting onto the network (including the admin interfaces to my router and NAS...).

Running 'ps | grep dhcpd' shows that dhcpd is not running. This is despite no warnings or errors being issued by the Synology UI. The first stop is /var/log/messages for any clues that might have been left.

Nov  3 21:42:18 dhcpserver.cgi: net_get_dhcp_server_conf.c:164 File [/etc/dhcpd/dhcpd-bond0-bond00.conf] does not exist.
Nov  3 21:42:18 dhcpserver.cgi: dhcpserver.cpp:130 Cannot read Global setting on bond0 bond00
Nov  3 21:42:19 dhcpserver.cgi: dhcpserver.cpp:317 Can not open DHCP static file
Nov  3 21:42:19 dhcpserver.cgi: dhcpserver.cpp:410 Can not open DHCP lease file
All the files that are claimed not to exist are clearly extant:
dns> ls /etc/dhcpd
dhcpd-bond0-bond00.conf  dhcpd-static.conf        dhcpd.conf               dhcpd.info

I then tried to hunt down the init.d management scripts and dhcpd binary but these were nowhere to be found on the system. So I have basically let myself up the garden path as I'm not looking for the right thing. You can install regular old dhcpd if you like via ipkg install dhcp, but it won't work with the Synology's UI (web management interface) - at least not without some hackery. It didn't initially twig for me that under the hood Synology's DHCP Server uses dnsmasq ("a lightweight, easy to configure DNS forwarder and DHCP server"). Admittedly, whilst I had seen references to dnsmasq I did not know what it was precisely and it wasn't until I dug around in places like /etc/rc.network and recalled some error entries in /var/log/messages like the below that it made sense that dnsmasq was in use and why it wouldn't run.

dnsmasq[22230]: failed to create listening socket for port 53: Address already in use
dnsmasq[22230]: FAILED to start up

If you had previously installed the dns package, it would listen on port 53 and would prevent dnsmasq from starting. You may also have noticed in Synology's UI that although it reports that DHCP Service is running in Package Center, every time you navigate back to Control Panel->Network->Network Interface->DHCP Server, under the General tab the 'Enable DHCP Server' check box is always unchecked even if you see the 'Settings applied' message after clicking Apply, implying to me at least that it wasn't actually running.

If you're running named, kill it:
/opt/etc/init.d/S09named stop

Under /var/log/messages you may also see something like:
dhcpserver.cgi: dhcpserver.cpp:410 Can not open DHCP lease file

There is a post on the Synology site that has a simple remedy for this:
touch /var/packages/DHCPServer/target/etc/dhcpd.conf.leases
touch /var/packages/DHCPServer/target/etc/dhcpd-leases.log

On my NAS, only dhcpd-leases.log wasn't present so I ran the second command only and dnsmasq came up cleanly using the Synology UI. Check that it's running:

dns> ps | grep dnsmasq
20315 root      1620 S    dnsmasq --user=root --cache-size=200 --conf-file=/etc/dhcpd/dhcpd.conf --dhcp-lease-max=2147483648
20316 root      1620 S    dnsmasq --user=root --cache-size=200 --conf-file=/etc/dhcpd/dhcpd.conf --dhcp-lease-max=2147483648

A few more tips here:
  • within the Synology UI you need to ensure that under the table headed 'Subnet list' that the row(s) you have entered have green check marks next to them. It doesn't seem to check this box by default and will not work if unchecked.
  • (obviously) you need to turn off other DHCP server(s) on your network as appropriate. Chances are your router is running DHCP which will interfere with things.
  • consider how to manage addresses on your network. There are going to be a number of hosts that you will probably want to have static IP addresses (NAS, routers, other servers), but the rest can be dynamic. Use start/end addresses and reserved addresses to ensure you have enough addresses of each type and don't overlap between the static and dynamic address pools.
Testing out the operation of the dnsmasq DNS server reveals that it pretty much works like the old named server however there are a few minor updates to /etc/hosts to capture the static addresses and hosts on your network. This is easier than mucking about with forward and reverse lookup files in BIND IMHO.
  • after config changes, restart dnsmasq (or the DHCP Server under Package Center)
  • the Synology implementation looks to regenerate /etc/dhcpd/dhcpd.conf when you make changes in the DSM UI. dnsmasq supports a lot of options, so you may need to look into how to preserve any extended config you intend to remain persistent.
[Edit 25/11/2012]: I upgraded to DSM 4.1-2661 which caused some silly issues.
  • I didn't disable my named package. When the NAS upgrade completed and the system rebooted, my old named config was left lying around which meant /opt/etc/init.d/S09named was called and prevented dnsmasq from starting (both need port 53). All dns and dhcp services were down as a result and therefore no internet access until this got fixed, made all the more annoying as none of my computers could get on the network without manual intervention.
  • /etc/hosts looks like it got touched during the upgrade. I have no proof, but some static host/IPs configured in this file appeared to have disappeared. When I readded them and restarted dnsmasq these hosts would resolve properly on the network again.

3 comments:

  1. Great post, thanks for the help as I had no idea why I couldn't get DHCP to work.

    One addition - the /etc/rc.network script concats all dhcpd-*.conf files in /etc/dhcpd to generate the dhcpd.conf for dnsmasq. A quick and easy way to get your dnsmasq changes to persist through GUI changes is to add a file, say dhcpd-KEEPMYCHANGES.conf in that directory.

    Thanks again

    ReplyDelete
  2. Hey good post:

    I wrote up the fix for this on the synology forum, but basically, just add --port=0 to the dnsmasq line in /etc/rc.network

    http://forum.synology.com/enu/viewtopic.php?f=196&t=48076&p=238469#p238469

    ReplyDelete
  3. Following on from Anonymous's comment, I found that you also have to create a dhcpd-KEEPMYCHANGES.info file containing:

    enable="yes"

    ...in order for the rc.network file to pick up your dhcpd-KEEPMYCHANGES.conf file.

    ReplyDelete