Creating a Local DNS Server for TLD Locally

Lately I’ve been finding the joys in the little things in my work life. I’ve been fortunate enough to be spending the last couple of days working on some internal tools that we’ve been using internally for the last year or so, and I’ve had the opportunity to grow. During this time however, I’ve found that not every developer does things the same way. Since working at TechSmith, I’ve always enjoyed how they did things there. It was a positive way of thinking about the web and how to handle Dev, Test, Staging and Production systems and creating a workflow that worked best after several years of iterations. One thing that stuck with me was how they handled those environments. Today what I wanted to share with you was an easy way to create a top level domain in a DNS server locally. It’s quite easy and you can have some fun with it.

This tutorial assumes the following:

  • You are running a Mac or Linux computer
  • You are familiar with the command-line, or can at least follow along copy/pasting things into it. 🙂
  • You want to use .dev for your top level domain. If you don’t you’ll just need to change .dev for .whatup or whatever you want to use.

Installing Homebrew

The first step to the process is making sure you have the right tools. You first need to get Homebrew installed. If you remember MacPorts, Homebrew is a fancier MacPorts. It’s great for installing programming languages and other useful tools. So to install Homebrew you need to open up your command-line tool (iTerm for me!) and type in the following:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

This will install homebrew for you. Just a little background on Homebrew, it’s all git and ruby underneath. So in my former life, I lived in Ruby. So I still use a lot of it’s tools. They are great for just about any type of development.

What about the Magic?

At this point you may be asking yourself, so why did I just install Homebrew. Well the answer is quite simple! You needed Homebrew installed to install dnsmasq. Dnsmasq is a lightweight, easy to configure DNS forwarder and DHCP server. It is designed to provide DNS and, optionally, DHCP, to a small network. And it will work perfectly for what we’re going to do next. We’re going to install dnsmasq through Homebrew!

To install dnsmasq we’ll start out with running these commans:

brew install dnsmasq
mkdir -pv $(brew --prefix)/etc/
echo 'address=/.dev/127.0.0.1' > $(brew --prefix)/etc/dnsmasq.conf
sudo cp -v $(brew --prefix dnsmasq)/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
sudo mkdir -v /etc/resolver
sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/dev'

That’s it! You should then be able to run scutil –dns and it should show all of your current resolvers. You should see that all requests for a domain ending in .dev will go to the DNS server at 127.0.0.1.

Now all you need to do is start adding anything.dev to your Apache virtual hosts config file, and you’ll be creating new sites locally in now time.