DEV Community

Cover image for Flush your DNS cache with a single easy-to-remember command
Dan Walsh
Dan Walsh

Posted on • Edited on

Flush your DNS cache with a single easy-to-remember command

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
Enter fullscreen mode Exit fullscreen mode

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 🧐

  1. Open up Terminal.
  2. 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
Enter fullscreen mode Exit fullscreen mode
  1. Close and relaunch Terminal.

Testing it out 👨‍🔬

Now the moment of truth!

  1. In your freshly opened terminal window, type in flush-dns and hit Enter.
  2. Terminal will prompt you for your password as it needs to elevate privileges via the sudo commands—type it in and hit Enter again.
  3. 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

Top comments (0)