How to have a hostname always be resolved using the local network’s DNS server when I’m connected to a VPN

dnsNetworkvpn

I have a MacBook Pro running macOS 10.14.6 that is frequently connected to a privacy-focused VPN that tunnels all non-local traffic and handles all DNS requests. However there is one hostname that I would like my local network's DNS server to handle (as specified by the local DHCP server). The reason is that depending on which local network my MacBook Pro is connected to this hostname may resolve to a local IP address, and in that case I don't want traffic to it to be tunneled through the VPN.

I've learned that I can override what DNS server is used to resolve a domain name by creating a file at /etc/resolver/hostname.domain and with the contents nameserver [ip address] where hostname.domain is the fully qualified domain name. However that only works if the IP address of the DNS server I want to use remains consistent. What I want it to do is use whatever DNS server is served by my local network's DHCP server.

Is there any way I can accomplish this using existing features of macOS or third party software?

In case it's relevant, the VPN's DNS server is provided and setup at connection time, and is not guaranteed to be the same IP address every time.

(I could write a script to do this by having it run whenever the network connection changes, checking the system's current local IP address, and then changing the /etc/resolver/hostname.domain file to point to the correct server but I'm trying to avoid having to do that work if possible!)

Best Answer

You could configure dnsmasq to

  • use your local DNS as an upstream server for the domain in question
  • use the VPN's DNS as the upstream for all other requests

This can be achieved by configuring a domain dependent upstream server in /etc/dnsmasq.conf, for example:

server=/hostname.domain/dns.server.local

Then change the VPN configuration to use your dnsmasq setup as its DNS server.

dnsmasq can be easily installed with Homebrew as follows:

brew install dnsmasq

Note that, in such a setup, the configuration file is located in $(brew --prefix)/etc/dnsmasq.conf. This post about using dnsmasq on macOS for local development may be provide you with some additional hints.