Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

How to flush the local DNS cache on Mac OS X (See related posts)

If you want to add a virtualhost on your Mac OS X box without having to wait around for ages, then the easiest way to do so is to shove a line into /etc/hosts and flush the dnscache. Here's a friendly bash function to throw into your .bashrc:

function edithosts {
        if [ -x "`which $EDITOR`" ] || [ -x "`which $1`" ]
        then
                if [ -x "`which $EDITOR`" ]
                then
                        export TEMP_EDIT="`which $EDITOR`"
                else
                        export TEMP_EDIT="`which $1`"
                fi
                echo "* Using ${TEMP_EDIT} as editor"
                $TEMP_EDIT /etc/hosts && echo "* Successfully edited /etc/hosts"
                lookupd -flushcache && echo "* Flushed local DNS cache"
        else
                echo "Usage: edithosts [editor]"
                echo "(The editor is optional, and defaults to \$EDITOR)"
        fi
        unset TEMP_EDIT
}


More simply, you can just flush the DNS cache manually with:

lookupd -flushcache

Comments on this post

hadley posts on Jun 15, 2005 at 20:52
I've never had to flush the dns cache = when I change /etc/hosts it always works immediately.
xdanger posts on Dec 24, 2006 at 04:31
Just
sudo killall lookupd

You need to create an account or log in to post comments to this site.


Related Posts