Difference between revisions of "Dnsmasq"

From ArchWiki
Jump to navigation Jump to search
(→‎PXE setup: Add RFC4578 source for dhcp boot client-arch numbering)
(Add IPv6 RA string)
 
(88 intermediate revisions by 13 users not shown)
Line 1: Line 1:
{{Lowercase_title}}
+
{{Lowercase title}}
 
[[Category:Domain Name System]]
 
[[Category:Domain Name System]]
 +
[[Category:DHCP]]
 
[[es:Dnsmasq]]
 
[[es:Dnsmasq]]
 
[[it:Dnsmasq]]
 
[[it:Dnsmasq]]
Line 6: Line 7:
 
[[pt:Dnsmasq]]
 
[[pt:Dnsmasq]]
 
[[ru:Dnsmasq]]
 
[[ru:Dnsmasq]]
[[zh-CN:Dnsmasq]]
+
[[zh-hans:Dnsmasq]]
 
+
{{Related articles start}}
[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.
+
{{Related|Domain name resolution}}
 +
{{Related articles end}}
 +
[http://www.thekelleys.org.uk/dnsmasq/doc.html dnsmasq] provides a [[Wikipedia:Name server|DNS server]], a [[Wikipedia:Dynamic Host Configuration Protocol|DHCP server]] with support for [[Wikipedia:DHCPv6|DHCPv6]] and [[Wikipedia:Preboot Execution Environment|PXE]], and a [[Wikipedia:Trivial File Transfer Protocol|TFTP server]]. It is designed to be lightweight and have a small footprint, suitable for resource constrained routers and firewalls. dnsmasq can also be configured to cache DNS queries for improved DNS lookup speeds to previously visited sites.
  
 
== Installation ==
 
== Installation ==
  
[[Install]] {{Pkg|dnsmasq}}.
+
[[Install]] the {{Pkg|dnsmasq}} package.
  
== Configuration ==
+
== Start the daemon ==
  
To configure dnsmasq, you need to edit {{ic|/etc/dnsmasq.conf}}. The file contains extensive comments explaining its options.  
+
[[Start/enable]] {{ic|dnsmasq.service}}.
  
{{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}}:
+
To see if dnsmasq started properly, check the system's journal:
{{hc|/etc/dnsmasq.conf|2=port=0}}
 
}}
 
  
{{Tip|To check configuration file(s) syntax, execute:
+
  $ journalctl -u dnsmasq.service
  $ dnsmasq --test
 
}}
 
  
== DNS cache setup ==
+
The network will also need to be restarted so the DHCP client can create a new {{ic|/etc/resolv.conf}}.
  
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:
+
== Configuration ==
  
listen-address=127.0.0.1
+
To configure dnsmasq, edit {{ic|/etc/dnsmasq.conf}}. The file contains comments explaining the options. For all available options see {{man|8|dnsmasq}}.
  
To use this computer to listen on its LAN IP address for other computers on the network:
+
{{Warning|dnsmasq's default configuration enables its DNS server. If you do not require it, you need to explicitly disable it by setting the DNS port to {{ic|0}}:
  
listen-address=192.168.1.1    # Example IP
+
{{bc|1=port=0}}
  
It is recommended that you use a static LAN IP in this case.
+
}}
  
Multiple ip address settings:
+
{{Tip|To check configuration file(s) syntax, execute:
  
  listen-address=127.0.0.1,192.168.1.1
+
  $ dnsmasq --test
 
 
=== 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:
+
=== DNS server ===
  
{{hc|/etc/dhcpcd.conf|
+
To set up dnsmasq as a DNS caching daemon on a single computer specify a {{ic|listen-address}} directive, adding in the localhost IP address:
...
 
nohook resolv.conf}}
 
  
It is also possible to write protect your resolv.conf:
+
listen-address=::1,127.0.0.1
  
# chattr +i /etc/resolv.conf
+
To use this computer to listen on its LAN IP address for other computers on the network. It is recommended that you use a static LAN IP in this case. E.g.:
  
===== More than three nameservers =====
+
listen-address=::1,127.0.0.1,192.168.1.1
  
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:
+
Set the number of cached domain names with {{ic|1=cache-size=''size''}} (the default is {{ic|150}}):
  
{{hc|/etc/resolv.dnsmasq.conf|
+
cache-size=1000
# Google's nameservers, for example
 
nameserver 8.8.8.8
 
nameserver 8.8.4.4
 
}}
 
  
And then edit {{ic|/etc/dnsmasq.conf}} to use your new resolv file:
+
To validate [[DNSSEC]] load the DNSSEC trust anchors provided by the {{Pkg|dnsmasq}} package  and set the option {{ic|dnssec}}:
  
{{hc|/etc/dnsmasq.conf|
+
conf-file=/usr/share/dnsmasq/trust-anchors.conf
...
+
dnssec
resolv-file=/etc/resolv.dnsmasq.conf
 
...
 
}}
 
  
==== dhcpcd ====
+
See {{man|8|dnsmasq}} for more options you might want to use.
  
[[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:
+
==== DNS addresses file and forwarding ====
  
echo "nameserver 127.0.0.1" > /etc/resolv.conf.head
+
After configuring dnsmasq, you need to add the localhost addresses as the only nameservers in {{ic|/etc/resolv.conf}}. This causes all queries to be sent to dnsmasq.
  
==== dhclient ====
+
Since dnsmasq is a stub resolver not a recursive resolver you must set up forwarding to an external DNS server. This can be done automatically by using [[openresolv]] or by manually specifying the DNS server address in dnsmasq's configuration.
  
For {{Pkg|dhclient}}, uncomment in {{ic|/etc/dhclient.conf}}:
+
===== openresolv =====
  
prepend domain-name-servers 127.0.0.1;
+
If your network manager supports ''resolvconf'', instead of directly altering {{ic|/etc/resolv.conf}}, you can use [[openresolv]] to generate configuration files for dnsmasq. [https://roy.marples.name/projects/openresolv/config]
  
=== NetworkManager ===
+
Edit {{ic|/etc/resolvconf.conf}} and add the loopback addresses as name servers, and configure openresolv to write out dnsmasq configuration:
DNS requests can be sped up by caching previous requests locally for subsequent lookup. [[NetworkManager]] has a plugin to enable DNS caching using dnsmasq, but it is not enabled in the default configuration.
 
  
Make sure {{Pkg|dnsmasq}} has been installed, but has been disabled.  Then, edit {{ic|/etc/NetworkManager/NetworkManager.conf}} and change the {{ic|dns}} in the {{ic|[main]}} section:
+
{{hc|/etc/resolvconf.conf|2=
{{hc|/etc/NetworkManager/NetworkManager.conf|<nowiki>
+
# Use the local name server
[main]
+
name_servers="::1 127.0.0.1"
...
 
dns=dnsmasq
 
</nowiki>}}
 
  
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/dnsmasq.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.
+
# Write out dnsmasq extended configuration and resolv files
 +
dnsmasq_conf=/etc/dnsmasq-openresolv.conf
 +
dnsmasq_resolv=/etc/dnsmasq-resolv.conf
 +
}}
  
==== Usage with libvirt ====
+
Run {{ic|resolvconf -u}} so that the configuration files get created. If the files do not exist {{ic|dnsmasq.service}} will fail to start.
  
Network-manager think if there is one running libvirt that he run this before. To fix conflicts between other dnsmasq, eg: used in [[libvirt]], you must run it externally.
+
Edit dnsmasq's configuration file to use openresolv's generated configuration:
  
We do '''not''' want change our resolv.conf automaticly.
+
# Read configuration generated by openresolv
 +
conf-file=/etc/dnsmasq-openresolv.conf
 +
resolv-file=/etc/dnsmasq-resolv.conf
  
{{hc|/etc/NetworkManager/NetworkManager.conf|<nowiki>
+
===== Manual forwarding =====
[main]
 
...
 
dns=none
 
</nowiki>}}
 
  
We put it manually here.
+
First you must set localhost addresses as the only nameservers in {{ic|/etc/resolv.conf}}:
  
{{hc|/etc/resolv.conf.head|2=
+
{{hc|/etc/resolv.conf|
 +
nameserver ::1
 
nameserver 127.0.0.1
 
nameserver 127.0.0.1
 
}}
 
}}
  
The interface to bind and bind it even if there is second dnsmasq runned on computer.
+
Make sure to protect {{ic|/etc/resolv.conf}} from modification as described in [[Domain name resolution#Overwriting of /etc/resolv.conf]].
  
{{hc|/etc/NetworkManager/dnsmasq.d/bind-interface.conf|2=
+
The upstream DNS server addresses must then be specified in dnsmasq's configuration file as {{ic|1=server=''server_address''}}. Also add {{ic|no-resolv}} so dnsmasq does not needlessly read {{ic|/etc/resolv.conf}} which only contains the localhost addresses of itself.
interface=lo
 
bind-interface
 
}}
 
  
This start service if interface is up. This service can start only once before stop which will be initiate by systemd on restart/shutdown.
+
{{bc|1=
 +
no-resolv
  
{{hc|/etc/NetworkManager/dispatcher.d/10_dnsmasq|2=
+
# Google's nameservers, for example
#!/bin/sh
+
server=8.8.8.8
if [ -n "$2" ] && [ "$2" = "up" ]; then # $INTERFACE is up
+
server=8.8.4.4
systemctl start NetworkManager-dnsmasq.service
 
fi
 
 
}}
 
}}
  
Systemd service.
+
Now DNS queries will be resolved with dnsmasq, only checking external servers if it cannot answer the query from its cache.
  
{{hc|/etc/systemd/system/NetworkManager-dnsmasq.service|2=
+
==== Adding a custom domain ====
[Unit]
 
Description=A lightweight DHCP and caching DNS server
 
After=network.target
 
Documentation=man:dnsmasq(8)
 
  
[Service]
+
It is possible to add a custom domain to hosts in your (local) network:
Type=dbus
 
BusName=uk.org.thekelleys.dnsmasq
 
ExecStartPre=/usr/bin/dnsmasq --test
 
ExecStart=/usr/bin/dnsmasq -k --enable-dbus --user=dnsmasq --pid-file --conf-dir=/etc/NetworkManager/dnsmasq.d/
 
ExecReload=/bin/kill -HUP $MAINPID
 
}}
 
  
==== Custom configuration ====
+
local=/lan/
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):
+
domain=lan
  
{{hc|/etc/NetworkManager/dnsmasq.d/cache.conf|2=
+
In this example it is possible to ping a host/device (e.g. defined in your {{ic|/etc/hosts}} file) as {{ic|''hostname''.lan}}.
cache-size=1000
 
}}
 
  
==== IPv6 ====
+
Uncomment {{ic|expand-hosts}} to add the custom domain to hosts entries:
  
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:
+
expand-hosts
 
 
{{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
 
  
==== Other methods ====
+
Without this setting, you will have to add the domain to entries of {{ic|/etc/hosts}}.
  
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, ...}}.
+
==== Test ====
  
=== Test ===
+
To do a lookup speed test choose a website that has not been visited since dnsmasq has been started (''drill'' is part of the {{Pkg|ldns}} package):
  
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):
+
  $ drill archlinux.org | grep "Query time"
 
 
  $ 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:
 
Running the command again will use the cached DNS IP and result in a faster lookup time if dnsmasq is setup correctly:
  
{{hc|<nowiki>$ dig archlinux.org | grep "Query time"</nowiki>|
+
{{hc|$ drill archlinux.org {{!}} grep "Query time"|
 
;; Query time: 18 msec
 
;; Query time: 18 msec
 
}}
 
}}
  
{{hc|<nowiki>$ dig archlinux.org | grep "Query time"</nowiki>|
+
{{hc|$ drill archlinux.org {{!}} grep "Query time"|
 
;; Query time: 2 msec
 
;; Query time: 2 msec
 
}}
 
}}
  
== DHCP server setup ==
+
To test if DNSSEC validation is working see [[DNSSEC#Testing]].
  
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:
+
=== DHCP server ===
 +
 
 +
{{Expansion|Add instructions for IPv6}}
 +
 
 +
By default dnsmasq has the DHCP functionality turned off, if you want to use it you must turn it on.  Here are the important settings:
  
 
{{bc|<nowiki>
 
{{bc|<nowiki>
Line 211: Line 166:
 
bind-interfaces
 
bind-interfaces
  
# Dynamic range of IPs to make available to LAN pc
+
# Optionally set a domain name
 +
domain=example.com
 +
 
 +
# Set default gateway
 +
dhcp-option=3,0.0.0.0
 +
 
 +
# Set DNS servers to announce
 +
dhcp-option=6,0.0.0.0
 +
 
 +
# If your dnsmasq server is also doing the routing for your network,
 +
# you can use option 121 to push a static route out.
 +
# x.x.x.x is the destination LAN, yy is the CIDR notation (usually /24),
 +
# and z.z.z.z is the host which will do the routing.
 +
dhcp-option=121,x.x.x.x/yy,z.z.z.z
 +
 
 +
# Dynamic range of IPs to make available to LAN PC and the lease time.
 +
# Ideally set the lease time to 5m only at first to test everything works okay before you set long-lasting records.
 
dhcp-range=192.168.111.50,192.168.111.100,12h
 
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
+
# Provide IPv6 DHCP leases through Router Advertisements (RAs) for aaaa:bbbb:cccc:dddd::/64 subnet
# NIC MAC address:
+
dhcp-range=aaaa:bbbb:cccc:dddd::,ra-only,infinite
 +
 
 +
# If you’d like to have dnsmasq assign static IPs to some clients, bind the LAN computers
 +
# NIC MAC addresses:
 
dhcp-host=aa:bb:cc:dd:ee:ff,192.168.111.50
 
dhcp-host=aa:bb:cc:dd:ee:ff,192.168.111.50
 +
dhcp-host=aa:bb:cc:ff:dd:ee,192.168.111.51
 
</nowiki>}}
 
</nowiki>}}
  
=== Test ===
+
See {{man|8|dnsmasq}} for more options.
 +
 
 +
==== 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.
 
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 ==
+
If you inspect the {{ic|/var/lib/misc/dnsmasq.leases}} file on the server, you should be able to see the lease.
  
Create a directory for TFTP root (e.g. {{ic|/srv/tftp}}) to put transferable files in.
+
=== TFTP server ===
  
To use dnsmasq's TFTP secure mode [[chown]] TFTP root and all files in it to {{ic|dnsmasq}} user.
+
dnsmasq has built-in [[TFTP]] server.
  
Enable TFTP in {{ic|dnsmasq.conf}}
+
To use it, create a directory for TFTP root (e.g. {{ic|/srv/tftp}}) to put transferable files in.
{{hc|/etc/dnsmasq.conf|<nowiki>
+
 
enable-tftp
+
For increased security it is advised to use dnsmasq's TFTP secure mode. In secure mode only files owned by the {{ic|dnsmasq}} user will be served over TFTP. You will need to [[chown]] TFTP root and all files in it to {{ic|dnsmasq}} user to use this feature.
tftp-root=/srv/tftp
+
 
tftp-secure
+
Enable TFTP:
</nowiki>}}
+
 
 +
enable-tftp
 +
tftp-root=/srv/tftp
 +
tftp-secure
 +
 
 +
See {{man|8|dnsmasq}} for more options.
  
== PXE setup ==
+
=== PXE server ===
  
 
PXE requires DHCP and TFTP servers, both functions can be provided by dnsmasq.
 
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:
+
{{Tip|dnsmasq can run in "proxy-DHCP" mode and add PXE booting options to a network with an already running DHCP server:
{{hc|/etc/dnsmasq.conf|2=
+
 
 +
{{bc|1=
 
interface=''enp0s0''
 
interface=''enp0s0''
 
bind-dynamic
 
bind-dynamic
dhcp-range=''192.168.0.1'',proxy}}
+
dhcp-range=''192.168.0.1'',proxy
 
}}
 
}}
  
# 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
+
 
 +
# set up [[#TFTP server]] and [[#DHCP server]]
 +
# copy and configure a PXE compatible bootloader (e.g. [[PXELINUX]]) on TFTP root
 
# enable PXE in {{ic|/etc/dnsmasq.conf}}:
 
# enable PXE in {{ic|/etc/dnsmasq.conf}}:
 +
 
{{Note|
 
{{Note|
 
*file paths are relative to TFTP root
 
*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
 
*if the file has a {{ic|.0}} suffix, you must exclude the suffix in {{ic|pxe-service}} options
 
}}
 
}}
 +
 
To simply send one file:
 
To simply send one file:
 +
 
  dhcp-boot=lpxelinux.0
 
  dhcp-boot=lpxelinux.0
 +
 
To send a file depending on client architecture:
 
To send a file depending on client architecture:
 +
 
  pxe-service=x86PC, "PXELINUX (BIOS)", "bios/lpxelinux"
 
  pxe-service=x86PC, "PXELINUX (BIOS)", "bios/lpxelinux"
 
  pxe-service=X86-64_EFI, "PXELINUX (EFI)", "efi64/syslinux.efi"
 
  pxe-service=X86-64_EFI, "PXELINUX (EFI)", "efi64/syslinux.efi"
  
{{Note|In case {{ic|pxe-service}} doesn't 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.}}
+
{{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,7
 
  dhcp-match=set:efi-x86_64,option:client-arch,9
 
  dhcp-match=set:efi-x86_64,option:client-arch,9
Line 269: Line 260:
 
  dhcp-boot=tag:bios,"bios/lpxelinux.0"
 
  dhcp-boot=tag:bios,"bios/lpxelinux.0"
  
 +
See {{man|8|dnsmasq}} for more options.
  
 
+
The rest is up to the [[bootloader]].
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 ==
Line 289: Line 270:
 
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}}:
 
{{bc|1=server=/www.google.com/<ISP DNS IP>}}
 
{{bc|1=server=/www.google.com/<ISP DNS IP>}}
 
=== View 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'll have to add the domain to entries of /etc/hosts.
 
  
 
=== Override addresses ===
 
=== Override addresses ===
Line 329: Line 296:
 
  bind-dynamic
 
  bind-dynamic
  
{{Note|This is default in libvirt.}}
+
{{Note|This is default in [[libvirt]].}}
 +
 
 +
=== Domain blacklisting ===
 +
 
 +
To blacklist domains, i.e. answer queries for them with NXDOMAIN, use the {{ic|address}} option without specifying the IP address:
 +
 
 +
address=/blacklisted.example/
 +
address=/another.blacklisted.example/
 +
 
 +
For ease of use place the blacklist in a separate file, e.g. {{ic|/etc/dnsmasq.d/blacklist.conf}} and load it from {{ic|/etc/dnsmasq.conf}} with {{ic|1=conf-file=/etc/dnsmasq.d/blacklist.conf}} or {{ic|1=conf-dir=conf-dir=/etc/dnsmasq.d/,*.conf}}.
 +
 
 +
{{Tip|A list of potential sources for the blacklist can be found in [https://github.com/openwrt/packages/blob/master/net/adblock/files/README.md OpenWrt's adblock package's README].}}
  
 
== See also ==
 
== 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.]
 
* [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:53, 27 October 2019

dnsmasq provides a DNS server, a DHCP server with support for DHCPv6 and PXE, and a TFTP server. It is designed to be lightweight and have a small footprint, suitable for resource constrained routers and firewalls. dnsmasq can also be configured to cache DNS queries for improved DNS lookup speeds to previously visited sites.

Installation

Install the dnsmasq package.

Start the daemon

Start/enable dnsmasq.service.

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

$ journalctl -u dnsmasq.service

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

Configuration

To configure dnsmasq, edit /etc/dnsmasq.conf. The file contains comments explaining the options. For all available options see dnsmasq(8).

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

DNS server

To set up dnsmasq as a DNS caching daemon on a single computer specify a listen-address directive, adding in the localhost IP address:

listen-address=::1,127.0.0.1

To use this computer to listen on its LAN IP address for other computers on the network. It is recommended that you use a static LAN IP in this case. E.g.:

listen-address=::1,127.0.0.1,192.168.1.1

Set the number of cached domain names with cache-size=size (the default is 150):

cache-size=1000

To validate DNSSEC load the DNSSEC trust anchors provided by the dnsmasq package and set the option dnssec:

conf-file=/usr/share/dnsmasq/trust-anchors.conf
dnssec

See dnsmasq(8) for more options you might want to use.

DNS addresses file and forwarding

After configuring dnsmasq, you need to add the localhost addresses as the only nameservers in /etc/resolv.conf. This causes all queries to be sent to dnsmasq.

Since dnsmasq is a stub resolver not a recursive resolver you must set up forwarding to an external DNS server. This can be done automatically by using openresolv or by manually specifying the DNS server address in dnsmasq's configuration.

openresolv

If your network manager supports resolvconf, instead of directly altering /etc/resolv.conf, you can use openresolv to generate configuration files for dnsmasq. [1]

Edit /etc/resolvconf.conf and add the loopback addresses as name servers, and configure openresolv to write out dnsmasq configuration:

/etc/resolvconf.conf
# Use the local name server
name_servers="::1 127.0.0.1"

# Write out dnsmasq extended configuration and resolv files
dnsmasq_conf=/etc/dnsmasq-openresolv.conf
dnsmasq_resolv=/etc/dnsmasq-resolv.conf

Run resolvconf -u so that the configuration files get created. If the files do not exist dnsmasq.service will fail to start.

Edit dnsmasq's configuration file to use openresolv's generated configuration:

# Read configuration generated by openresolv
conf-file=/etc/dnsmasq-openresolv.conf
resolv-file=/etc/dnsmasq-resolv.conf
Manual forwarding

First you must set localhost addresses as the only nameservers in /etc/resolv.conf:

/etc/resolv.conf
nameserver ::1
nameserver 127.0.0.1

Make sure to protect /etc/resolv.conf from modification as described in Domain name resolution#Overwriting of /etc/resolv.conf.

The upstream DNS server addresses must then be specified in dnsmasq's configuration file as server=server_address. Also add no-resolv so dnsmasq does not needlessly read /etc/resolv.conf which only contains the localhost addresses of itself.

no-resolv

# Google's nameservers, for example
server=8.8.8.8
server=8.8.4.4

Now DNS queries will be resolved with dnsmasq, only checking external servers if it cannot answer the query from its cache.

Adding a custom domain

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

local=/lan/
domain=lan

In this example it is possible to ping a host/device (e.g. defined in your /etc/hosts file) as hostname.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.

Test

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

$ drill 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:

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

To test if DNSSEC validation is working see DNSSEC#Testing.

DHCP server

Tango-view-fullscreen.pngThis article or section needs expansion.Tango-view-fullscreen.png

Reason: Add instructions for IPv6 (Discuss in Talk:Dnsmasq#)

By default dnsmasq has the DHCP functionality turned off, if you want to use it you must turn it on. 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

# Optionally set a domain name
domain=example.com

# Set default gateway
dhcp-option=3,0.0.0.0

# Set DNS servers to announce
dhcp-option=6,0.0.0.0

# If your dnsmasq server is also doing the routing for your network,
# you can use option 121 to push a static route out.
# x.x.x.x is the destination LAN, yy is the CIDR notation (usually /24), 
# and z.z.z.z is the host which will do the routing.
dhcp-option=121,x.x.x.x/yy,z.z.z.z

# Dynamic range of IPs to make available to LAN PC and the lease time. 
# Ideally set the lease time to 5m only at first to test everything works okay before you set long-lasting records.
dhcp-range=192.168.111.50,192.168.111.100,12h

# Provide IPv6 DHCP leases through Router Advertisements (RAs) for aaaa:bbbb:cccc:dddd::/64 subnet
dhcp-range=aaaa:bbbb:cccc:dddd::,ra-only,infinite

# If you’d like to have dnsmasq assign static IPs to some clients, bind the LAN computers
# NIC MAC addresses:
dhcp-host=aa:bb:cc:dd:ee:ff,192.168.111.50
dhcp-host=aa:bb:cc:ff:dd:ee,192.168.111.51

See dnsmasq(8) for more options.

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.

If you inspect the /var/lib/misc/dnsmasq.leases file on the server, you should be able to see the lease.

TFTP server

dnsmasq has built-in TFTP server.

To use it, create a directory for TFTP root (e.g. /srv/tftp) to put transferable files in.

For increased security it is advised to use dnsmasq's TFTP secure mode. In secure mode only files owned by the dnsmasq user will be served over TFTP. You will need to chown TFTP root and all files in it to dnsmasq user to use this feature.

Enable TFTP:

enable-tftp
tftp-root=/srv/tftp
tftp-secure

See dnsmasq(8) for more options.

PXE server

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

Tip: dnsmasq can run in "proxy-DHCP" mode and add PXE booting options to a network with an already running DHCP server:
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"

See dnsmasq(8) for more options.

The rest is up to the bootloader.

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>

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.

Domain blacklisting

To blacklist domains, i.e. answer queries for them with NXDOMAIN, use the address option without specifying the IP address:

address=/blacklisted.example/
address=/another.blacklisted.example/

For ease of use place the blacklist in a separate file, e.g. /etc/dnsmasq.d/blacklist.conf and load it from /etc/dnsmasq.conf with conf-file=/etc/dnsmasq.d/blacklist.conf or conf-dir=conf-dir=/etc/dnsmasq.d/,*.conf.

Tip: A list of potential sources for the blacklist can be found in OpenWrt's adblock package's README.

See also