Difference between revisions of "Dnsmasq"

From ArchWiki
Jump to: navigation, search
(Updated and Cleanup)
(resolv.conf)
(41 intermediate revisions by 20 users not shown)
Line 1: Line 1:
[[Category:Networking]]
+
[[Category:Domain Name System]]
{{i18n|Dnsmasq}}
+
[[it:Dnsmasq]]
 +
[[ru:Dnsmasq]]
 +
[[zh-CN:Dnsmasq]]
 
{{Lowercase_title}}
 
{{Lowercase_title}}
  
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.
+
'''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 ==
Line 9: Line 11:
 
[[pacman|Install]] {{Pkg|dnsmasq}} from the [[Official Repositories|official repositories]].
 
[[pacman|Install]] {{Pkg|dnsmasq}} from the [[Official Repositories|official repositories]].
  
== DHCP Server Setup ==
+
== DNS Cache Setup ==
  
The dnsmasq configuration file needs to be configured ({{ic|/etc/dnsmasq.conf}}).  Here are the important settings:
+
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:
  
{{bc|<nowiki>
+
  listen-address=127.0.0.1
# 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
+
To use this computer to listen on it's LAN IP address for other computers on the network:
# 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
+
listen-address=192.168.1.1    # Example IP
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
+
It is recommended that you use a static LAN ip in this case.
# NIC MAC address:
+
dhcp-host=aa:bb:cc:dd:ee:ff,192.168.111.50
+
</nowiki>}}
+
  
== DNS Cache Setup ==
+
=== DNS Addresses File ===
  
To set up dnsmasq as a DNS caching daemon on a single computer edit {{ic|/etc/dnsmasq.conf}} and uncomment the localhost listening address:
+
{{Merge|resolv.conf|Same topic. Also note that most of this can be done also natively in {{ic|/etc/resolvconf.conf}} using the {{ic|name_servers}} and {{ic|name_servers_append}} options.}}
  
listen-address=127.0.0.1
+
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.
  
To use this computer to act as a default DNS specify the fixed IP address of the network:
+
==== resolv.conf ====
  
listen-address=<192.168.1.1>  # Example IP
+
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:
  
=== DHCP Setup ===
+
{{hc|/etc/resolv.conf|
 +
nameserver 127.0.0.1
 +
# External nameservers
 +
...
 +
}}
  
After configuring dnsmasq, the DHCP client will need to prepend the localhost address to the known DNS addresses file ({{ic|/etc/resolv.conf}}) - this sends all queries to dnsmasq before trying to resolve them to an external DNS. After the DHCP client is configured the network will need to be restarted for changes to take effect.
+
Now DNS queries will be resolved first with dnsmasq, only checking external servers if dnsmasq cannot resolve the query. {{Pkg|dhcpcd}} and {{Pkg|networkmanager}}, 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.4.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 ====
 
==== dhcpcd ====
Line 60: Line 77:
 
==== NetworkManager ====
 
==== NetworkManager ====
  
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.
+
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 option would be to add a script to the NetworkManager dispatcher to prepend localhost to {{ic|resolv.conf}}:
+
{{hc|/etc/NetworkManager/NetworkManager.conf|<nowiki>
 +
[main]
 +
plugins=keyfile
 +
dns=dnsmasq
 +
</nowiki>}}
  
{{hc|/etc/NetworkManager/dispatcher.d/localhost-prepend|<nowiki>
+
For permanent caching add a config directory for {{ic|dnsmasq}} and set the cache number of nameservers (default: 150?):
#!/bin/bash                                     
+
# Prepend localhost to resolv.conf for dnsmasq
+
  
if [[ ! $(grep 127.0.0.1 /etc/resolv.conf) ]]; then
+
mkdir /etc/NetworkManager/dnsmasq.d
  sed -i "0,/nameserver/inameserver 127.0.0.1" /etc/resolv.conf
+
echo "cache-size=1000" | sudo tee /etc/NetworkManager/dnsmasq.d/cache
fi</nowiki>}}
+
  
and make it executable:
+
===== Other methods =====
  
  # chmod +x /etc/NetworkManager/dispatcher.d/localhost-prepend
+
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, ...}}.
  
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, ...}}.
+
===== Custom Configuration =====
  
Lastly, NetworkManager with dhclient can be used ({{AUR|networkmanager-dhclient}}).
+
As of NetworkManager 0.9.6, custom configurations can be created for {{Pkg|dnsmasq}} by creating configuration files in {{ic|/etc/NetworkManager/dnsmasq.d/}}
  
== Start the Daemon ==
+
{{Note|You have to add the following line to your NetworkManager configuration file {{ic|/etc/NetworkManager/NetworkManager.conf}} : {{ic|dns&#61;dnsmasq}} in order to enable custom configuration files.}}
  
To have dnsmasq to load upon startup, add it to the daemons array in {{ic|/etc/rc.conf}}:
+
{{Tip|This method can allow you to enable custom DNS settings on particular domains. For instance : {{ic|server&#61;/example1.com/exemple2.com/xx.xxx.xxx.x}} change the first DNS address to {{ic|xx.xxx.xxx.xx}} while browsing only the following websites {{ic|example1.com, example2.com}}. This method is preferred to a global DNS configuration when using particular DNS nameservers which lack of speed, stability, privacy and security.}}
  
DAEMONS=(... dnsmasq network ...)
+
== DHCP Server Setup ==
  
To stand dnsmasq immediately:
+
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:
  
  $ rc.d start dnsmasq
+
{{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>
 +
 
 +
# 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
 +
</nowiki>}}
 +
 
 +
== Start the daemon ==
 +
To have dnsmasq to load upon startup:
 +
 
 +
{{bc|# systemctl enable dnsmasq}}
 +
 
 +
To start dnsmasq immediately:
 +
 
 +
{{bc|# systemctl start dnsmasq}}
 +
 
 +
To see if dnsmasq started properly, check the system's journal:
 +
 
 +
{{bc|# journalctl -u dnsmasq}}
  
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}}.
+
The network will also need to be restarted so the the DHCP client can create a new {{ic|/etc/resolv.conf}}.
  
== Test DNS Caching ==
+
== 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):
 
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):
Line 100: Line 149:
 
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.
  
== Test DHCP Server ==
+
=== 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.
 
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 ==
+
== 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}}:
 
+
{{bc|1=server=/www.google.com/<ISP DNS IP>}}
server=/www.google.com/<ISP DNS IP>
+
  
 
=== View leases ===
 
=== View leases ===
cat /var/lib/misc/dnsmasq.leases
+
{{bc|$ cat /var/lib/misc/dnsmasq.leases}}

Revision as of 18:22, 8 January 2014

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 listen-address directive, adding in the localhost IP address:

listen-address=127.0.0.1

To use this computer to listen on it's 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.

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 be done also 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 and networkmanager, 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.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 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

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

Custom Configuration

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

Note: You have to add the following line to your NetworkManager configuration file /etc/NetworkManager/NetworkManager.conf : dns=dnsmasq in order to enable custom configuration files.
Tip: This method can allow you to enable custom DNS settings on particular domains. For instance : server=/example1.com/exemple2.com/xx.xxx.xxx.x change the first DNS address to xx.xxx.xxx.xx while browsing only the following websites example1.com, example2.com. This method is preferred to a global DNS configuration when using particular DNS nameservers which lack of speed, stability, privacy and security.

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 system's journal:

# journalctl -u dnsmasq

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