Cloudflare 1.1.1.1 with Google Cloud VM Instance


Cloudflare’s 1.1.1.1 is pretty amazing. If you haven’t seen it, you should check it out. It’s a DNS lookup service which is fast, free and private. It works across all of your devices.

I recently had DNS issues on a Google Cloud VM: it was causing sporadic failures with my site monitor. I thought maybe Cloudflare’s 1.1.1.1 could help. The official Cloudflare DNS instructions for linux do not work with Google Cloud VM’s, because Google reloads the resolv.conf file every 24 hours when DHCP is renewed.

Preload custom configurations with the resolvconf package.

There are various methods for applying custom configurations into resolv.conf. I had success using the resolvconf package. First install that package sudo apt install resolvconf. Now run sudo nano /etc/resolvconf/resolv.conf.d/head. This is where we add Cloudflare’s 1.1.1.1 configurations which will be added at the top of the resolv.conf file.

nameserver 1.1.1.1
nameserver 1.0.0.1

This won’t trigger until the next DHCP renew. We can manually trigger that by restarting the VM’s network sudo systemctl restart systemd-networkd. Now check to see the newly generated resolve.conf file by running cat /etc/resolv.conf. Should look like this.

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "systemd-resolve --status" to see details about the actual nameservers.

nameserver 1.1.1.1
nameserver 1.0.0.1
nameserver 127.0.0.53
search x.xxxxxxxxx.internal google.internal
options edns0

To verify it’s working do a DNS lookup with DIG from the VM, example: dig anchor.host. In the bottom of the response you should see the following.

;; Query time: 8 msec
;; SERVER: 1.1.1.1#53(1.1.1.1)
;; WHEN: Thu Aug 22 08:18:42 EDT 2019
;; MSG SIZE  rcvd: 56

If the server line matches 1.1.1.1 or 1.0.0.1 then your Google Cloud VM is now using Cloudflare for DNS lookups. Success! 👏