Skip to content

Instantly share code, notes, and snippets.

@thewellington
Last active January 18, 2023 02:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thewellington/6736175 to your computer and use it in GitHub Desktop.
Save thewellington/6736175 to your computer and use it in GitHub Desktop.
Flush DNS in OS X
#!/bin/bash
#
# flushdns
# This script will flush your DNS cache on Mac OS X 10.1 - 13.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.2022-11-03 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, Monterey, and Ventura
11\.*|12\.*|13\.*)
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 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.

@thewellington
Copy link
Author

thewellington commented Nov 3, 2022

Updated to include Ventura

Look at that, including the link to the gist last year made me remember to update the gist this year!

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