Difference between revisions of "Dnsmasq"

From ArchWiki
Jump to: navigation, search
m (NetworkManager)
(NetworkManager: BugFix: /run/NetworkManager/dnsmasq.conf --> /run/NetworkManager/resolv.conf)
 
(94 intermediate revisions by 34 users not shown)
Line 1: Line 1:
 +
{{Lowercase title}}
 
[[Category:Domain Name System]]
 
[[Category:Domain Name System]]
 +
[[es:Dnsmasq]]
 
[[it:Dnsmasq]]
 
[[it:Dnsmasq]]
 +
[[ja:Dnsmasq]]
 +
[[pt:Dnsmasq]]
 
[[ru:Dnsmasq]]
 
[[ru:Dnsmasq]]
 
[[zh-CN:Dnsmasq]]
 
[[zh-CN:Dnsmasq]]
{{Lowercase_title}}
+
[http://www.thekelleys.org.uk/dnsmasq/doc.html dnsmasq] provides services as a DNS cacher and a DHCP server.  As a Domain Name Server (DNS) it can cache DNS queries to improve connection speeds to previously visited sites, and as a DHCP server dnsmasq can be used to provide internal IP addresses and routes to computers on a LAN.  Either or both of these services can be implemented.  dnsmasq is considered to be lightweight and easy to configure; it is designed for personal computer use or for use on a network with less than 50 computers. It also comes with a [[PXE]] server.
  
'''dnsmasq''' provides services as a DNS cacher and a DHCP server.  As a Domain Name Server (DNS), it can cache DNS queries to improve connection speed to previously visited sites.  As a DHCP server, {{Pkg|dnsmasq}} can be used to provide internal IP addresses and routes to computers on a LAN.  Either or both of these services can be implemented.  dnsmasq is considered to be lightweight and easy to configure; it is designed for personal computer use or for use on a network with less than 50 computers. It also comes with a [[PXE]] server.
+
== Installation ==
  
== Installing ==
+
[[Install]] {{Pkg|dnsmasq}}.
  
[[pacman|Install]] {{Pkg|dnsmasq}} from the [[Official Repositories|official repositories]].
+
== Configuration ==
  
== DNS Cache Setup ==
+
To configure dnsmasq, you need to edit {{ic|/etc/dnsmasq.conf}}. The file contains extensive comments explaining its options.
  
To set up dnsmasq as a DNS caching daemon on a single computer edit {{ic|/etc/dnsmasq.conf}} and uncomment the localhost listening address:
+
{{Warning|dnsmasq by default enables its DNS server. If you do not require it, you need to explicitly disable it by setting DNS port to {{ic|0}}:
 +
{{hc|/etc/dnsmasq.conf|2=port=0}}
 +
}}
 +
 
 +
{{Tip|To check configuration file(s) syntax, execute:
 +
$ dnsmasq --test
 +
}}
 +
 
 +
== DNS cache setup ==
 +
 
 +
To set up dnsmasq as a DNS caching daemon on a single computer edit {{ic|/etc/dnsmasq.conf}} and uncomment the {{ic|listen-address}} directive, adding in the localhost IP address:
  
 
  listen-address=127.0.0.1
 
  listen-address=127.0.0.1
  
To use this computer to act as a default DNS specify the fixed IP address of the network:
+
To use this computer to listen on its LAN IP address for other computers on the network:
 +
 
 +
listen-address=192.168.1.1    # Example IP
 +
 
 +
It is recommended that you use a static LAN IP in this case.
 +
 
 +
Multiple ip address settings:
 +
 
 +
listen-address=127.0.0.1,192.168.1.1
 +
 
 +
=== DNS addresses file ===
 +
 
 +
{{Merge|resolv.conf|Same topic. Also note that most of this can also be done natively in {{ic|/etc/resolvconf.conf}} using the {{ic|name_servers}} and {{ic|name_servers_append}} options.}}
 +
 
 +
After configuring dnsmasq, the DHCP client will need to prepend the localhost address to the known DNS addresses in {{ic|/etc/resolv.conf}}.  This causes all queries to be sent to dnsmasq before trying to resolve them with an external DNS.  After the DHCP client is configured, the network will need to be restarted for changes to take effect.
 +
 
 +
==== resolv.conf ====
 +
 
 +
One option is a pure {{ic|resolv.conf}} configuration. To do this, just make the first nameserver in {{ic|/etc/resolv.conf}} point to localhost:
 +
 
 +
{{hc|/etc/resolv.conf|
 +
nameserver 127.0.0.1
 +
# External nameservers
 +
...
 +
}}
 +
 
 +
Now DNS queries will be resolved first with dnsmasq, only checking external servers if dnsmasq cannot resolve the query. {{Pkg|dhcpcd}}, unfortunately, tends to overwrite {{ic|/etc/resolv.conf}} by default, so if you use DHCP it is a good idea to protect {{ic|/etc/resolv.conf}}. To do this, append {{ic|nohook resolv.conf}} to the dhcpcd config file:
 +
 
 +
{{hc|/etc/dhcpcd.conf|
 +
...
 +
nohook resolv.conf}}
 +
 
 +
It is also possible to write protect your resolv.conf:
 +
 
 +
# chattr +i /etc/resolv.conf
 +
 
 +
===== More than three nameservers =====
 +
 
 +
A limitation in the way Linux handles DNS queries is that there can only be a maximum of three nameservers used in {{ic|resolv.conf}}. As a workaround, you can make localhost the only nameserver in {{ic|resolv.conf}}, and then create a separate {{ic|resolv-file}} for your external nameservers. First, create a new resolv file for dnsmasq:
  
listen-address=<192.168.1.1>  # Example IP
+
{{hc|/etc/resolv.dnsmasq.conf|
 +
# Google's nameservers, for example
 +
nameserver 8.8.8.8
 +
nameserver 8.8.4.4
 +
}}
  
=== DNS Addresses File ===
+
And then edit {{ic|/etc/dnsmasq.conf}} to use your new resolv file:
  
After configuring dnsmasq the DHCP client will need to prepend the localhost address to the known DNS addresses in {{ic|/etc/resolv.conf}}. This causes all queries to be sent to dnsmasq before trying to resolve them with an external DNS. After the DHCP client is configured the network will need to be restarted for changes to take effect.
+
{{hc|/etc/dnsmasq.conf|
 +
...
 +
resolv-file&#61;/etc/resolv.dnsmasq.conf
 +
...
 +
}}
  
 
==== dhcpcd ====
 
==== dhcpcd ====
  
{{Pkg|dhcpcd}} has the ability to prepend or append nameservers to {{ic|/etc/resolv.conf}} by creating (or editing) the {{ic|/etc/resolv.conf.head}} and {{ic|/etc/resolv.conf.tail}} files respectively:
+
[[dhcpcd]] has the ability to prepend or append nameservers to {{ic|/etc/resolv.conf}} by creating (or editing) the {{ic|/etc/resolv.conf.head}} and {{ic|/etc/resolv.conf.tail}} files respectively:
  
 
  echo "nameserver 127.0.0.1" > /etc/resolv.conf.head
 
  echo "nameserver 127.0.0.1" > /etc/resolv.conf.head
Line 33: Line 93:
 
==== dhclient ====
 
==== dhclient ====
  
For dhclient, uncomment in {{ic|/etc/dhclient.conf}}:
+
For {{Pkg|dhclient}}, uncomment in {{ic|/etc/dhclient.conf}}:
  
 
  prepend domain-name-servers 127.0.0.1;
 
  prepend domain-name-servers 127.0.0.1;
  
==== NetworkManager ====
+
=== NetworkManager ===
 +
[[NetworkManager]] has a plugin to enable DNS using dnsmasq. The advantages of this setup is that DNS lookups will be cached, shortening resolve times, and DNS lookups of VPN hosts will be routed to the relevant VPN's DNS servers (especially useful if you are connected to more than one VPN).
  
NetworkManager has the ability to start {{ic|dnsmasq}} from its configuration fileAdd the option {{ic|1=dns=dnsmasq}} to {{ic|NetworkManager.conf}} in the {{ic|[main]}} section then remove {{ic|dnsmasq}} from loading as a daemon:
+
Make sure {{Pkg|dnsmasq}} has been installed, but has been disabledThen, edit {{ic|/etc/NetworkManager/NetworkManager.conf}} and change the {{ic|dns}} in the {{ic|[main]}} section:
 
+
{{hc|/etc/NetworkManager/NetworkManager.conf|<nowiki>
{{hc|/etc/NetworkManager/NetworkManager.conf|<nowiki>
+
 
[main]
 
[main]
plugins=keyfile
+
...
 
dns=dnsmasq
 
dns=dnsmasq
 
</nowiki>}}
 
</nowiki>}}
  
For permanent caching add a config directory for {{ic|dnsmasq}} and set the cache number of nameservers (default: 150?):
+
Now restart NetworkManager or reboot.  NetworkManager will automatically start dnsmasq and add 127.0.0.1 to {{ic|/etc/resolv.conf}}.  The actual DNS servers can be found in {{ic|/run/NetworkManager/resolv.conf}}.  You can verify dnsmasq is being used by doing the same DNS lookup twice with {{ic|$ dig example.com}} that can be installed with {{Pkg|bind-tools}} and verifying the server and query times.
  
mkdir /etc/NetworkManager/dnsmasq.d
+
==== Custom configuration ====
echo "cache-size=1000" | sudo tee /etc/NetworkManager/dnsmasq.d/cache
+
Custom configurations can be created for ''dnsmasq'' by creating configuration files in {{ic|/etc/NetworkManager/dnsmasq.d/}}. For example, to change the size of the DNS cache (which is stored in RAM):
  
===== Other methods =====
+
{{hc|/etc/NetworkManager/dnsmasq.d/cache.conf|2=
 +
cache-size=1000
 +
}}
  
If using the dnsmasq daemon, then it is necessary to add the localhost address to {{ic|resolv.conf}} (which NetworkManager will be overriding).
+
==== IPv6 ====
  
Since the upgrade of [[NetworkManager]] to 0.7, Arch Linux now calls {{Pkg|dhcpcd}} directly instead of the common default with {{Pkg|dhclient}}. Because of the arguments set with {{Pkg|dhcpcd}}, it no longer sources the {{ic|/etc/resolv.conf.head}}, and {{ic|/etc/resolv.conf.tail}} settings for insertion of name servers.  Several options are available.
+
Enabling {{ic|dnsmasq}} in NetworkManager may break IPv6-only DNS lookups (i.e. {{ic|dig -6 [hostname]}}) which would otherwise work. In order to resolve this, creating the following file will configure ''dnsmasq'' to also listen to the IPv6 loopback:
  
The first option would be to add a script to the NetworkManager dispatcher to prepend localhost to {{ic|resolv.conf}}:
+
{{hc|/etc/NetworkManager/dnsmasq.d/ipv6_listen.conf|2=
 +
listen-address=::1
 +
}}
 +
In addition, {{ic|dnsmasq}} also does not prioritize upstream IPv6 DNS. Unfortunately NetworkManager does not do this ([https://bugs.launchpad.net/ubuntu/+source/network-manager/+bug/936712 Ubuntu Bug]). A workaround would be to disable IPv4 DNS in the NetworkManager config, assuming one exists
  
{{hc|/etc/NetworkManager/dispatcher.d/localhost-prepend|<nowiki>
+
==== Other methods ====
#!/bin/bash                                     
+
# Prepend localhost to resolv.conf for dnsmasq
+
  
if [[ ! $(grep 127.0.0.1 /etc/resolv.conf) ]]; then
+
Another option is in NetworkManagers' settings (usually by right-clicking the applet) and entering settings manually. Setting up will depending on the type of front-end used; the process usually involves right-clicking on the applet, editing (or creating) a profile, and then choosing DHCP type as 'Automatic (specify addresses).' The DNS addresses will need to be entered and are usually in this form: {{ic|127.0.0.1, DNS-server-one, ...}}.
  sed -i '1s|^|nameserver 127.0.0.1\n|' /etc/resolv.conf
+
fi</nowiki>}}
+
  
and make it executable:
+
=== Test ===
  
# chmod +x /etc/NetworkManager/dispatcher.d/localhost-prepend
+
To do a lookup speed test choose a website that has not been visited since dnsmasq has been started (''dig'' is part of the {{Pkg|bind-tools}} package):
  
The second option be to go into NetworkManagers' settings (usually by right-clicking the applet) and entering settings manually. Setting up will depending on the type of front-end used; the process usually involves right-clicking on the applet, editing (or creating) a profile, and then choosing DHCP type as 'Automatic (specify addresses).'  The DNS addresses will need to be entered and are usually in this form: {{ic|127.0.0.1, DNS-server-one, ...}}.
+
  $ dig archlinux.org | grep "Query time"
  
Lastly, NetworkManager with dhclient can be used ({{AUR|networkmanager-dhclient}}).
+
Running the command again will use the cached DNS IP and result in a faster lookup time if dnsmasq is setup correctly:
  
===== Custom Configuration =====
+
{{hc|<nowiki>$ dig archlinux.org | grep "Query time"</nowiki>|
 +
;; Query time: 18 msec
 +
}}
  
As of NetworkManager 0.9.6, custom configurations can be created for dnsmasq by creating configuration files in {{ic|/etc/NetworkManager/dnsmasq.d/}}
+
{{hc|<nowiki>$ dig archlinux.org | grep "Query time"</nowiki>|
 +
;; Query time: 2 msec
 +
}}
  
== DHCP Server Setup ==
+
== DHCP server setup ==
  
 
By default dnsmasq has the DHCP functionality turned off, if you want to use it you must turn it on in ({{ic|/etc/dnsmasq.conf}}).  Here are the important settings:
 
By default dnsmasq has the DHCP functionality turned off, if you want to use it you must turn it on in ({{ic|/etc/dnsmasq.conf}}).  Here are the important settings:
Line 103: Line 168:
 
</nowiki>}}
 
</nowiki>}}
  
== Start the Daemon ==
+
=== Test ===
To have dnsmasq to load upon startup:
+
  
{{bc|# systemctl enable dnsmasq}}
+
From a computer that is connected to the one with dnsmasq on it, configure it to use DHCP for automatic IP address assignment, then attempt to log into the network normally.
  
To stand dnsmasq immediately:
+
== TFTP server setup ==
  
{{bc|# systemctl start dnsmasq}}
+
Create a directory for TFTP root (e.g. {{ic|/srv/tftp}}) to put transferable files in.
  
To see if dnsmasq started properly, check the log; dnsmasq sends its messages to {{ic|/var/log/messages.log}}. The network will also need to be restarted so the the DHCP client can create a new {{ic|/etc/resolv.conf}}.
+
To use dnsmasq's TFTP secure mode [[chown]] TFTP root and all files in it to {{ic|dnsmasq}} user.
  
== Test ==
+
Enable TFTP in {{ic|dnsmasq.conf}}
=== DNS Caching ===
+
{{hc|/etc/dnsmasq.conf|<nowiki>
 +
enable-tftp
 +
tftp-root=/srv/tftp
 +
tftp-secure
 +
</nowiki>}}
  
To do a lookup speed test choose a website that has not been visited since dnsmasq has been started ({{ic|dig}} is part of the {{Pkg|dnsutils}} package):
+
== PXE setup ==
  
$ dig archlinux.org | grep "Query time"
+
PXE requires DHCP and TFTP servers, both functions can be provided by dnsmasq.
  
Running the command again will use the cached DNS IP and result in a faster lookup time if dnsmasq is setup correctly.
+
{{Tip|dnsmasq can add PXE booting options to a network with an already running DHCP server:
 +
{{hc|/etc/dnsmasq.conf|2=
 +
interface=''enp0s0''
 +
bind-dynamic
 +
dhcp-range=''192.168.0.1'',proxy}}
 +
}}
  
=== DHCP Server ===
+
# set up [[#TFTP server setup|TFTP server]] and [[#DHCP server setup|DHCP server]]
 +
# copy and configure a PXE compatible bootloader (e.g. [[Syslinux#Pxelinux|PXELINUX]]) on TFTP root
 +
# enable PXE in {{ic|/etc/dnsmasq.conf}}:
 +
{{Note|
 +
*file paths are relative to TFTP root
 +
*if the file has a {{ic|.0}} suffix, you must exclude the suffix in {{ic|pxe-service}} options
 +
}}
 +
To simply send one file:
 +
dhcp-boot=lpxelinux.0
 +
To send a file depending on client architecture:
 +
pxe-service=x86PC, "PXELINUX (BIOS)", "bios/lpxelinux"
 +
pxe-service=X86-64_EFI, "PXELINUX (EFI)", "efi64/syslinux.efi"
  
From a computer that is connected to the one with dnsmasq on it, configure it to use DHCP for automatic IP address assignment, then attempt to log into the network normally.
+
{{Note|In case {{ic|pxe-service}} does not work (especially for UEFI-based clients), combination of {{ic|dhcp-match}} and {{ic|dhcp-boot}} can be used. See [https://tools.ietf.org/html/rfc4578#section-2.1 RFC4578] for more {{ic|client-arch}} numbers for use with dhcp boot protocol.}}
 +
dhcp-match=set:efi-x86_64,option:client-arch,7
 +
dhcp-match=set:efi-x86_64,option:client-arch,9
 +
dhcp-match=set:efi-x86,option:client-arch,6
 +
dhcp-match=set:bios,option:client-arch,0
 +
dhcp-boot=tag:efi-x86_64,"efi64/syslinux.efi"
 +
dhcp-boot=tag:efi-x86,"efi32/syslinux.efi"
 +
dhcp-boot=tag:bios,"bios/lpxelinux.0"
 +
 
 +
The rest is up to the bootloader.
 +
 
 +
== Start the daemon ==
 +
 
 +
[[Start/enable]] {{ic|dnsmasq.service}}.
 +
 
 +
To see if dnsmasq started properly, check the system's journal:
 +
 
 +
{{bc|$ journalctl -u dnsmasq}}
 +
 
 +
The network will also need to be restarted so the DHCP client can create a new {{ic|/etc/resolv.conf}}.
  
 
== Tips and tricks ==
 
== Tips and tricks ==
  
=== Prevent OpenDNS Redirecting Google Queries ===
+
=== Prevent OpenDNS redirecting Google queries ===
  
 
To prevent OpenDNS from redirecting all Google queries to their own search server, add to {{ic|/etc/dnsmasq.conf}}:
 
To prevent OpenDNS from redirecting all Google queries to their own search server, add to {{ic|/etc/dnsmasq.conf}}:
Line 136: Line 239:
 
=== View leases ===
 
=== View leases ===
 
{{bc|$ cat /var/lib/misc/dnsmasq.leases}}
 
{{bc|$ cat /var/lib/misc/dnsmasq.leases}}
 +
 +
=== Adding a custom domain ===
 +
It is possible to add a custom domain to hosts in your (local) network:
 +
local=/home.lan/
 +
domain=home.lan
 +
 +
In this example it is possible to ping a host/device (e.g. defined in your hosts file) as {{ic|hostname.home.lan}}.
 +
 +
Uncomment {{ic|expand-hosts}} to add the custom domain to hosts entries:
 +
expand-hosts
 +
Without this setting, you will have to add the domain to entries of /etc/hosts.
 +
 +
=== Override addresses ===
 +
 +
In some cases, such as when operating a captive portal, it can be useful to resolve specific domains names to a hard-coded set of addresses. This is done with the {{ic|address}} config:
 +
 +
address=/example.com/1.2.3.4
 +
 +
Furthermore, it's possible to return a specific address for all domain names that are not answered from {{ic|/etc/hosts}} or DHCP by using a special wildcard:
 +
 +
address=/#/1.2.3.4
 +
 +
=== More than one instance ===
 +
 +
If we want two or more dnsmasq servers works per interface(s).
 +
 +
==== Static ====
 +
 +
To do this staticly, server per interface, use {{ic|interface}} and {{ic|bind-interface}} options. This enforce start second dnsmasq.
 +
 +
==== Dynamic ====
 +
 +
In this case we can exclude per interface and bind any others:
 +
 +
except-interface=lo
 +
bind-dynamic
 +
 +
{{Note|This is default in libvirt.}}
 +
 +
== See also ==
 +
 +
* [http://www.g-loaded.eu/2010/09/18/caching-nameserver-using-dnsmasq/ Caching Nameserver using dnsmasq, and a few other tips and tricks.]

Latest revision as of 18:05, 15 September 2016

dnsmasq provides services as a DNS cacher and a DHCP server. As a Domain Name Server (DNS) it can cache DNS queries to improve connection speeds to previously visited sites, and as a DHCP server dnsmasq can be used to provide internal IP addresses and routes to computers on a LAN. Either or both of these services can be implemented. dnsmasq is considered to be lightweight and easy to configure; it is designed for personal computer use or for use on a network with less than 50 computers. It also comes with a PXE server.

Installation

Install dnsmasq.

Configuration

To configure dnsmasq, you need to edit /etc/dnsmasq.conf. The file contains extensive comments explaining its options.

Warning: dnsmasq by default enables its DNS server. If you do not require it, you need to explicitly disable it by setting DNS port to 0:
/etc/dnsmasq.conf
port=0
Tip: To check configuration file(s) syntax, execute:
$ dnsmasq --test

DNS cache setup

To set up dnsmasq as a DNS caching daemon on a single computer edit /etc/dnsmasq.conf and uncomment the listen-address directive, adding in the localhost IP address:

listen-address=127.0.0.1

To use this computer to listen on its LAN IP address for other computers on the network:

listen-address=192.168.1.1    # Example IP

It is recommended that you use a static LAN IP in this case.

Multiple ip address settings:

listen-address=127.0.0.1,192.168.1.1 

DNS addresses file

Merge-arrows-2.pngThis article or section is a candidate for merging with resolv.conf.Merge-arrows-2.png

Notes: Same topic. Also note that most of this can also be done natively in /etc/resolvconf.conf using the name_servers and name_servers_append options. (Discuss in Talk:Dnsmasq#)

After configuring dnsmasq, the DHCP client will need to prepend the localhost address to the known DNS addresses in /etc/resolv.conf. This causes all queries to be sent to dnsmasq before trying to resolve them with an external DNS. After the DHCP client is configured, the network will need to be restarted for changes to take effect.

resolv.conf

One option is a pure resolv.conf configuration. To do this, just make the first nameserver in /etc/resolv.conf point to localhost:

/etc/resolv.conf
nameserver 127.0.0.1
# External nameservers
...

Now DNS queries will be resolved first with dnsmasq, only checking external servers if dnsmasq cannot resolve the query. dhcpcd, unfortunately, tends to overwrite /etc/resolv.conf by default, so if you use DHCP it is a good idea to protect /etc/resolv.conf. To do this, append nohook resolv.conf to the dhcpcd config file:

/etc/dhcpcd.conf
...
nohook resolv.conf

It is also possible to write protect your resolv.conf:

# chattr +i /etc/resolv.conf
More than three nameservers

A limitation in the way Linux handles DNS queries is that there can only be a maximum of three nameservers used in resolv.conf. As a workaround, you can make localhost the only nameserver in resolv.conf, and then create a separate resolv-file for your external nameservers. First, create a new resolv file for dnsmasq:

/etc/resolv.dnsmasq.conf
# Google's nameservers, for example
nameserver 8.8.8.8
nameserver 8.8.4.4

And then edit /etc/dnsmasq.conf to use your new resolv file:

/etc/dnsmasq.conf
...
resolv-file=/etc/resolv.dnsmasq.conf
...

dhcpcd

dhcpcd has the ability to prepend or append nameservers to /etc/resolv.conf by creating (or editing) the /etc/resolv.conf.head and /etc/resolv.conf.tail files respectively:

echo "nameserver 127.0.0.1" > /etc/resolv.conf.head

dhclient

For dhclient, uncomment in /etc/dhclient.conf:

prepend domain-name-servers 127.0.0.1;

NetworkManager

NetworkManager has a plugin to enable DNS using dnsmasq. The advantages of this setup is that DNS lookups will be cached, shortening resolve times, and DNS lookups of VPN hosts will be routed to the relevant VPN's DNS servers (especially useful if you are connected to more than one VPN).

Make sure dnsmasq has been installed, but has been disabled. Then, edit /etc/NetworkManager/NetworkManager.conf and change the dns in the [main] section:

/etc/NetworkManager/NetworkManager.conf
[main]
...
dns=dnsmasq

Now restart NetworkManager or reboot. NetworkManager will automatically start dnsmasq and add 127.0.0.1 to /etc/resolv.conf. The actual DNS servers can be found in /run/NetworkManager/resolv.conf. You can verify dnsmasq is being used by doing the same DNS lookup twice with $ dig example.com that can be installed with bind-tools and verifying the server and query times.

Custom configuration

Custom configurations can be created for dnsmasq by creating configuration files in /etc/NetworkManager/dnsmasq.d/. For example, to change the size of the DNS cache (which is stored in RAM):

/etc/NetworkManager/dnsmasq.d/cache.conf
cache-size=1000

IPv6

Enabling dnsmasq in NetworkManager may break IPv6-only DNS lookups (i.e. dig -6 [hostname]) which would otherwise work. In order to resolve this, creating the following file will configure dnsmasq to also listen to the IPv6 loopback:

/etc/NetworkManager/dnsmasq.d/ipv6_listen.conf
listen-address=::1

In addition, dnsmasq also does not prioritize upstream IPv6 DNS. Unfortunately NetworkManager does not do this (Ubuntu Bug). A workaround would be to disable IPv4 DNS in the NetworkManager config, assuming one exists

Other methods

Another option is in NetworkManagers' settings (usually by right-clicking the applet) and entering settings manually. Setting up will depending on the type of front-end used; the process usually involves right-clicking on the applet, editing (or creating) a profile, and then choosing DHCP type as 'Automatic (specify addresses).' The DNS addresses will need to be entered and are usually in this form: 127.0.0.1, DNS-server-one, ....

Test

To do a lookup speed test choose a website that has not been visited since dnsmasq has been started (dig is part of the bind-tools package):

$ dig archlinux.org | grep "Query time"

Running the command again will use the cached DNS IP and result in a faster lookup time if dnsmasq is setup correctly:

$ dig archlinux.org | grep "Query time"
;; Query time: 18 msec
$ dig archlinux.org | grep "Query time"
;; Query time: 2 msec

DHCP server setup

By default dnsmasq has the DHCP functionality turned off, if you want to use it you must turn it on in (/etc/dnsmasq.conf). Here are the important settings:

# Only listen to routers' LAN NIC.  Doing so opens up tcp/udp port 53 to
# localhost and udp port 67 to world:
interface=<LAN-NIC>

# dnsmasq will open tcp/udp port 53 and udp port 67 to world to help with
# dynamic interfaces (assigning dynamic ips). Dnsmasq will discard world
# requests to them, but the paranoid might like to close them and let the 
# kernel handle them:
bind-interfaces

# Dynamic range of IPs to make available to LAN pc
dhcp-range=192.168.111.50,192.168.111.100,12h

# If you’d like to have dnsmasq assign static IPs, bind the LAN computer's
# NIC MAC address:
dhcp-host=aa:bb:cc:dd:ee:ff,192.168.111.50

Test

From a computer that is connected to the one with dnsmasq on it, configure it to use DHCP for automatic IP address assignment, then attempt to log into the network normally.

TFTP server setup

Create a directory for TFTP root (e.g. /srv/tftp) to put transferable files in.

To use dnsmasq's TFTP secure mode chown TFTP root and all files in it to dnsmasq user.

Enable TFTP in dnsmasq.conf

/etc/dnsmasq.conf
enable-tftp
tftp-root=/srv/tftp
tftp-secure

PXE setup

PXE requires DHCP and TFTP servers, both functions can be provided by dnsmasq.

Tip: dnsmasq can add PXE booting options to a network with an already running DHCP server:
/etc/dnsmasq.conf
interface=enp0s0
bind-dynamic
dhcp-range=192.168.0.1,proxy
  1. set up TFTP server and DHCP server
  2. copy and configure a PXE compatible bootloader (e.g. PXELINUX) on TFTP root
  3. enable PXE in /etc/dnsmasq.conf:
Note:
  • file paths are relative to TFTP root
  • if the file has a .0 suffix, you must exclude the suffix in pxe-service options

To simply send one file:

dhcp-boot=lpxelinux.0

To send a file depending on client architecture:

pxe-service=x86PC, "PXELINUX (BIOS)", "bios/lpxelinux"
pxe-service=X86-64_EFI, "PXELINUX (EFI)", "efi64/syslinux.efi"
Note: In case pxe-service does not work (especially for UEFI-based clients), combination of dhcp-match and dhcp-boot can be used. See RFC4578 for more client-arch numbers for use with dhcp boot protocol.
dhcp-match=set:efi-x86_64,option:client-arch,7
dhcp-match=set:efi-x86_64,option:client-arch,9
dhcp-match=set:efi-x86,option:client-arch,6
dhcp-match=set:bios,option:client-arch,0
dhcp-boot=tag:efi-x86_64,"efi64/syslinux.efi"
dhcp-boot=tag:efi-x86,"efi32/syslinux.efi"
dhcp-boot=tag:bios,"bios/lpxelinux.0"

The rest is up to the bootloader.

Start the daemon

Start/enable dnsmasq.service.

To see if dnsmasq started properly, check the system's journal:

$ journalctl -u dnsmasq

The network will also need to be restarted so the DHCP client can create a new /etc/resolv.conf.

Tips and tricks

Prevent OpenDNS redirecting Google queries

To prevent OpenDNS from redirecting all Google queries to their own search server, add to /etc/dnsmasq.conf:

server=/www.google.com/<ISP DNS IP>

View leases

$ cat /var/lib/misc/dnsmasq.leases

Adding a custom domain

It is possible to add a custom domain to hosts in your (local) network:

local=/home.lan/
domain=home.lan

In this example it is possible to ping a host/device (e.g. defined in your hosts file) as hostname.home.lan.

Uncomment expand-hosts to add the custom domain to hosts entries:

expand-hosts

Without this setting, you will have to add the domain to entries of /etc/hosts.

Override addresses

In some cases, such as when operating a captive portal, it can be useful to resolve specific domains names to a hard-coded set of addresses. This is done with the address config:

address=/example.com/1.2.3.4

Furthermore, it's possible to return a specific address for all domain names that are not answered from /etc/hosts or DHCP by using a special wildcard:

address=/#/1.2.3.4

More than one instance

If we want two or more dnsmasq servers works per interface(s).

Static

To do this staticly, server per interface, use interface and bind-interface options. This enforce start second dnsmasq.

Dynamic

In this case we can exclude per interface and bind any others:

except-interface=lo
bind-dynamic
Note: This is default in libvirt.

See also