This post will explain how to Setup dnsmasq as DNS DHCP.
What is Dnsmaq
This description of dnsmasq shamelessly taken from the dnsmasq home page.
dnsmasq is a lightweight DNS, TFTP, PXE, router advertisement and DHCP server. It is intended to provide coupled DNS and DHCP service to a LAN.
Dnsmasq accepts DNS queries and either answers them from a small, local, cache or forwards them to a real, recursive, DNS server. It loads the contents of /etc/hosts so that local hostnames which do not appear in the global DNS can be resolved and also answers DNS queries for DHCP configured hosts. It can also act as the authoritative DNS server for one or more domains, allowing local names to appear in the global DNS.
The dnsmasq DHCP server supports static address assignments and multiple networks. It automatically sends a sensible default set of DHCP options, and can be configured to send any desired set of DHCP options, including vendor-encapsulated options. It includes a secure, read-only, TFTP server to allow net/PXE boot of DHCP hosts and also supports BOOTP. The PXE support is full featured, and includes a proxy mode which supplies PXE information to clients whilst DHCP address allocation is done by another server.
The dnsmasq DHCPv6 server provides the same set of features as the DHCPv4 server, and in addition, it includes router advertisements and a neat feature which allows naming for clients which use DHCPv4 and stateless auto-configuration only for IPv6 configuration. There is support for doing address allocation (both DHCPv6 and RA) from subnets which are dynamically delegated via DHCPv6 prefix delegation.
Dnsmasq is coded with small embedded systems in mind. It aims for the smallest possible memory footprint compatible with the supported functions, and allows unneeded functions to be omitted from the compiled binary.
In short, IT IS EXCELLENT!!
Installing dnsmasq is just a case of using apt-get.
sudo apt-get install dnsmasq
Setup dnsmasq as DNS DHCP
Looking at the file /etc/dnsmasq.conf first. The lines are listed are those that I changed from their defaults. Just uncomment and amend them as necessary. (Remove the ‘#’ from the beginning of the line).
sudo nano /etc/dnsmasq.conf
domain-needed bogus-priv no-resolv no-poll server=/example.com/192.168.0.5 server=22.214.171.124 server=126.96.36.199 local=/example.com/ address=/doubleclick.net/127.0.0.1 no-hosts addn-hosts=/etc/dnsmasq_static_hosts.conf expand-hosts domain=example.com dhcp-range=192.168.0.20,192.168.0.50,72h dhcp-range=tftp,192.168.0.250,192.168.0.254 dhcp-host=mylaptop,192.168.0.199,36h dhcp-option=option:router,192.168.0.1 dhcp-option=option:ntp-server,192.168.0.5 dhcp-option=19,0 # ip-forwarding off dhcp-option=44,192.168.0.5 # set netbios-over-TCP/IP aka WINS dhcp-option=45,192.168.0.5 # netbios datagram distribution server dhcp-option=46,8 # netbios node type
What these lines will do for you.
- domain-needed This tells dnsmasq to never pass short names to the upstream DNS servers. If the name is not in the local /etc/hosts file then “not found” will be returned.
- bogus-priv All reverse IP (192.168.x.x) lookups that are not found in /etc/hosts will be returned as “no such domain” and not forwarded to the upstream servers.
- no-resolv Do not read resolv.conf to find the servers where to lookup dns.
- no-poll Do not poll resolv.conf for changes
- server=188.8.131.52 Set one or more DNS servers to use when addresses are not local. These are 184.108.40.206 Google DNS server and open DNS server, 220.127.116.11.
- local=/example.com/ Our local domain, queries in these domains are answered from /etc/hosts or the static-hosts files.
- address=/doubleclick.net/127.0.0.1 Use this force an address for the specified domains. e.g to block adverts forced by doubleclck.net to localhost
- no-hosts This options stops dnsmasq using the local /etc/hosts file as a source for lookups .
- addn-hosts=/etc/dnsmasq_static_hosts.conf Force dnsmasq to use this file for lookups. It is in the same format as /etc/hosts.
- expand_hosts So we can see our local hosts via our home domain without having to repeatedly specify the domain in our /etc/hosts file.
- domain This is your local domain name. It will tell the DHCP server which host to give out IP addresses for.
- dhcp-range This is the range of IPs that DHCP will serve: 192.168.0.20 to 192.168.0.50, with a lease time of 72 hours. The lease time is how long that IP will be linked to a host. (All most 🙂 )
- dhcp-range=tftp,192.168.0.250,192.168.0.255 For tftp connections use this range of IP addresses
- dhcp-host=mylaptop,192.168.0.199,36h Any machine saying they are hostname = ‘mylaptop’ gets this IP address
- dhcp-option=option:router,192.168.0.1 When a host is requesting an IP address via DHCP also tell it the gateway to use.
- dhcp-option=option:ntp-server,192.168.0.5 When a host is requesting an IP address via DHCP also tell it the NTP to use.
In the file /etc/dnsmasq_static_hosts.conf you can add a list of local machines with static IP addresses in the same format as the hosts file. It is also an easy way of creating aliases or CNAME records.
192.168.0.8 mail mail.example.com 192.168.0.9 smtp smtp.example.com 192.168.0.120 mythtvbox mythtvbox.example.com
Starting and stopping the service
sudo service dnsmasq start sudo service dnsmasq stop sudo service dnsmasq restart