The problem 🙄
Has this ever happened to you?
You're working away merrily on your web project and you've just made a DNS change—maybe you've added a new subdomain or you're now redirecting site traffic through a Web Application Firewall (WAF). But... your browser still thinks it can find your web project at the old IP address. You could wait until the time-to-live (TTL) expires on the DNS records you changed, but you've got a deadline to meet. Now you'll have to scour the web for another
mac flush dns
article...
Because it happens to me all the time!
The solution 👍
I figured it was time that I made my life just a little easier and create a system-wide bash script that I could invoke from any user account on my Mac.
Now when I want to flush my DNS, all I need to do is open a Terminal window and type:
$ flush-dns
NOTE: This works on MacOS Catalina and MacOS Big Sur. Be sure to replace the DNS flushing commands in my example with those relevant to your OS and version.
How to do it 🧐
- Open up Terminal.
- Enter the following commands:
$ cd /usr/local/bin
$ echo "sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder && echo \"DNS cache flushed successfully\"" > flush-dns
$ chmod +x flush-dns
- Close and relaunch Terminal.
Testing it out 👨🔬
Now the moment of truth!
- In your freshly opened terminal window, type in
flush-dns
and hit Enter. - Terminal will prompt you for your password as it needs to elevate privileges via the
sudo
commands—type it in and hit Enter again. - Terminal will then execute the DNS cache flushing commands we added earlier—perfect!
Don't forget! — You might also need to clear your browser cache and restart your browser for the script to take effect!
Cover image source: macrovector @ www.freepik.com