29.3 C

How To Install & configure Dnsmasq on Ubuntu 18.04 LTS

Today we are going to Install & configure Dnsmasq DNS Server on Ubuntu 18.04 LTS. Dnsmasq is a simple, lightweight, easy to use & manage DNS server along with support for Lua scripting, IPv6, DNSSEC etc. It has a small footprint hence, suitable for resource-constrained routers & firewalls.

Dnsmasq has been designed to provide DNS, and optionally DHCP/TFTP services for a small to mid-size networks. Fist of all we will look at Dnsmasq subsystems & then move toward the Installation.

Dnsmasq has three main subsystems.

  • DNS subsystem: Used for caching of A. AAAA,CNAME & PTR.
  • DHCP subsystem: It supports DHCPv4, DHCPv6,BOTP & PXE.
  • Router Advertisement: Provides basic autoconfiguration for IPv6 host.

Step 1: Installing Dnsmasq on Ubuntu 18.04

Disable systemd-resolve as it binds to port 53 due to which Dnsmasq will be effected.

- Advertisement -

Type the given command to disable the resolved service.

sudo systemctl disable systemd-resolved
sudo systemctl stop systemd-resolved

Also, remove the sysmlinked resolv.conf file by

$ ls -lh /etc/resolv.conf 
lrwxrwxrwx 1 root root 39 Aug 8 15:52 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
$ sudo rm /etc/resolv.conf

Then create new resolv.conf file

echo "nameserver" > /etc/resolv.conf

As Dnsmasq is available on the apt repository, install it by typing

sudo apt-get install dnsmasq

You can find the main config file for Dnsmasq under /etc/dnsmasq.conf

sudo nano /etc/dnsmasq.conf

See the minimal configuration

Listen on this specific port instead of the standard DNS port
(53). Setting this to zero completely disables DNS function,
leaving only DHCP and/or TFTP.
Never forward plain names (without a dot or domain part)
Never forward addresses in the non-routed address spaces.
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
Set this (and domain: see below) if you want to have a domain
automatically added to simple names in a hosts-file.
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"
Set Liste address
listen-address= # Set to Server IP for network responses

To enable DNSSEC validation & caching, uncomment


Do the config according to your own environment & restart dnsmasq services.

sudo systemctl restart dnsmasq

Step 2: Add DNS records to Dnsmasq

Add DNS records in the file /etc/hosts/. Dnsmasq will reply to queries from clients using these records.

$ sudo vim /etc/hosts server1.mypridomain.com erp.mypridomain.com checkout.mypridomain.com hello.world

Then restart dnsmasq services.

sudo systemctl restart dnsmasq

Step 3: Test Dnsmasq functionality

Edit the file /etc/network/interfaces for persistent configuration, or the file /etc/netplan/ on Ubuntu 18.04 servers. As it is a test, I’ll modify runtime file /etc/resolv.conf

$ sudo nano /etc/resolv.conf

Then test using dig:

$ dig A erp.mypridomain.com
; <<>> DiG 9.11.3-1ubuntu1.1-Ubuntu <<>> A erp.mypridomain.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 43392
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
; EDNS: version: 0, flags:; udp: 4096
;erp.mypridomain.com. IN A
erp.mypridomain.com. 0 IN A
;; Query time: 0 msec
;; WHEN: Tue Aug 21 10:35:41 UTC 2018
;; MSG SIZE rcvd: 64

See the other example

$ dig checkout.mypridomain.com A +noall +answer
; <<>> DiG 9.11.3-1ubuntu1.1-Ubuntu <<>> checkout.mypridomain.com A +noall +answer
;; global options: +cmd
checkout.mypridomain.com. 0 IN A

Confirm that we are getting responses as configured.

Step 3:Configure Dnsmasq as DHCP Server (Optional)

Configure /etc/dnsmasq.conf & provide DHCP options. You need to provide.

  • Gateway IP address
  • DNS Server IP address
  • Network Subnet mask
  • DHCP Addresses range
  • NTP Server

Look at the example


Then restart dnsmasq services & configure clients to obtain an IP address from this server.

sudo systemctl restart dnsmasq
- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
"The best Linux newsletter on the web"


Please enter your comment!
Please enter your name here

Latest article