Create your own local domain and DHCP with dnsmasq
Back in 2007, Bernat
explained how to set up our own domain name using ISC BIND and ISC DHCP. You
can’t go wrong with those servers but maybe you prefer something more
straightforward. I present here a simpler alternative built on top of
dnsmasq
which is an
integrated DNS and DHCP.
What we are going to do is to configure our a forwarding DNS (so it will
forward queries to other DNS servers), it will provide us a DNS zone
.mydomain
and also DHCP.
The following example assumes that your LAN gateway is in 192.168.1.1
and we
will call it router.mydomain
. Your DNS server is in 192.168.1.2
and we
will call it dns.mydomain
. Install dnsmasq
in dns.mydomain
(check your
Linux distribution on how to do this). dnsmasq
settings are commonly found
in /etc/dnsmasq.conf
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Never forward plain names (without a dot or domain part)
domain-needed
# Never forward addresses in the non-routed address spaces
bogus-priv
# Don't read /etc/resolv.conf or any other
# file to get the forwarding files.
no-resolv
# Add other name servers here, with domain specs if they are for
# non-public domains.
server=8.8.8.8
server=8.8.4.4
# Add local-only domains here, queries in these domains are answered
# from /etc/hosts or DHCP only.
local=/mydomain/
# It does the following things.
# 1) Allows DHCP hosts to have fully qualified domain names, as long
# as the domain part matches this setting.
# 2) Sets the "domain" DHCP option thereby potentially setting the
# domain of all systems configured by DHCP
# 3) Provides the domain part for "expand-hosts"
domain=mydomain
# Uncomment this to enable the integrated DHCP server, you need
# to supply the range of addresses available for lease and optionally
# a lease time. If you have more than one network, you will need to
# repeat this for each network on which you want to supply DHCP
# service.
dhcp-range=192.168.1.32,192.168.1.250,24h
# Always give the host with Ethernet address 11:22:33:44:55:66
# the name fred and IP address 192.168.1.60 and lease time 45 minutes
# dhcp-host=11:22:33:44:55:66,fred,192.168.1.60,45m
dhcp-host=11:22:33:44:55:66,uber,192.168.1.3
dhcp-host=21:22:33:44:55:67,dad,192.168.1.4
dhcp-host=31:22:33:44:55:68,xbox360,192.168.1.5
# Override the default route supplied by dnsmasq, which assumes the
# router is the same machine as the one running dnsmasq.
dhcp-option=option:router,192.168.1.1
Option domain-needed
in line 2 is to make sure we don’t forward to DNS
servers plain names without a domain separator. So we will forward things like
xbox360.com
or xbox360.foo
but not xbox360
. Also option bogus-priv
avoids us forwarding
names like 3.1.168.192.in-addr.arpa
. This is a reverse DNS for an IP 192.168.1.3
which belongs
to the non-routable space of 192.168.0.0/24
Line 7 tells dnsmasq.conf
that does not try to use /etc/resolv.conf
to get
the forwarder servers. We will specify them, for maximum control, in
dnsmasq.conf
itself. We do this in lines 10 and 11. Here we use the Google
DNS servers, but feel free to use other servers such as OpenDNS or the one of
your ISP.
Line 14, local
, is a way to restrict the domains we’re going to asnswer
locally. Which we will enable in line 21, domain
. Now the names registered in
the DHCP will have a .mydomain
suffix as part of their fully qualified domain
name.
In line 27, dhcp-range
we configure the range of IPs for which we will assign
automatically DHCP addresses. In this example from 32 to 250, leaving us some
room from 192.168.1.1
to 192.168.1.32
and 192.168.1.250
to
192.168.1.254
. The DHCP lease will last 24 hours.
Next in lines 31 to 33 we statically assign IPs (should be from the range not
used by DHCP) to specific machines when they do a DHCP request. We match them
using their MAC address (such as 11:22:33:44:55:66
), use the right MAC
addresses of your network devices here.
Finally in line 36 we make sure that when a node in our network requests IP,
the gateway is correctly set to the IP of our LAN gateway (192.168.1.1
).
A final note for devices with fully static IPs (i.e. those that will never get
their IP via DHCP) such as router.mydomain
an dns.mydomain
. You can
use /etc/hosts
in dns.mydomain
to set them up. In our example setup we
would add the following lines. dnsmasq
will use this file to register those
names in its DNS database.
192.168.1.1 router.mydomain router
192.168.1.2 dns.mydomain dns