Since 18.04, Ubuntu started utilizing systemd-resolved for name resolution, which means that setting DNS servers in /etc/resolv.conf will no longer work. In a modern Ubuntu versions, such changes needs to be done in Netplan configuration(which delegates name resolution to systemd-resolved.resolv.conf).

nano /etc/netplan/my-dns-config.yaml

Example: Assuming that we want to set public Cloudflare DNS for both IPv4 and IPv6 for interface ens3:

# This is my DNS config
network:
  ethernets:
    ens3:
      dhcp4: true
      dhcp4-overrides:
        use-dns: false
      nameservers:
        addresses: [1.1.1.1, 1.0.0.1, 2606:4700:4700::1111, 2606:4700:4700::1001]
  version: 2

Configuration test:

netplan --debug apply

Apply:

netplan apply

Check DNS settings with systemd-resolved:

systemd-resolve --status