<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Passing Curiosity: Posts tagged dns</title>
    <link href="https://passingcuriosity.com/tags/dns/dns.xml" rel="self" />
    <link href="https://passingcuriosity.com" />
    <id>https://passingcuriosity.com/tags/dns/dns.xml</id>
    <author>
        <name>Thomas Sutton</name>
        
        <email>me@thomas-sutton.id.au</email>
        
    </author>
    <updated>2013-10-23T00:00:00Z</updated>
    <entry>
    <title>Using Dnsmasq for local development on OS X</title>
    <link href="https://passingcuriosity.com/2013/dnsmasq-dev-osx/" />
    <id>https://passingcuriosity.com/2013/dnsmasq-dev-osx/</id>
    <published>2013-10-23T00:00:00Z</published>
    <updated>2013-10-23T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>Most web developers will be familiar with the process of updating your
<code>/etc/hosts</code> file to direct traffic for <code>coolproject.dev</code> to <code>127.0.0.1</code>. Most
will also be familiar with the problems of this approach:</p>
<ul>
<li><p>it requires a configuration change every time you add or remove a project; and</p></li>
<li><p>it requires administration access to make the change.</p></li>
</ul>
<p>Installing a local DNS server like <a href="http://www.thekelleys.org.uk/dnsmasq/doc.html">Dnsmasq</a> and configuring your
system to use <em>that</em> server can make these configuration changes a thing of the
past. In this post, I’ll run through the process of:</p>
<ol type="1">
<li><p>Installing Dnsmasq on OS X.</p></li>
<li><p>Configuring Dnsmasq to respond to all <code>.dev</code> requests with <code>127.0.0.1</code>.</p></li>
<li><p>Configure OS X to send all <code>.dev</code> requests requests to Dnsmasq.</p></li>
</ol>
<p>Before we get started, I should give you a warning: these instructions show you
how to install new system software and change your system configuration. Like
all such changes, you <strong>should not proceed</strong> unless you are confident you have
understood them <em>and</em> that you can reverse the changes if needed.</p>
<blockquote>
<p>The web world has moved on since I wrote this post in 2013. <code>.dev</code> is
now a real domain (owned by Google) and some browsers (at least Google
Chrome) require all connections to <code>.dev</code> to use HTTPS. If you are
going to follow these instructions you should probably <strong>use <code>.test</code>
instead of <code>.dev</code></strong>. See <a href="https://ma.ttias.be/chrome-force-dev-domains-https-via-preloaded-hsts/">this blog post</a> for more details.</p>
<p>Thanks to Kinjal Dixit and Milan Peters for the suggestion.</p>
</blockquote>
<h2 id="installing-dnsmasq">Installing Dnsmasq</h2>
<p>To quote the Dnsmasq project home page</p>
<blockquote>
<p>Dnsmasq is a lightweight, easy to configure DNS forwarder and DHCP
server […] is targeted at home networks[.]</p>
</blockquote>
<p>There are plenty of ways to install Dnsmasq but my favourite (on OS X) is to
use the <a href="http://brew.sh">Homebrew</a> package manager. Installing Homebrew is fairly simple but
beyond my scope here.</p>
<p>Once you have Homebrew installed, using it to install Dnsmasq is easy:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Update your homebrew installation</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="ex">brew</span> up</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Install dnsmasq</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="ex">brew</span> install dnsmasq</span></code></pre></div>
<p>The installation process will output several commands that you can use to start
Dnsmasq automatically with a default configuration. I used the following
commands but you should use whichever commands <code>brew</code> tells you to:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Copy the default configuration file.</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="fu">cp</span> <span class="va">$(</span><span class="ex">brew</span> list dnsmasq <span class="kw">|</span> <span class="fu">grep</span> /dnsmasq.conf.example$<span class="va">)</span> /usr/local/etc/dnsmasq.conf</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Copy the daemon configuration file into place.</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="fu">sudo</span> cp <span class="va">$(</span><span class="ex">brew</span> list dnsmasq <span class="kw">|</span> <span class="fu">grep</span> /homebrew.mxcl.dnsmasq.plist$<span class="va">)</span> /Library/LaunchDaemons/</span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Start Dnsmasq automatically.</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a><span class="fu">sudo</span> launchctl load /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist</span></code></pre></div>
<h2 id="configuring-dnsmasq">Configuring Dnsmasq</h2>
<p>Now that you have Dnsmasq installed and running, it’s time to configure it! The
configuration file lives at <code>/usr/local/etc/dnsmasq.conf</code> by default, so open
this file in your favourite editor.</p>
<p>One the many, many things that Dnsmasq can do is compare DNS requests against a
database of patterns and use these to determine the correct response. I use
this functionality to match any request which ends in <code>.dev</code> and send
<code>127.0.0.1</code> in response. The Dnsmasq configuration directive to do this is very
simple:</p>
<pre><code>address=/dev/127.0.0.1</code></pre>
<p>Insert this into your <code>/usr/local/etc/dnsmasq.conf</code> file (I put it near the
example <code>address=/double-click.net/127.0.0.1</code> entry just to keep them all
together) and save the file.</p>
<p>You may need to restart Dnsmasq to get it to recognise this change. Restarting
Dnsmasq is the same as any other service running under <code>launchd</code>:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sudo</span> launchctl stop homebrew.mxcl.dnsmasq</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="fu">sudo</span> launchctl start homebrew.mxcl.dnsmasq</span></code></pre></div>
<p>You can test Dnsmasq by sending it a DNS query using the <code>dig</code> utility. Pick a
name ending in <code>dev</code> and use dig to query your new DNS server:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="ex">dig</span> testing.testing.one.two.three.dev @127.0.0.1</span></code></pre></div>
<p>You should get back a response something like:</p>
<pre><code>;; ANSWER SECTION:
testing.testing.one.two.three.dev. 0 IN	A	127.0.0.1</code></pre>
<h2 id="configuring-os-x">Configuring OS X</h2>
<p>Now that you have a working DNS server you can configure your operating system
to <em>use</em> it. There are two approaches to this:</p>
<ol type="1">
<li><p>Send <em>all</em> DNS queries to Dnsmasq.</p></li>
<li><p>Send <em>only <code>.dev</code></em> queries to Dnsmasq.</p></li>
</ol>
<p>The first approach is easy – just change your DNS settings in System
Preferences – but probably won’t work without additional changes to the
Dnsmasq configuration.</p>
<p>The second is a bit more tricky, but not much. Most UNIX-like operating systems
have a configuration file called <code>/etc/resolv.conf</code> which controls the way DNS
queries are performed, including the default server to use for DNS queries
(this is the setting that gets set automatically when you connect to a network
or change your DNS server/s in System Preferences).</p>
<p>OS X also allows you to configure additional <em>resolvers</em> by creating
configuration files in the <code>/etc/resolver/</code> directory. This directory probably
won’t exist on your system, so your first step should be to create it:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sudo</span> mkdir <span class="at">-p</span> /etc/resolver</span></code></pre></div>
<p>Now you should create a new file in this directory for each resolver you want
to configure. Each resolver corresponds – roughly and for our purposes – to a
top-level domain like our <code>dev</code>. There a number of details you can configure
for each resolver but I generally only bother with two:</p>
<ul>
<li>the <em>name</em> of the resolver (which corresponds to the <em>domain name</em> to be
resolved); and</li>
<li>the DNS server to be used.</li>
</ul>
<p>For more information about these files, see the <code>resolver(5)</code> manual page:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="fu">man</span> 5 resolver</span></code></pre></div>
<p>Create a new file with <em>the same name</em> as your new top-level domain (I’m using
<code>dev</code>, recall) in the <code>/etc/resolver/</code> directory and add a <code>nameserver</code> to it
by running the following commands:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sudo</span> tee /etc/resolver/dev <span class="op">&gt;</span>/dev/null <span class="op">&lt;&lt;EOF</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="st">nameserver 127.0.0.1</span></span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a><span class="op">EOF</span></span></code></pre></div>
<p>Here <code>dev</code> is the top-level domain name that I’ve configured Dnsmasq to respond
to and <code>127.0.0.1</code> is the IP address of the server to use.</p>
<p>Once you’ve created this file, OS X will automatically read it and you’re done!</p>
<h2 id="testing">Testing</h2>
<p>Testing you new configuration is easy; just use <code>ping</code> check that you can now
resolve some DNS names in your new top-level domain:</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Make sure you haven't broken your DNS.</span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ping</span> <span class="at">-c</span> 1 www.google.com</span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Check that .dev names work</span></span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a><span class="fu">ping</span> <span class="at">-c</span> 1 this.is.a.test.dev</span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ping</span> <span class="at">-c</span> 1 iam.the.walrus.dev</span></code></pre></div>
<p>You should see results that mention the IP address in your Dnsmasq
configuration like this:</p>
<pre><code>PING iam.the.walrus.dev (127.0.0.1): 56 data bytes</code></pre>
<p>You can now just make up new DNS names under <code>.dev</code> whenever you please.
Congratulations!</p>
<p>Maybe the next step on your journey should be learning how to do configure
Apache virtual hosts automatically based on host names? If this sounds
interesting, contact me using the details below and I’ll write that article too.</p>]]></summary>
</entry>

</feed>
