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 dnsarticle...
Because it happens to me all the time!
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:
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.
- 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.
Now the moment of truth!
- In your freshly opened terminal window, type in
flush-dnsand hit Enter.
- Terminal will prompt you for your password as it needs to elevate privileges via the
sudocommands—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