Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Flush DNS in OS X
#!/bin/bash
#
# flushdns
# This script will flush your DNS cache on Mac OS X 10.1 - 12.x
# The method for doing this chages it seems in every version of OS X,
# and I can never remember the correct command, so I wrote this script
# and gave it a name I could remeber.
#
# v.2021-12-07 by bill@wellingtonnet.net
# https://gist.github.com/thewellington/6736175
OSX_VERSION=`sw_vers -productVersion`
echo "Attempting to flush DNS cache"
case $OSX_VERSION in
# OS X 10.1 - 10.4 (Puma, Jaguar, Panther, and Tiger )
10\.[1-4]*)
lookupd -flushcache
;;
# OS X 10.5 & 10.6 (Leopard and Snow Leopard)
10\.[56]*)
dscacheutil -flushcache
;;
# OS X 10.7 & 10.8 (Lion and Mountain Lion)
10\.[78]*)
killall -HUP mDNSResponder
;;
# OS X 10.9 (Mavericks)
10\.9*)
dscacheutil -flushcache
sudo killall -HUP mDNSResponder
;;
# OS X 10.10 - 10.10.3 - the crappy times (Yosemite, part 1 )
10\.10|10\.10\.[1-3])
sudo discoveryutil mdnsflushcache
sudo discoveryutil udnsflushcache
;;
# OS X 10.10.4 and up -- long live mDNSResponder! (Yosemite, part 2 )
10\.10\.[4-5])
dscacheutil -flushcache
sudo killall -HUP mDNSResponder
;;
# OS X 10.11 - 10.12.2 -- El Capitan, and Sierra
10\.11|10\.12|10\.12.[1-2]*)
dscacheutil -flushcache
sudo killall -HUP mDNSResponder
;;
# OS X 10.12.3 - 10.15 -- Sierra, High Sierra, Mojave, and Catalina
10\.12.[3-6]*|10\.1[3-5]*)
sudo killall -HUP mDNSResponder
sudo killall mDNSResponderHelper
dscacheutil -flushcache
;;
# OS X 11.0 -- Big Sur and Monterey, when in compatibility mode show up as "10.16". (SYSTEM_VERSION_COMPAT=1)
10\.16*)
sudo killall -HUP mDNSResponder
sudo killall mDNSResponderHelper
dscacheutil -flushcache
;;
# OS X 11.0 12.x -- Big Sur and Monterey
11\.*|12\.*)
sudo killall -HUP mDNSResponder
sudo killall mDNSResponderHelper
dscacheutil -flushcache
;;
# Catch anything else...
*)
echo "Sorry, this script is not prepared to handle your version of OS X."
exit 1
esac
echo "DNS cache flushed."
exit 0
@thewellington
Copy link
Author

thewellington commented Sep 2, 2015

Updated because Apple brought back the mDNSResponder!

@thewellington
Copy link
Author

thewellington commented Dec 7, 2021

Updated again for El Capitan through Monterey - Big Sur and Monterey include compatibility mode recognition too. (10.16)

I have been maintaining this script for some time, but I have been forgetting to update the gist. I have included the gist in the comments near the version, so that in the future, I will hopefully remember to update the gist when incrementing the version.

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