Question

How to permanently change DNS nameservers in Ubuntu 18.04.3? Wiki article is not working.

Posted November 23, 2019 29.2k views
DNSUbuntu 18.04

Hello,

I am trying to permanently change the DNS nameserver being using on my droplet.

I following this article:
https://www.digitalocean.com/community/questions/how-do-i-switch-my-dns-resolvers-away-from-google

But the permanent fix option is not working.

The file “/etc/network/interfaces.d/50-cloud-init.cfg” does not exist.
And when I add nameservers to the file “/etc/network/interfaces” and restart the droplet, no change occurs. The file is saved, but the DNS server being used are still the default ones.

My “/etc/network/interfaces” file looks like the following:

auto lo
iface lo inet loopback

source /etc/network/interfaces.d/*

dns-nameservers 208.67.222.222 208.67.220.220

Am I missing something? I am new to Linux.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
2 answers

Hello,

With the older Ubuntu distributions, you could do that with the /etc/resolv.conf however, with Ubuntu 18.04 you should make the change in the netplan configs at /etc/netplan/*.yaml files.

The file would look something like this:

network:
    version: 2
    ethernets:
        eth0:
            addresses:
            - 123.123.123.123/20
            - 10.0.0.0/16
            gateway4: 123.123.123.1
            match:
                macaddress: cc:8c:11:e1:1b:81
            nameservers:
                addresses:
                - 8.8.8.8
                - 8.8.4.4
                search: []
            set-name: eth0

To change your nameservers, just update the addresses under the nameservers section. And then run:

sudo netplan apply

Hope that this helps!
Regards,
Bobby

  • Since im running a local upstream service on my droplet, I changed the nameservers addresses section to only contain 127.0.0.1.

    When I run sudo netplan apply and then (after rebooting) check the nameserver that’s being used by typing dig google.com it shows SERVER: 127.0.0.53#53(127.0.0.53).

    Running sudo netplan --debug apply I get:

    ** (generate:1157): DEBUG: 15:23:20.061: Processing input file /etc/netplan/50-cloud-init.yaml..
    ** (generate:1157): DEBUG: 15:23:20.061: starting new processing pass
    ** (generate:1157): DEBUG: 15:23:20.061: We have some netdefs, pass them through a final round of validation
    ** (generate:1157): DEBUG: 15:23:20.061: eth1: setting default backend to 1
    ** (generate:1157): DEBUG: 15:23:20.062: Configuration is valid
    ** (generate:1157): DEBUG: 15:23:20.062: eth0: setting default backend to 1
    ** (generate:1157): DEBUG: 15:23:20.062: Configuration is valid
    ** (generate:1157): DEBUG: 15:23:20.062: Generating output files..
    ** (generate:1157): DEBUG: 15:23:20.062: NetworkManager: definition eth0 is not for us (backend 1)
    ** (generate:1157): DEBUG: 15:23:20.062: NetworkManager: definition eth1 is not for us (backend 1)
    DEBUG:netplan generated networkd configuration changed, restarting networkd
    DEBUG:no netplan generated NM configuration exists
    DEBUG:eth0 not found in {}
    DEBUG:eth1 not found in {'eth0': {'addresses': ['[redacted]', '[redacted]'], 'gateway4': '[redacted]', 'match': {'macaddress': '[redacted]'}, 'nameservers': {'addresses': ['127.0.0.1'], 'search': []}, 'set-name': 'eth0'}}
    DEBUG:Merged config:
    network:
      bonds: {}
      bridges: {}
      ethernets:
        eth0:
          addresses:
          - [redacted]
          - [redacted]
          gateway4: [redacted]
          match:
            macaddress: [redacted]
          nameservers:
            addresses:
            - 127.0.0.1
            search: []
          set-name: eth0
        eth1:
          addresses:
          - [redacted]
          match:
            macaddress: [redacted]
          nameservers:
            addresses:
            - 127.0.0.1
            search: []
          set-name: eth1
      vlans: {}
      wifis: {}
    
    DEBUG:Skipping non-physical interface: lo
    DEBUG:device eth0 operstate is up, not changing
    DEBUG:device eth1 operstate is up, not changing
    DEBUG:{}
    DEBUG:netplan triggering .link rules for lo
    DEBUG:netplan triggering .link rules for eth0
    DEBUG:netplan triggering .link rules for eth1
    

    (Note: everything that I replaced with [redacted] was left as it was by default)

    Any ideas on how to fix this? I didnt think changing my nameserver was such a pain on Ubuntu 18.04, but I have to use this system for other reasons…

Just to point out for servers:

For persistence through reboots, you must create a file to stop cloudinit from resetting 50-cloud-init.yaml… (tested on ubuntu 20 server on DO today)

Copied from the default /etc/netplan/50-cloud-init.yaml :
This file is generated from information provided by the datasource. Changes
to it will not persist across an instance reboot. To disable cloud-init’s
network configuration capabilities, write a file
/etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
network: {config: disabled}

*** Info: The actual addresses will be nameservers rather than redirects to resolvconf, so no 8.8.8.8 etc in the yaml file.