Blocking specific DNS queries in a VPN using iptables

Blocking specific DNS queries in a VPN using iptables


I have a VPN running on a VM in Google Cloud and had recently came across  a problem of is getting resolved to the link-local address. is used by applications running in Google Cloud to read information about the host.

But the problem is, if you are developing applications that uses this to check if the app is running on the Google Cloud platform, this could cause problems when you are on the VPN.

The Hack

One of the hack that I was successful in implementing was to use iptables to redirect these requests to the Google public resolver.

Here is the rule:

iptables -t nat -A PREROUTING -i tun0 -p udp --dport 53 -m string --hex-string "|08|metadata|06|google|08|internal|" --algo bm -j DNAT --to-destination

make sure to change the interface to the correct one.

To block any other domain, just change the string in this format:

|number of following characters|the domain part|


If you want to block, it would be |03|foo|03|bar|02|in|

You get the idea.

Note: I am redirecting these requests to because they will reply with "NXDOMAIN" which should be the ideal case