set dns ubuntu

Back in the day, users would directly edit the /etc/resolv.conf file to configure DNS on Ubuntu. These days, DNS configuration is handled by the systemd-resolved service.

You can still edit the conf files to set the DNS servers, but this change won’t be persistent. Instead, methods like netplan or nmcli are used to permanently change the DNS server.

Change via Network Settings

The traditional method works differently now, and there are multiple new ways to configure the DNS. I’ve seen a lot of users get confused due to these changes. 

If you’ve been out of the loop for a while or are new to Linux, I recommend following the GUI approach as it’s the easiest to understand.

  1. Search Settings and open the Network (Ethernet) or WiFi page.
    network settings ubuntu
  2. Click on the Settings cog beside the connection and switch to the IPv4 tab.
    ipv4 dns ubuntu
  3. Toggle off Automatic DNS here, enter the DNS server address, and press Apply.
    manual ipv4 dns ubuntu
  4. Restart the connection (toggle on-off) to apply the new config. You can press the Settings cog afterward to verify the change.
    dns server ubuntu

Edit Netplan Configuration

Netplan is the default network configuration utility on Ubuntu. To put things simply, you can edit its config file from the /etc/netplan directory to set custom parameters such as the IP Address or DNS server.

You’ll need the interface’s name to set the DNS, so check that first with

nmcli device status

Open the config file with a text editor.

sudo nano /etc/netplan/*.yaml

The config should look something like this. Identify the interface you’re trying to modify with the device name from earlier. Then, change the DNS values in the nameservers section, or add the section if it isn’t present.

network:
  version: 2
  renderer: NetworkManager
  ethernets:
    enp1s0:
      dhcp4: no
      addresses:
        - 192.168.122.50/24
      routes:
        - to: default
          via: 192.168.122.1
      nameservers:
          addresses: [8.8.8.8, 1.1.1.1]

Just make sure the number of prefix spaces is the same as shown here, as that’s necessary for the config to work.

Now, save the new config and apply the changes with

sudo netplan apply

You can verify the change using

resolvectl status

Use Nmcli to Set DNS

We used the NetworkManager GUI applet to set the DNS in the first method, but you can also do the same with the CLI tool (nmcli). To do this, list the interfaces first and note the connection name.

nmcli device status

Modify the specified connection like so

sudo nmcli con edit ‘Wired connection 1’

Use the set ipv4.dns commands to set the DNS servers.

set ipv4.dns 8.8.8.8, 1.1.1.1

Save the changes, quit the nmcli editor, and verify the new DNS servers with the following commands.

save persistent
quit
resolvectl status

Set Temporary DNS with resolvectl

If you only need to change your DNS server for the current session, you can use the resolvectl binary to make a temporary change. First, check the interface name with

nmcli device status

Now, specify the interface name and the DNS servers in the following manner.

resolvectl dns enp1s0 8.8.8.8 1.1.1.1

Finally, verify the change with

resolvectl status

Keep in mind that NetworkManager will overwrite this config when its restarted, so this change isn’t permanent.

Anup Thapa

Senior Writer

Anup Thapa is a Linux enthusiast with an extensive background in computer hardware and networking. His goal is to effectively communicate technical concepts in a simplified form understandable by new Linux users. To this end, he mainly writes beginner-friendly tutorials and troubleshooting guides. Outside of work, he enjoys reading up on a range of topics, traveling, working out, and MOBAs.