Difference between revisions of "Dnsmasq"

From ArchWiki
Jump to: navigation, search
m (Fix a small typo)
(DNS Addresses File: Adds section about resolv.conf-only configuration)
(41 intermediate revisions by 22 users not shown)
Line 1: Line 1:
[[Category:Networking (English)]]
+
[[Category:Domain Name System]]
{{i18n_links_start}}
+
[[it:Dnsmasq]]
{{i18n_entry|English|Dnsmasq}}
+
[[ru:Dnsmasq]]
{{i18n_entry|Русский|Dnsmasq_(Русский)}}
+
[[zh-CN:Dnsmasq]]
{{i18n_entry|简体中文|Dnsmasq_(简体中文)}}
+
{{Lowercase_title}}
{{i18n_links_end}}
+
  
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, {{Package Official|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, and is designed for personal computer use or for use on a network with less that 50 computers. This piece of software will certainly speed up your web navigation.
+
'''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.
  
 
== Installing ==
 
== Installing ==
  
Dnsmasq is in the official repositories and can be installed by:
+
[[pacman|Install]] {{Pkg|dnsmasq}} from the [[Official Repositories|official repositories]].
  
pacman -S dnsmasq
+
== DNS Cache Setup ==
  
== DHCP Server Setup ==
+
To set up dnsmasq as a DNS caching daemon on a single computer edit {{ic|/etc/dnsmasq.conf}} and uncomment the localhost listening address:
  
The Dnsmasq configuration file do not need to be configured ({{Filename|/etc/dnsmasq.conf}}) for the daemon to work. The config file is very well documented and verbose.
+
listen-address=127.0.0.1
  
== DNS Cache Setup ==
+
To use this computer to act as a default DNS specify the fixed IP address of the network:
  
If you set up Dnsmasq as a DHCP server, it is already setup to record DNS queries and relay them to an internal network. To set up dnsmasq as a DNS caching daemon on a personal PC, edit {{Filename|/etc/dnsmasq.conf}} and add the listen address:
+
  listen-address=<192.168.1.1>  # Example IP
  
listen-address=127.0.0.1
+
=== DNS Addresses File ===
  
After you have configured Dnsmasq, you will need to tell your DHCP client to pre-pend the localhost address to the known DNS addresses file ({{Filename|/etc/resolv.conf}}).  This sends all queries to dnsmasq first before trying to resolve them to an external DNS server.  After your DHCP client is configured, you will need to restart the network for changes to take effect.
+
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.
  
=== dhcpcd ===
+
==== resolv.conf ====
  
{{Package Official|dhcpcd}} has the ability to prepend or append nameservers to {{Filename|/etc/resolv.conf}} by creating (or editing) the {{Filename|/etc/resolv.conf.head}} and {{Filename|/etc/resolv.conf.tail}} files respectively:
+
One option is a pure {{ic|resolv.conf}} configuration. To do this, just make the first nameserver in {{ic|/etc/resolv.conf}} point 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}}
 +
 
 +
===== More than three nameservers =====
 +
 
 +
A limitation 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:
 +
 
 +
{{hc|/etc/resolv.dnsmasq.conf|
 +
# Google's nameservers, for example
 +
nameserver 8.8.8.8
 +
nameserver 8.8.8.4
 +
}}
 +
 
 +
And then edit {{ic|/etc/dnsmasq.conf}} to use your new resolv file:
 +
 
 +
{{hc|/etc/dnsmasq.conf|
 +
...
 +
resolv-file&#61;/etc/resolv.dnsmasq.conf
 +
...
 +
}}
 +
 
 +
==== 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:
  
 
  echo "nameserver 127.0.0.1" > /etc/resolv.conf.head
 
  echo "nameserver 127.0.0.1" > /etc/resolv.conf.head
  
=== dhclient ===
+
==== dhclient ====
  
If you use dhclient, you will need to add to (or create) to {{Filename|/etc/dhclient.conf}}:
+
For 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 ====
  
Since the upgrade of [[NetworkManager]] to 0.7, Arch Linux now calls {{Package Official|dhcpcd}} directly instead of the common default with {{Package Official|dhclient}}.  Because of the arguments set with {{Package Official|dhcpcd}}, it no longer sources the {{Filename|/etc/resolv.conf.head}}, and {{Filename|/etc/resolv.conf.tail}} settings for insertion of name servers.  There are three workarounds to fix this:
+
NetworkManager has the ability to start {{ic|dnsmasq}} from its configuration file.  Add the option {{ic|1=dns=dnsmasq}} to {{ic|NetworkManager.conf}} in the {{ic|[main]}} section then disable {{ic|dnsmasq}} from loading as a daemon:
  
The first would be to use NetworkManager with dhclient which can be found in {{Package AUR|networkmanager-dhclient|here}}.
+
{{hc|/etc/NetworkManager/NetworkManager.conf|<nowiki>
 +
[main]
 +
plugins=keyfile
 +
dns=dnsmasq
 +
</nowiki>}}
  
The second workaround would be to go into NetworkManagers' settings (usually by right-clicking the applet) and entering your settings manually.  Depending on the type of front-end you use for NetworkManager, 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 ddresses are usually entered in such form: {{Codeline|127.0.0.1, DNS-server-one, ...}}.
+
For permanent caching add a config directory for {{ic|dnsmasq}} and set the cache number of nameservers (default: 150?):
  
The third workaround is to put a script like this in /etc/Networkmanager/dispatcher.d/ and don't forget to make it executable:
+
mkdir /etc/NetworkManager/dnsmasq.d
 +
echo "cache-size=1000" | sudo tee /etc/NetworkManager/dnsmasq.d/cache
  
#!/bin/bash
+
===== Other methods =====
#
+
# Override /etc/resolv.conf and tell
+
# NetworkManagerDispatcher to go pluck itself.
+
#
+
# scripts in the /etc/NetworkManager/dispatcher.d/ directory
+
# are called alphabetically and are passed two parameters:
+
# $1 is the interface name, and $2 is "up" or "down" as the
+
# case may be.
+
#
+
# Here, no matter what interface or state, override the
+
# created resolver config with my config.
+
#
+
cp -f /etc/resolv.conf.myDNSoverride /etc/resolv.conf
+
  
Then create a file with the nameservers (in this case opendns ones), according to what you specified on the script (/etc/resolv.conf.myDNSoverride):
+
If using the dnsmasq daemon, then it is necessary to add the localhost address to {{ic|resolv.conf}} (which NetworkManager will be overriding).
  
  nameserver 208.67.222.222
+
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.
nameserver 208.67.220.220
+
  
Of course you'll have to start the daemon networkmanager-dispatcher.
+
The first option would be to add a script to the NetworkManager dispatcher to prepend localhost to {{ic|resolv.conf}}:
  
Alternatively, if you want to keep your current resolv.conf file, use a script similar to
+
{{hc|/etc/NetworkManager/dispatcher.d/localhost-prepend|<nowiki>
+
#!/bin/bash                                      
#!/bin/bash
+
# Prepend localhost to resolv.conf for dnsmasq
#
+
# Creates a copy of resolv.conf with "nameserver 127.0.0.1" as first line. 
+
cat - /etc/resolv.conf <<<"nameserver 127.0.0.1"  > /etc/resolv.conf.new
+
cp -f /etc/resolv.conf.new /etc/resolv.conf
+
  
==Start the Daemon==
+
if [[ ! $(grep 127.0.0.1 /etc/resolv.conf) ]]; then
 +
  sed -i '1s|^|nameserver 127.0.0.1\n|' /etc/resolv.conf
 +
fi</nowiki>}}
  
Dnsmasq runs as a daemon. But before we start it, let's do a quick check of what our current speed for resolving is by issuing this command (dig is part of the {{Package Official|dnsutils}} package) :
+
and make it executable:
  
  $ dig archlinux.org | grep Query
+
  # chmod +x /etc/NetworkManager/dispatcher.d/localhost-prepend
  
Now let's start it :
+
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, ...}}.
  
# /etc/rc.d/dnsmasq start
+
Lastly, NetworkManager with dhclient can be used ({{AUR|networkmanager-dhclient}}).
  
To have dnsmasq to load upon startup, add dnsmasq to your daemons array in {{Filename|/etc/rc.conf}}:
+
===== Custom Configuration =====
  
DAEMONS=(network dnsmasq ...)
+
As of NetworkManager 0.9.6, custom configurations can be created for dnsmasq by creating configuration files in {{ic|/etc/NetworkManager/dnsmasq.d/}}
  
To see if dnsmasq started properly, check the log; dnsmasq sends its messages to {{Filename|/var/log/messages.log}}.
+
== DHCP Server Setup ==
You will also need to restart the network so that dhcpd can recreate {{Filename|/etc/resolv.conf}}.
+
  
# /etc/rc.d/network restart
+
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:
  
Now we will test our DNS lookup and measure the time response :
+
{{bc|<nowiki>
 +
# 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>
  
dig archlinux.org | grep "Query time"
+
# 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
  
The Query time should have decreased.
+
# Dynamic range of IPs to make available to LAN pc
Also if you remove the grep, you can see the server used (the line under Query time), and now it should be localhost aka 127.0.0.1.
+
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
 +
</nowiki>}}
  
== Test DHCP Server ==
+
== Start the daemon ==
 +
To have dnsmasq to load upon startup:
  
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 as you normally would.
+
{{bc|# systemctl enable dnsmasq}}
  
== Tips ==
+
To start dnsmasq immediately:
  
=== Prevent OpenDNS Redirecting Google Queries ===
+
{{bc|# systemctl start dnsmasq}}
  
To prevent OpenDNS from redirecting all Google queries to their own search server, add to {{Filename|/etc/dnsmasq.conf}}:
+
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}}.
 +
 
 +
== Test ==
 +
=== DNS Caching ===
 +
 
 +
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):
 +
 
 +
$ 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.
 +
 
 +
=== DHCP Server ===
 +
 
 +
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.
 +
 
 +
== Tips and tricks ==
 +
 
 +
=== Prevent OpenDNS Redirecting Google Queries ===
  
server=/www.google.com/X.X.X.X
+
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>}}
  
Replace X.X.X.X with your ISP's DNS server/Router IP.
+
=== View leases ===
 +
{{bc|$ cat /var/lib/misc/dnsmasq.leases}}

Revision as of 22:23, 17 July 2013

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, 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.

Installing

Install dnsmasq from the official repositories.

DNS Cache Setup

To set up dnsmasq as a DNS caching daemon on a single computer edit /etc/dnsmasq.conf and uncomment the localhost listening address:

listen-address=127.0.0.1

To use this computer to act as a default DNS specify the fixed IP address of the network:

listen-address=<192.168.1.1>  # Example IP

DNS Addresses File

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 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
More than three nameservers

A limitation 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.8.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 the ability to start dnsmasq from its configuration file. Add the option dns=dnsmasq to NetworkManager.conf in the [main] section then disable dnsmasq from loading as a daemon:

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

For permanent caching add a config directory for dnsmasq and set the cache number of nameservers (default: 150?):

mkdir /etc/NetworkManager/dnsmasq.d
echo "cache-size=1000" | sudo tee /etc/NetworkManager/dnsmasq.d/cache
Other methods

If using the dnsmasq daemon, then it is necessary to add the localhost address to resolv.conf (which NetworkManager will be overriding).

Since the upgrade of NetworkManager to 0.7, Arch Linux now calls dhcpcd directly instead of the common default with dhclient. Because of the arguments set with dhcpcd, it no longer sources the /etc/resolv.conf.head, and /etc/resolv.conf.tail settings for insertion of name servers. Several options are available.

The first option would be to add a script to the NetworkManager dispatcher to prepend localhost to resolv.conf:

/etc/NetworkManager/dispatcher.d/localhost-prepend
#!/bin/bash                                       
# Prepend localhost to resolv.conf for dnsmasq

if [[ ! $(grep 127.0.0.1 /etc/resolv.conf) ]]; then
  sed -i '1s|^|nameserver 127.0.0.1\n|' /etc/resolv.conf
fi

and make it executable:

# chmod +x /etc/NetworkManager/dispatcher.d/localhost-prepend

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: 127.0.0.1, DNS-server-one, ....

Lastly, NetworkManager with dhclient can be used (networkmanager-dhclientAUR).

Custom Configuration

As of NetworkManager 0.9.6, custom configurations can be created for dnsmasq by creating configuration files in /etc/NetworkManager/dnsmasq.d/

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

Start the daemon

To have dnsmasq to load upon startup:

# systemctl enable dnsmasq

To start dnsmasq immediately:

# systemctl start dnsmasq

To see if dnsmasq started properly, check the log; dnsmasq sends its messages to /var/log/messages.log. The network will also need to be restarted so the the DHCP client can create a new /etc/resolv.conf.

Test

DNS Caching

To do a lookup speed test choose a website that has not been visited since dnsmasq has been started (dig is part of the dnsutils 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.

DHCP Server

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.

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