Skip to content

Instantly share code, notes, and snippets.

@KelseyDH
Forked from nsbingham/local-dnsserver.md
Created February 4, 2020 20:30
Show Gist options
  • Save KelseyDH/8a3922cce4d75e197fdfe5c39387d71a to your computer and use it in GitHub Desktop.
Save KelseyDH/8a3922cce4d75e197fdfe5c39387d71a to your computer and use it in GitHub Desktop.
Testing a CNAME

Setting up a local DNS server with bind on OSX Mavericks

This is really just an approach for locally testing DNS changes, which can easily be done with a HOSTS file if the change involves an IP address, but gets a bit trickier when things like CNAMEs are involved. This is only meant to test locally off a single machine.

  1. Install bind using homebrew

    brew install bind

  2. Follow the installation steps to start up bind

    To have launchd start bind at startup:
        sudo cp -fv /usr/local/opt/bind/*.plist /Library/LaunchDaemons
    Then to load bind now:
        sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.bind.plist
    
  3. Create a new zone file. This can technically live wherever, but it's probably a good idea to keep it close to the name.conf which is bind's main config file. Homebrew installs it here: /usr/local/etc/. It's smart to use the name of the domain and name it something like db.example.org.

    Enter something like the following:

    ;
    ; BIND data file for example.org
    ;
    $TTL 4h
    @  IN  SOA ns1.example.org. root.example.org. (
    	    2		; Serial
    	    604800 	; Refresh
    	    86400 	; Retry
    	    2419200 	; Expire
    	    604800 ) 	; Negative Cache TTL
    ;
    @	IN NS	ns1.example.org.
    @ 	IN A	123.12.34.57
    www	IN A	123.12.34.57
    ns1 	IN A 	127.0.0.1
    

    There's a great breakdown of what this file means here. The main thing to note is that ns1 or your name server should point to your machine.

  4. Update the named.conf file to point to your new zone.

    zone "example.org" {
    	type master;
    	file "/usr/local/etc/db.example.org";
    };
    
  5. Verify that there are no errors in both files using the following commands.

    named-checkconf /usr/local/etc/named.conf
    named-checkzone db.modern.ie /usr/local/etc/db.modern.ie 
    

    If everything is right, the first command should output nothing. The second command should print OK on the last line.

  6. Set your machine's DNS to point to your machine as a nameserver.

    Example: Changing DNS server settings on Mac OS 10.5

    1. From the Apple menu, click System Preferences, then click Network.
    2. Select the connection for which you want to configure DNS. For example: To change the settings for an Ethernet connection, select Built-In Ethernet, and click Advanced. To change the settings for a wireless connection, select Airport, and click Advanced.
    3. Select the DNS tab.
    4. Click + to replace any listed addresses with, or add, the address 127.0.0.1.
    5. Click Apply and OK.
  7. Flush your DNS cache with the following command.

    dscacheutil -flushcache
    
  8. Restart bind with the following commands.

    sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.bind.plist
    sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.bind.plist
    
  9. Validate that your domain is now using your local DNS.

    nslookup www.example.org localhost
    Server:		localhost
    

    I also added a resolver that pointed to 127.0.0.1, but I'm not sure it is actually needed.

    Create configuration so that the wilcard is still accesible when you are not connected to a network.

    mkdir /etc/resolver
    vim /etc/resolver/org
    

    Add the following:

    nameserver 127.0.0.1
    

    This should route all .org domains through your local DNS server.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment