Jan 12, 2020
1 Min read

DNS Settings with Netplan

Configuring DNS server IP address in Ubuntu is quick and easy with Netplan.

Netplan is the way to configure network interfaces in newer versions of Ubuntu (after 16.04). This replaces the older method of configuring interfaces using the /etc/network/interfaces file.

We are going to use Netplan to configure the DNS IP address in our Ubuntu 18.04 server. Netplan uses the /etc/netplan/50-cloud-init.yaml file to store its configurations. After the initial boot this file looks like below.

$ cat /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.
# 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}
network:
    version: 2
    ethernets:
        ens3:
            addresses:
            - 10.0.0.2/29
            match:
                macaddress: fa:16:3e:23:6c:84
            mtu: 1450
            routes:
            -   to: 0.0.0.0/0
                via: 10.0.0.1
            set-name: ens3

We will edit this file with vim.

$ sudo vim /etc/netplan/50-cloud-init.yaml

Insert below lines after mtu parameter

            nameservers:
                addresses: [8.8.8.8]

Although we are adding only one nameserver here, you can add multiple DNS server addresses using commas.

            nameservers:
                addresses: [8.8.8.8, 1.1.1.1]

Then apply the changes.

$ sudo netplan apply

If everything goes well, now you will be able to resolve URLs.

$ nslookup google.lk

The complete file is below as a reference.

$ cat /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.
# 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}
network:
    version: 2
    ethernets:
        ens3:
            addresses:
            - 10.0.0.2/29
            match:
                macaddress: fa:16:3e:23:6c:84
            mtu: 1450
            nameservers:
                addresses: [8.8.8.8]
            routes:
            -   to: 0.0.0.0/0
                via: 10.0.0.1
            set-name: ens3