Configure Dnsmasq on FreeBSD For Local Network

Dnsmasq is a DNS and DHCP server for local networks.

A DNS server translates urls into IP addresses. DHCP is dynamic host configuration protocol, a method of assigning an IP address to computers on a network.

I have a wifi router that serves as the DNS and DHCP server on my network. When my phone connects to the wifi, the router assigns an IP address and provides it's own ip, 192.168.0.1, as the DNS provider to use. When I browse the web on my phone, it looks up URLs at the router's IP, which forwards the request on to the configured DNS name servers.

My router got the nameservers for my ISP when it connected. I could override this with one of the public nameservers from google or cloudflare.

I don't need to use dnsmasq as a DHCP server because the router is working fine, but I will need to tell other computers to use the computer running dnsmasq as the DNS nameserver.

But first, let's install dnsmasq.

sudo pkg install dnsmasq

Configuration is at /usr/local/etc/dnsmasq.conf.

# Never forward plain names (without a dot or domain part)
domain-needed
# Never forward addresses in the non-routed address spaces.
bogus-priv

# By  default,  dnsmasq  will  send queries to any of the upstream
# servers it knows about and tries to favour servers to are  known
# to  be  up.  Uncommenting this forces dnsmasq to try each query
# with  each  server  strictly  in  the  order  they   appear   in
# /etc/resolv.conf
strict-order

# Add other name servers here, with domain specs if they are for
# non-public domains.
server=8.8.8.8 # google
server=1.1.1.1 # cloudflare

# Add local-only domains here, queries in these domains are answered
# from /etc/hosts or DHCP only.
local=/local.mantra.network/

# If you want dnsmasq to listen for DHCP and DNS requests only on
# specified interfaces (and the loopback) give the name of the
# interface (eg eth0) here.
# Repeat the line for more than one interface.
#interface=
# Or you can specify which interface _not_ to listen on
#except-interface=
# Or which to listen on by address (remember to include 127.0.0.1 if
# you use this.)
listen-address=::1,127.0.0.1,192.168.0.100
# If you want dnsmasq to provide only DNS service on an interface,
# configure it as shown above, and then use the following line to
# disable DHCP and TFTP on it.
#no-dhcp-interface=

# Set this (and domain: see below) if you want to have a domain
# automatically added to simple names in a hosts-file.
expand-hosts
# Set the domain for dnsmasq. this is optional, but if it is set, 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=local.mantra.network

# Set the cachesize here.
cache-size=1500
# For debugging purposes, log each DNS query as it passes through
# dnsmasq.
log-queries

/etc/resolv.conf has configuration for DNS nameservers, and should be set to 127.0.0.1 to use dnsmasq.

nameserver 127.0.0.1

/etc/hosts maps host names to IP addresses. Dnsmasq appends the domain name with expand-hosts enabled.

I am a bit confused with the local option vs domain. Maybe the local is not relevant here and domain is the one I need.

I had to stop unbound to get this working, I'll have to look into that more to see what it did.

sudo service local_unbound stop

Restart dnsmasq to get the new settings.

sudo service dnsmasq restart

Now I can get an IP for infoplex.local.mantra.network.

root@infoplex:/usr/local/etc # host infoplex.local.mantra.network
infoplex.local.mantra.network has address 192.168.0.100
Topics