Feb 052012
 

This post describes hot to use dnsmasq as a local DNS server for faster caching and to resolve domain names in your private address space.

Installing dnsmasq

Installing dnsmasq is easy when you have Homebrew installed on your Mac:

brew update && brew install dnsmasq

And follow Homebrew’s hints on how to activate dnsmasq:

# copy example configuration file:
cp /usr/local/Cellar/dnsmasq/2.57/dnsmasq.conf.example /usr/local/etc/dnsmasq.conf
# To load dnsmasq automatically on startup:
sudo cp /usr/local/Cellar/dnsmasq/2.57/uk.org.thekelleys.dnsmasq.plist /Library/LaunchDaemons
sudo launchctl load -w /Library/LaunchDaemons/uk.org.thekelleys.dnsmasq.plist

Configuring dnsmasq

OK, let’s edit the dnsmasq configuration file /usr/local/etc/dnsmasq.conf now.

#address=/www.thekelleys.org.uk/fe80::20d:60ff:fe36:f83
# Change this line if you want dns to get its upstream servers from somewhere other that `/etc/resolv.conf`:
resolv-file=/etc/resolv.dnsmasq.conf

You also have to uncomment the line that says listen-address=127.0.0.1 .

Then save the following as /etc/resolv.dnsmasq.conf:

# Philipp's custom nameserver selection
# OpenDNS IPv6:
nameserver 2620:0:ccd::2
# Google:
nameserver 8.8.8.8
# OpenDNS IPv6:
nameserver 2620:0:ccc::2
# OpenDNS:
nameserver 208.67.222.222

(Re)Starting dnsmasq (The sudo launchctl load -w [...] command must have been run beforehand):

sudo launchctl stop uk.org.thekelleys.dnsmasq && sudo launchctl start uk.org.thekelleys.dnsmasq

Tell OS X to use dnsmasq

Now, edit /etc/resolv.conf to have ONLY the following line in it:

nameserver 127.0.0.1

Set the DNS settings in the network settings’ system preferences pane to 127.0.0.1 and ::1 to change this permanently.

Resources

VN:F [1.9.22_1171]
Rating: 8.5/10 (2 votes cast)
Install DNSmasq locally on Mac OS X via Homebrew, 8.5 out of 10 based on 2 ratings

Related Posts:

  6 Responses to “Install DNSmasq locally on Mac OS X via Homebrew”

  1. I’m curious if you know how to modify this setup to work with upstream servers provided by DHCP. Since you’re setting resolv-file=/etc/resolv.dnsmasq.conf dnsmasq is no longer paying attention to resolv.conf which is where os x lists the current dns servers. I can set dnsmasq to use resolv.conf but when i tell my machine to resolve DNS addresses locally I create a loop – my mac saves 127.0.0.1 to resolv.conf and the dhcp provided dns servers have disappeared from the file.

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
    • Hi matt, I would do it as follows:

      1. Configure dnsmasq to use a custom resolv file (/etc/resolv.dnsmasq.conf).
      2. Configure your system to use localhost (127.0.0.1 and ::1) as your DNS server.
      3. Set up a hook for network changes. That’s a script that should be called whenever your network interfaces change (get a new DHCP lease). In this script you should run ipconfig to find out the IP address(es) of the DNS server(s) that you are provided via DHCP and update dnsmasq’s custom resolv file accordingly. See below for details on how to parse it.


      So to set up a script to be run on network changes, I was looking for information on how to do it on google:

      1. Create a user daemon (in Library/LaunchDaemons) that watches the path /Library/Preferences/SystemConfiguration as shown in http://tech.inhelsinki.nl/locationchanger/
      2. As a dhcp hook. Stuff like dhcpcd-run-hooks (Linux) or /etc/dhclient-enter-hooks (NetBSD) doesn’t exist for Mac OS X.
      3. Before 10.5, adding your script to /System/Library/SystemConfiguration/Kicker.bundle/Contents/Resources/Kicker.xml has been an option. It is not anymore.

      If you need more help, please let me know ;-)


      update: I found a regex to parse the ipconfig command ( see https://gist.github.com/1825733 ):

      QUERY0="$(ipconfig getpacket en1)"
      DNS="$(echo $QUERY0 | sed 's/.domain_name_server (ip_mult): {\([^}]\)}.*/\1/')";
      echo $DNS
      VN:F [1.9.22_1171]
      Rating: 0 (from 0 votes)
  2. “You also have to uncomment the line that says listen-address=127.0.0.1″ Could you please tell me where I could find that line, to uncomment ? Sorry I’m quite new to OS X so please forgive my ignorance

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
    • Hi Jaison! The line listen-address=127.0.0.1 can be found in the file /usr/local/etc/dnsmasq.conf. ;-)

      VN:F [1.9.22_1171]
      Rating: 0 (from 0 votes)
  3. [...] machine.One thing that I learned from troubleshooting (in addition to .htaccess behavior) was about DNSMasq.  It allows you to create wildcard entries like .dev, which saves a lot of effort editing the [...]

 Leave a Reply

(required)

(required)


two − = 0

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>