Ubuntu: How to add a DNS server (5 approaches)

Updated: January 29, 2024 By: Guest Contributor Post a comment

Introduction

Domain Name System (DNS) is a critical part of Internet infrastructure, transforming human-readable domain names into IP addresses. In Ubuntu, configuring DNS settings is a common task for network administrators and users who desire to improve their network configurations. In this tutorial, we’ll discuss different methods to add a DNS server on an Ubuntu system, from simple GUI changes to editing configuration files, and even using command-line tools for dynamic updates. We will cover methods suited for desktop users as well as server administrators, with code examples that illustrate each step in the process.

Prerequisites

  • An Ubuntu machine (Desktop or Server)
  • Root/sudo access
  • Basic knowledge of network configuration and terminal usage

Method 1: Using the GUI (Network Manager)

For Ubuntu Desktop users, adding a DNS server can be achieved easily through the Network Manager GUI. Follow these steps to add a DNS server:

  1. Click on the network icon in the top right corner of your screen.
  2. Navigate to ‘Wired Connected’ or ‘Wi-Fi Connected > Select Network Settings’.
  3. Choose the network you wish to configure and click on the gear icon.
  4. Go to the ‘IPv4’ or ‘IPv6’ tab depending on your network configuration.
  5. Disable ‘Automatic DNS’ and add your desired DNS server IP address in the ‘DNS’ field.
  6. Click ‘Apply’ to save the changes, then reconnect to the network for changes to take effect.

For example, to add Google’s DNS server, which is ‘8.8.8.8’, enter this IP address into the ‘DNS’ field.

Method 2: Editing /etc/resolv.conf Manually

Editing the resolv.conf file was a traditional method to set DNS servers. We’ll first secure the file against automatic overwriting and then add our desired DNS servers.

sudo chattr +i /etc/resolv.conf
sudo nano /etc/resolv.conf

Add the following lines:

nameserver 8.8.8.8
nameserver 8.8.4.4

After saving and closing the file, use the dig or nslookup command to test the new DNS configuration:

dig example.com

The output should show the query passing through the newly configured DNS server.

Method 3: Using systemd-resolved

In newer versions of Ubuntu, DNS settings are managed by ‘systemd-resolved’. To use this, disable the symlink of /etc/resolv.conf and create a custom configuration file.

sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
sudo nano /etc/systemd/resolved.conf

Add the following lines under the [Resolve] section:

DNS=8.8.8.8 8.8.4.4
FallbackDNS=1.1.1.1

Restart the systemd-resolved service:

sudo systemctl restart systemd-resolved

Verify the DNS configuration:

systemd-resolve --status

The output will show the DNS servers that are currently being used.

Method 4: Using the nmcli Tool

The Network Manager command-line tool, nmcli, is useful for configuring network settings in scripts or when the GUI is not available, such as on a server without a desktop environment. Use the following command:

nmcli con mod 'connection-name' ipv4.dns '8.8.8.8 8.8.4.4' ipv4.dns-priority -1

Replace ‘connection-name’ with the actual name of your network connection. Apply changes:

nmcli con up 'connection-name'

This will update the DNS servers for the specified connection and apply them immediately.

Method 5: Using Netplan (For Ubuntu Server)

Ubuntu Server 17.10 and later versions use ‘netplan’ for network configuration. Locate your netplan configuration files in ‘/etc/netplan/’, edit the appropriate file with your preferred editor:

sudo nano /etc/netplan/01-netcfg.yaml

Add or modify the DNS servers under the appropriate ethernet or wireless interface:

network:
    version: 2
    renderer: networkd
    ethernets:
        enp0s3:
            dhcp4: yes
            nameservers:
                addresses: [8.8.8.8, 8.8.4.4]

Apply the changes with:

sudo netplan apply

This will set the specified DNS servers for the defined interface.

Conclusion

Through this tutorial, we’ve explored multiple methods for adding DNS servers on Ubuntu. Regardless of your familiarity with the command line or preference for a GUI interface, these examples have provided a stepping stone towards customizing DNS settings on your Ubuntu systems.