Flush dnsmasq cache automatically after vpn connection

There are some cases which will be good to flush dnsmasq internal cache.

Lets assume that you have an openvpn connection profile and when you connect to vpn in network-manager, you’re getting a custom dns server address. Most of the distributions uses dnsmasq internally in network-manager and can update dns server list of dnsmasq successfully. But, if you previously queried a host before the vpn connection is active, it is cached by the dnsmasq and you’ll get same answer after vpn connection is activated.

But, if your newly received dns servers from the vpn connection has some other records for the same dns query, you’ll have to restart or flush the cache’s of dnsmasq.

Dnsmasq has clear-on-reload startup parameter for this specific case. In order to flush the cache automatically, you just need to start dnsmasq with this parameter.

On Debian based distros, just add following lines to /etc/default/dnsmasq and restart it:

DNSMASQ_OPTS="--clear-on-reload"

Now, dnsmasq will be signalled by network manager on every new connection setup and it will be flush its cache automatically.