Windows – Setting DNS servers using OpenVPN client config file

networkingopenvpnvpnwindows

How can I set DNS servers on the client using only the client configuration. My client is a windows machine and I want to change the DNS servers when the client connects and revert back to the original configuration when I disconnect from the VPN.

All information I have found so far refers to pushing the DNS configuration to the client using the server's config but in this case I can't change the server configuration and am currently doing it manually every time I connect to the VPN. An openvpn config option to set the local machines DNS servers for the duration of the connection would be great.

Best Answer

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