Flush DNS in OS X
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
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.
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
Updated because Apple brought back the mDNSResponder!