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

+

'''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 {{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.

== Installation ==

== Installation ==

Revision as of 14:35, 14 March 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 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.

DNS Addresses File

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

dhclient

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 the dnsmasq.service from being loaded by systemd:

/etc/NetworkManager/NetworkManager.conf

[main]
plugins=keyfile
dns=dnsmasq

Custom configurations can be created for dnsmasq by creating configuration files in /etc/NetworkManager/dnsmasq.d/. For example, to change the size of the DNS cache (which is stored in RAM):

/etc/NetworkManager/dnsmasq.d/cache

cache-size=1000

When dnsmasq is started by NetworkManager, the config file in this directory is used instead of the default config file.

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.

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

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: