Difference between revisions of "Dnsmasq"

From ArchWiki
Jump to: navigation, search
m (NetworkManager: - Cleanup, new script work)
(Updated and Cleanup)
Line 1: Line 1:
 
[[Category:Networking]]
 
[[Category:Networking]]
 
{{i18n|Dnsmasq}}
 
{{i18n|Dnsmasq}}
 +
{{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.
  
 
== Installing ==
 
== Installing ==
Line 10: Line 11:
 
== DHCP Server Setup ==
 
== DHCP Server Setup ==
  
The Dnsmasq configuration file needs to be configured ({{ic|/etc/dnsmasq.conf}}).  The most likely settings you'll need to configure are:
+
The dnsmasq configuration file needs to be configured ({{ic|/etc/dnsmasq.conf}}).  Here are the important settings:
  
 
{{bc|<nowiki>
 
{{bc|<nowiki>
Line 30: Line 31:
 
dhcp-host=aa:bb:cc:dd:ee:ff,192.168.111.50
 
dhcp-host=aa:bb:cc:dd:ee:ff,192.168.111.50
 
</nowiki>}}
 
</nowiki>}}
 
(Deprecated since the removal of {{AUR|tcp_wrappers}} from the official repos). If you choose not to bind the interfaces, the domain port will need to be allowed in {{ic|/etc/hosts.allow}}:
 
 
domain ALL : ALLOW
 
  
 
== DNS Cache Setup ==
 
== DNS Cache Setup ==
  
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 single computer edit {{ic|/etc/dnsmasq.conf}} and add the localhost listening address:
+
To set up dnsmasq as a DNS caching daemon on a single computer edit {{ic|/etc/dnsmasq.conf}} and uncomment the localhost listening address:
  
 
  listen-address=127.0.0.1
 
  listen-address=127.0.0.1
  
If you use this computer to act as a default DNS specify the (fixed) IP-addresse of this computer instead of 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 #replace this with the IP-address of your computer
+
  listen-address=<192.168.1.1# Example IP
  
After you have configured Dnsmasq, you will need to tell your DHCP client to prepend the localhost address to the known DNS addresses file ({{ic|/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.
+
=== DHCP Setup ===
  
=== dhcpcd ===
+
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.
 +
 
 +
==== 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:
 
{{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:
Line 53: Line 52:
 
  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 {{ic|/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 {{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.
 
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.
Line 81: Line 80:
 
Lastly, NetworkManager with dhclient can be used ({{AUR|networkmanager-dhclient}}).
 
Lastly, NetworkManager with dhclient can be used ({{AUR|networkmanager-dhclient}}).
  
==Start the Daemon==
+
== Start the Daemon ==
  
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 {{Pkg|dnsutils}} package) :
+
To have dnsmasq to load upon startup, add it to the daemons array in {{ic|/etc/rc.conf}}:
  
  $ dig archlinux.org | grep Query
+
  DAEMONS=(... dnsmasq network ...)
  
Now let's start it :
+
To stand dnsmasq immediately:
  
  # /etc/rc.d/dnsmasq start
+
  $ rc.d start dnsmasq
  
To have dnsmasq to load upon startup, add dnsmasq to your daemons array in {{ic|/etc/rc.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}}.
  
DAEMONS=(network dnsmasq ...)
+
== Test DNS Caching ==
  
To see if dnsmasq started properly, check the log; dnsmasq sends its messages to {{ic|/var/log/messages.log}}.
+
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):
You will also need to restart the network so that dhcpd can recreate {{ic|/etc/resolv.conf}}.
+
 
+
# /etc/rc.d/network restart
+
 
+
Now we will test our DNS lookup and measure the time response :
+
  
 
  $ dig archlinux.org | grep "Query time"
 
  $ dig archlinux.org | grep "Query time"
  
The Query time should have decreased.
+
Running the command again will use the cached DNS IP and result in a faster lookup time if dnsmasq is setup correctly.
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.
+
  
 
== Test DHCP Server ==
 
== Test 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 as you normally would.
+
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 ==
Line 117: Line 110:
 
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}}:
  
  server=/www.google.com/X.X.X.X
+
  server=/www.google.com/<ISP DNS IP>
 
+
Replace X.X.X.X with your ISP's DNS server/Router IP.
+
  
 
=== View leases ===
 
=== View leases ===
 
  cat /var/lib/misc/dnsmasq.leases
 
  cat /var/lib/misc/dnsmasq.leases

Revision as of 08:20, 13 May 2012

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

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.

Installing

Install dnsmasq from the official repositories.

DHCP Server Setup

The dnsmasq configuration file needs to be configured (/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

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

DHCP Setup

After configuring dnsmasq, the DHCP client will need to prepend the localhost address to the known DNS addresses file (/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.

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

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 "0,/nameserver/inameserver 127.0.0.1" /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).

Start the Daemon

To have dnsmasq to load upon startup, add it to the daemons array in /etc/rc.conf:

DAEMONS=(... dnsmasq network ...)

To stand dnsmasq immediately:

$ rc.d 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.

Test 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

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