Setting DNS servers using OpenVPN client config file

Solution 1:

You can use batch script to do this, something like the following. It assumes your home DNS servers are 1.1.1.1 and 2.2.2.2 and your VPN DNS servers are 8.8.8.8 9.9.9.9:

vpn-connect.bat:

netsh interface ip set dns "Local Area Connection" static 8.8.8.8

netsh interface ip add dns "Local Area Connection" 9.9.9.9

vpn-disconnect.bat

netsh interface ip set dns "Local Area Connection" static 1.1.1.1

netsh interface ip add dns "Local Area Connection" 2.2.2.2

I was going to include in these scripts methods of connecting and disconnecting, however I do not see an option in OpenVPN to disconnect via command line. If you wish to automate connection, this should work:

C:\Program Files\bin\openvpn.exe C:\Program Files\conf\client.ovpn

Solution 2:

You can add the following to the client config file.

dhcp-option DNS <dns_server_ip_address>

On the server side it would have been :

push "dhcp-option DNS <dns_server_ip_address>"

It seems it's using dhcp-option on both sides. You can do the same with route.

Solution 3:

In addition to either of the two below:

dhcp-option DNS <dns_server_ip_address>          (add to client config)

or

push "dhcp-option DNS <dns_server_ip_address>"   (add to server config)

Add these to the client config as well, to force Windows to use the configured DNS:

register-dns
block-outside-dns

The 1st forces Windows to prefer the configured DNS server over any other it may have received from DHCP. The 2nd prevents DNS leakage to any DNS server other than the configured one.