Sponsored By Aspose - File Format APIs for .NET

Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

Save your DNS and your SANITY when using VPN on a Mac (without rebooting)

There was time when using my Mac was bliss form a DNS perspective, I never had to worry about my routing tables getting corrupted. I could always rely on hosts getting resolved, life was good! And then a combination of things happened and well those good old days are gone :-(

  • The networking stack on OSX went downhill.
  • I joined Splunk.
  • I started using a VPN on my Mac (We use Juniper SSL VPN).
  • I started having to deal with this now recurring nightmare of my DNS suddenly failing, generally after using the VPN.

If you use a VPN on a Mac, I am sure you’ve seen it. Suddenly you type “https://github.com” in your browser, and you get a 404. “Is Github down?” you ask your co-workers? “Nope, works perfectly fine for me”. “Is hipchat down?”. “Nope, I am chatting away.”

Meanwhile. your browser looks something like this:

Screen Shot 2015-10-01 at 7.08.31 AM

 

 

 

 

 

 

AAARGH!

 

 

 

 

 

So you reboot, and then you find out that Github was up all along, the problem was your routing tables got screwed somehow related to the VPN, either that or the DNS demons have taken over your machine!

demons-1

 

 

 

 

 

After dealing with this constantly, you start to seriously lose your sanity! It will of course always happen at the most inopportune time, like when you are about to present to your execs or walk on stage!

But my friends, there is hope, I have a cure! This is a cure I learned from the wise ninjas at my office (Thank you Danielle and Itay!), it is a little bash alias, and it will save you AND your DNS. Drop it in your .bash_profile and open a new terminal.

alias fixvpn="sudo route -n flush && sudo networksetup -setv4off Wi-Fi && sudo networksetup -setdhcp Wi-Fi"

Next time the DNS demons come to get you, run this baby from the shell. It will excommunicate those demons and quick.

Screen Shot 2015-10-01 at 7.09.48 AM

 

 

 

 

 

 

Wait a few seconds, and bring up that webpage again.

Screen Shot 2015-10-01 at 7.09.57 AM

 

 

 

 

 

 

You DNS and Sanity are restored!

This entry was posted in tips, Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://www.crazyask.com Ashley Jones

    I’m also very interested in setting up my own server to tunnel my Internet traffic.

    But it seems hosting companies only give you limited download/upload bandwidth. And if they provide you unlimited bandwidth, usually the yearly price is quite high compared to subscribing to these VPN services (Witopia, BolehVPN, etc)

  • Mladen Mihajlovic

    Yup – OpenVPN with TunnelBlick

  • http://www.codebetter.com/glennblock Glenn Block

    Ugh, I had really hoped it would improve with El Cap. Are you using a VPN?

  • Mladen Mihajlovic

    Seems to bring things back up for a little while – but not a permanent solution (not even for the life of that connection)

  • Mladen Mihajlovic

    I’m having horrible dns experiences on El Cap. Just stumbled upon this so I’ll give it a try and let you know…

  • http://www.codebetter.com/glennblock Glenn Block

    I am hoping it is fixed, but have not tested yet.

  • Jose Santiago

    Is this still the case on El Cap where discoveryd which caused serious DNS issues has been removed in favor of the original mDNSresponder?

  • http://www.codebetter.com/glennblock Glenn Block

    Hey Troy

    You are correct in what is happening as to the why…..

    The first networksetup call disables DNS resolution. The second call re-enables it, which restores things to normal.

    As to the V4 vs V6, good question, no idea. It may be that the problem is specifically related to IPV4 but it generally gets in the way.

    Yes it will work in VPN or outside VPN. If you are on VPN and you run it, it will kill your VPN connection, which will generally automatically restore itself once resolution is back.

  • http://www.codebetter.com/glennblock Glenn Block

    Hey Troy

    You are correct in what is happening as to the why…..

    The first networksetup call disables DNS resolution. The second call re-enables it, which restores things to normal.

    As to the V4 vs V6, good question, no idea. It may be that the problem is specifically related to IPV4 but it generally gets in the way.

    Yes it will work in VPN or outside VPN. If you are on VPN and you run it, it will kill your VPN connection, which will generally automatically restore itself once resolution is back.

  • thoward37

    Pretty handy. For those who want to dig deeper, do you mind explaining what each of those commands does, and why they are needed?

    My understanding is that:

    – `route -n flush` clears the routing table (-n just makes it faster by avoiding name resolution).
    – `networksetup -setv4off Wi-Fi` turns off IPv4 for the Wi-Fi connection, so it only uses IPv6.. Why? I mean, sure we should all be using IPv6 nowadays, but does having IPv4 enabled as well somehow break your routing?
    – `networksetup -setdhcp Wi-Fi` enables DHCP for the Wi-Fi connection, so you get a dynamically assigned IP address vs statically assigned. That should be the default though, so why is it necessary to explicitly set it?

    Do these settings work in both a VPN and non-VPN context?

    Thanks,
    Troy