Speed internet browsing via a local domain name server

May 04, '05 10:55:00AM

Contributed by: sparkleytone

Anyone who has 'switched' to the Mac, or has experience on a PC, knows that the PC makes the Mac look like a dog when it comes to web browsing. This is true for all browsers on the Mac. It turns out this has less to do with the end-user applications and really centers around DNS (Domain Name Server) lookups. The major bottleneck in our systems seems to be initial contact with the websites. We are having to make every single request to an external DNS server, thus creating a lag in initial response. Considering OS X is UNIX, this is completely unnecessary. We have a perfectly good DNS server (BIND) that is turned off by default.

In order to drastically improve web browsing experience, we will enable a local caching name server. This will require sudo privileges and comfort in the Terminal. We will be using the command-line text editor pico here, as TextEdit will not execute properly as root.

[robg adds: I haven't tested this hint ... and it does require a high level of comfort in the Terminal. Consider yourselves forewarned...]

A few things to remember before we start...

And now, the step-by-step...
  1. Start a root Terminal session

    Open a Terminal session and launch a root shell by typing sudo bash. The rest of the instructions assume every step is done in this root shell!

  2. Set up your machine to run the BIND server.
    # rndc-confgen > /etc/rndc.conf 
      /* creates rndc.conf and generates key */
    # head -n 6 /etc/rndc.conf > /etc/rndc.key
      /* parses the key into the proper file */
    # pico -w /etc/named.conf
      /* opens the BIND configuration file for editing in the text editor */
    
    Add the following into the options section, where x.x.x.x represents the IP addresses of either your ISP, or other DNS servers, known to you (Google for DNS servers if you don't know of any):
    forwarders {
    x.x.x.x;
    x.x.x.x;
    };
    
    Save the file and quit the editor. Hit Control-O, Enter, then Control-X.

  3. Create StartupItem for DNS [10.4 only; otherwise jump to next step]

    It seems as if Tiger has removed the (already disabled in Panther) DNS Server completely from StartupItems. We have to add it if running Tiger.
    # cd /System/Library/StartupItems
    # mkdir BIND
    # cd BIND
    # touch BIND; pico -w BIND
      /* Create first of two files, then open it in text editor */
    
    Copy the following into the file, save it, and exit:
    #!/bin/sh
    
    . /etc/rc.common
    
    if [ "${DNSSERVER}" = "-YES-" ]; then
      ConsoleMessage "Starting BIND DNS Server"
      /usr/sbin/named
    fi
    
    $ touch StartupParameters.plist; pico -w StartupParameters.plist
      /* Create and open second of two files needed */
    
    Copy the following into the file,save it, and exit:
    {
      Description = "Local Caching DNS Server";
      Provides = ("DNS Server");
      OrderPreference = "None";
      Messages = 
      {
      start = "Starting BIND DNS Server";
      stop = "Stopping BIND DNS Server";
      };
    }
    
    $ chmod +x BIND
      /* Make the script executable so it can actually be run */
    
  4. Enable DNS on boot
    # pico -w /etc/hostconfig
      /* Open the file OS X reads to start services */
    
    Change it to make DNSSERVER=-YES-. Here Tiger users will have to add this value; Panther users will simply change it to -YES-. Save the file and exit.

  5. Finish up with root
    # exit
      /* End root shell */
    
  6. Tell OS X to use your local DNS

    Open System Preferences, then the Network preferences panel. Change your 'DNS Server' setting to 127.0.0.1 for all the connections you use. This step is crucial, as failing to do so will make all your hard work completely useless.

    Finally, either reboot, or issue the following command in the terminal:
    $ sudo /System/Library/StartupItems/BIND/BIND
    
That's it -- enjoy your new internet connection!

Comments (70)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20050420025219402