I have an DigitalOcean server that's being configured via cloud-init. With Ubuntu Bionic Beaver, this creates a netplan file in
/etc/netplan/50-cloud-init.yaml that contains nameserver addresses. However, overriding these addresses is not straightforward.
On Bionic Beaver,
/etc/resolv.conf is managed by the systemd-resolved service. I can edit the global nameservers like so:
echo 'DNS=184.108.40.206 220.127.116.11' >> /etc/systemd/resolved.conf service systemd-resolved restart
However, this doesn't affect the interface-specific nameservers set up by cloud-init. For that, I have to edit the netplan file directly or override it somehow. I can edit the file directly, but this isn't very automation-friendly. The netplan documentation says there's a hierarchy for netplan YAML files:
Configuration files can exist in three different locations with the precedence from most important to least as follows:
Alphabetically later files, no matter what directory in, will amend keys if the key does not already exist and override previous keys if they do.
Given this, I wrote an "alphabetically later file" as follows:
# /etc/netplan/51-cloudflare-dns.yaml network: ethernets: eth0: nameservers: addresses: - 18.104.22.168 - 22.214.171.124
Given the guidance above, I expected this to replace the nameservers for eth0 with the new ones applied. However, running
netplan apply appended to the list of nameservers for the interface rather than replacing them, as seen with
Link 2 (eth0) Current Scopes: DNS LLMNR setting: yes MulticastDNS setting: no DNSSEC setting: no DNSSEC supported: no DNS Servers: 126.96.36.199 188.8.131.52 184.108.40.206 220.127.116.11
How can I override netplan's per-interface nameservers, rather than appending to them? Both file-based and command-line solutions that persist across reboots are welcome.