Difference between revisions of "Dnsmasq"

From ArchWiki
Jump to: navigation, search
m (proper name)
m (A minor apostrophe removal.)
(15 intermediate revisions by 9 users not shown)
Line 1: Line 1:
[[Category:Networking]]
+
[[Category:Domain Name System]]
 +
[[it:Dnsmasq]]
 
[[ru:Dnsmasq]]
 
[[ru:Dnsmasq]]
 
[[zh-CN: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 ==
  
 
[[pacman|Install]] {{Pkg|dnsmasq}} from the [[Official Repositories|official repositories]].
 
[[pacman|Install]] {{Pkg|dnsmasq}} from the [[Official Repositories|official repositories]].
 
== DHCP Server Setup ==
 
 
The dnsmasq configuration file needs to be configured ({{ic|/etc/dnsmasq.conf}}).  Here are the important settings:
 
 
{{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>}}
 
  
 
== DNS Cache Setup ==
 
== DNS Cache Setup ==
Line 43: Line 21:
 
  listen-address=<192.168.1.1>  # Example IP
 
  listen-address=<192.168.1.1>  # Example IP
  
=== DHCP Setup ===
+
=== DNS Addresses File ===
  
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.
+
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 ====
 
==== dhcpcd ====
Line 60: Line 38:
  
 
==== NetworkManager ====
 
==== NetworkManager ====
 +
 +
NetworkManager has the ability to start {{ic|dnsmasq}} from its configuration file.  Add the {{ic|1=dns=dnsmasq}} option to {{ic|NetworkManager.conf}} in the {{ic|[main]}} section and remove dnsmasq from loading at boot:
 +
 +
{{hc|/etc/NetworkManager/NetworkManager.conf|<nowiki>
 +
[main]
 +
plugins=keyfile
 +
dns=dnsmasq
 +
</nowiki>}}
 +
 +
Create a NetworkManager dnsmasq config directory if it doesn't exist:
 +
mkdir /etc/NetworkManager/dnsmasq.d
 +
 +
===== Other methods =====
 +
 +
If using the dnsmasq daemon, then it is necessary to add the localhost address to {{ic|resolv.conf}} (which NetworkManager will be overriding).
  
 
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 70: Line 63:
  
 
if [[ ! $(grep 127.0.0.1 /etc/resolv.conf) ]]; then
 
if [[ ! $(grep 127.0.0.1 /etc/resolv.conf) ]]; then
   sed -i ’1s|^|nameserver 127.0.0.1\n|/etc/resolv.conf
+
   sed -i '1s|^|nameserver 127.0.0.1\n|' /etc/resolv.conf
 
fi</nowiki>}}
 
fi</nowiki>}}
  
Line 81: Line 74:
 
Lastly, NetworkManager with dhclient can be used ({{AUR|networkmanager-dhclient}}).
 
Lastly, NetworkManager with dhclient can be used ({{AUR|networkmanager-dhclient}}).
  
== Start the Daemon ==
+
===== Custom Configuration =====
  
To have dnsmasq to load upon startup, add it to the daemons array in {{ic|/etc/rc.conf}}:
+
As of NetworkManager 0.9.6, custom configurations can be created for dnsmasq by creating configuration files in {{ic|/etc/NetworkManager/dnsmasq.d/}}
  
  DAEMONS=(... dnsmasq network ...)
+
== 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:
 +
 
 +
{{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 stand dnsmasq immediately:
 
To stand dnsmasq immediately:
  
$ rc.d start dnsmasq
+
{{bc|# systemctl start 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}}.
 
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}}.
Line 106: Line 125:
 
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 06:42, 3 January 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.

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 dns=dnsmasq option to NetworkManager.conf in the [main] section and remove dnsmasq from loading at boot:

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

Create a NetworkManager dnsmasq config directory if it doesn't exist:

mkdir /etc/NetworkManager/dnsmasq.d
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 stand 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