How to Set DNS Automatically Using OpenVPN

How to Set DNS Automatically Using OpenVPN

The idea for this article started after hearing TWiT Security Now podcast, episode Quad Nine where Steve Gibson and Leo Laporte talk about what they described as a ‘very appealing and speedy new alternative DNS service, called Quad9.’

I wanted my DNS on all my devices to be set automatically without me having to re-configure it every time I join a new network.

As we all know, in iOS, you need to change your DNS records for each and every network. On Android, you need to change your IP settings first to static then write the IP manually. After that, you’d enter your preferred DNS for each and every network.

Well, shit. This is way too much work for something that should be quite easy and simple and more importantly, automatic. As in, set it and forget it.

If you use a VPN, as you should, then keep reading to learn How to Set DNS Automatically Using OpenVPN

What I did was take advantage of the fact that I use a VPN1 with the OpenVPN app2 and I configured my .ovpn file to uses a specific DNS every time it connects to the VPN network by adding the following line:

    
dhcp-option DNS X.X.X.X

Voilà. You’re good to go.

Test it, you should see that the DNS is now automatically being set to the one you added.

If you want to add something like Google DNS or OpenDNS which contains a primary and secondary DNS record, all you have to do is add that same line a second time with the second DNS record. Like this:

    
dhcp-option DNS X.X.X.X
dhcp-option DNS X.X.X.X

Conclusion:

Now, no matter what DNS provider you use, whether it’s Google DNS3, OpenDNS4 or the brand new Quad9 DNS5 by IBM or 1.1.1.1 DNS6 by Cloudflare, with this “hack” you’ll be able to Set DNS Automatically While Using OpenVPN and continue doing your things online.


  1. I’ve been using TorGuard for 2 years now without any issues. 
  2. iOS and Android
  3. 8.8.8.8 – 8.8.4.4 
  4. 208.67.222.222 – 208.67.220.220 
  5. 9.9.9.9 – 149.112.112.112 
  6. 1.1.1.1 – 1.0.0.1 

Enjoyed this article?

Consider becoming a member, It will help keep the site up and running and me awake to write more articles.

It can be as little as $1 and take only 1 minute. Thank you.

Become a Member