DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

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

Posted 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
    
  3. 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)

๐ŸŒš Friends don't let friends browse without dark mode.

Sorry, it's true.