X
Popular Searches

How to Set DNS Search Order in Ubuntu 18.04 using NetPlan

Ubuntu logo.

Prior to Ubuntu 18.04, DNS nameservers would traditionally be configured using the /etc/resolv.conf configuration file. For a long time, this file allowed quick and easy configuration of DNS nameservers, as can be seen in an example resolv.conf file below.

nameserver 1.1.1.1
nameserver 1.0.0.1

Starting in Ubuntu 18.04, network settings, including nameservers, are now controlled through the NetPlan interface. Even prior to 18.04, you were still allowed to make changes to the original resolv.conf, but this file was actually was controlled by NetworkManager and its configuration files, typically located in the /etc/network/interfaces directory.

So, what prompted this change to using the new NetPlan interface? Simplicity and flexibility. Using a human-readable YAML file makes configuring complex networking configurations far easier to do. In addition, debugging is quicker and errors are easier to track.

Changing NameServers in NetPlan

With this change in interfaces, how do we actually update the nameservers for an Ubuntu 18.04 system? Instead of using /etc/resolv.conf, navigate to /etc/netplan, and you may find a series *.yaml files there. If there are none yet defined, you should create one in the recommended naming format of, ##-name.yaml, such as 01-netconfig.yaml. NetPlan will load these files in the following directory order of precedence and then by the preceding number order.

  • /run/netplan/*.yaml
  • /etc/netplan/*.yaml
  • /lib/netplan/*.yaml

Find the Existing IP Configuration

Before changing the NetPlan network configuration, it is prudent to learn what the existing interface configuration is. NetPlan itself doesn’t monitor the active status, instead we use the ip command to display this.

user> ip a

The output should look similar to the below screenshot, but keep in mind that each network interface may be different depending on the system.

Output of the existing interface configuration.

Within this configuration, you will find your active interfaces. In this case, we are looking for the eth0 configuration for purposes of this tutorial.

Updating the Configuration

Either choose an existing YAML file to edit or create a new one and add the following configuration, modified as necessary for your configuration.

This configuration assumes that you are using a static IP and eth0 is your primary interface as learned from the above ip a command.

network:
  version: 2
  ethernets:
    eth0:
			addresses:
        - 10.10.10.2/24
        gateway4: 10.10.10.1
      nameservers:
        addresses:
        - 1.1.1.1
        - 1.0.0.1
        - 2606:4700:4700::1111
        - 2606:4700:4700::1001
        search: []

The nameservers that are in use here are the public Cloudflare nameservers, but use any that works best for you. Included are the IPv6 resolvers as well.

If you want to use DHCP, you can replace the static IP configuration with dhcp4: yes under the eth0 section.

Additionally, if you use DHCP, you will need to set an override for nameservers.

dhcp4-overrides:
	use-dns: false

You might notice that the addresses are listed in a list underneath addresses but it’s possible to also define these addresses in an array configuration as well.

addresses: [1.1.1.1, 1.0.0.1, "2606:4700:4700::1111", "2606:4700:4700::1001"]

Renderers

In some configurations, you might see a renderer configuration listed. There are two available types: NetworkManager and networkd. By default, networkd is used. The difference is that NetworkManager is the GUI manager used by Ubuntu, whereas networkd is used by Systemd. It’s unnecessary to define this, as the default configuration should work in most cases.

Testing the NameServer Change

After the configuration file has been created, run the following command to temporarily try the new settings and revert if there is a misconfiguration.

sudo netplan try
Screen show when you try to run the new settings.

If there is a failure, the configuration will automatically revert after 120 seconds. You can open up a second shell to test out any configurations while this is applying. If the settings work as expected, you can hit enter to keep those settings or revert if they do not. This allows you to modify the configurations until it works.

Applying the Change

Finally, after making sure the changes are accurate, the changes need to be applied. To do so, run the netplan apply command like so.

sudo netplan apply

If the interface is just switching nameservers, a reboot is generally unnecessary. If larger configuration changes are made, then a reboot of the system may be necessary to effect the changes.

Bringing Interfaces Up and Down

Previously, ifconfig was used to bring an interface up and down. As noted previously though, the ip command is taking the place of ifconfig. In the example below, eth0 is brought down and then back up.

ip link set eth0 down
ip link set eth0 up

If eth0 is your only interface, be careful not to bring the interface down without having a way to connect to the system remotely.

Debugging NetPlan

What happens when a configuration goes wrong? There are a few ways to explore the NetPlan configuration and figure out where a failure might have occurred. One way to determine if the correct configuration was actively applied is to verify the configuration files generated in /run/systemd/network. For example, you can navigate to that directory and find a matching *.network configuration file, then verify that the configuration matches what you expect.

An additional command that works well to verify that the generated config is correct is to use the following:

sudo netplan --debug generate

Keep in mind that the following screenshot may be different depending on your configuration.

Screen verifying that the generated config is correct.

Conclusion

NetPlan is a big change in the existing Ubuntu networking, but it is intended to simplify and create more flexible configurations. Using a combination of existing commands and the new NetPlan one’s we can quickly create and apply complex networking configurations.

With the introduction of a YAML-based configuration and updated abilities through the ip command, it is easier than ever to create complex configurations to handle even the most unique situations.

Adam Bertram Adam Bertram
Adam Bertram is a 20+ year veteran of IT and an experienced online business professional. He’s a consultant, Microsoft MVP, blogger, trainer, published author and content marketer for multiple technology companies. Catch up on Adam’s articles at adamtheautomator.com, connect on LinkedIn, or follow him on Twitter at @adbertram. Read Full Bio »

The above article may contain affiliate links, which help support CloudSavvy IT.