Change DNS Server given during Ubuntu 18.04 installation

Edit your netplan configuration file and remove the old dns server names and add new ones. Edit the file with sudo nano /etc/netplan/01-netcfg.yaml and yours should be similar to the example below:

network:
    version:2 
    renderer: networkd
    ethernets:
         enp0s3:
             dhcp4: true
             nameservers:
                  search: [mydomain, otherdomain]
                  addresses: [10.10.10.1, 1.1.1.1]

The line of interest is the one that says addresses under the settings nameserver. It might also be written like so:

nameservers:
     search: 
         - mydomain
         - otherdomain
     addresses:
         - "10.10.10.1"
         - "1.1.1.1"

Change the address there to the one you desire. Make sure to observe the indentations as ther are. Now after thatv save the file and aplly the changes:

sudo netplan --debug apply

This is a script based on an answer showed on datawookie.

#!/bin/bash        
# Installing resolvconf package
    sudo apt install resolvconf
# Detecting a magic sentence to applies changes... or not
    grep "Make edits to /etc/resolvconf/resolv.conf.d/head." /etc/resolvconf/resolv.conf.d/head &> /dev/null
    if [ ! $? -eq 0 ]
    then
     echo '# Make edits to /etc/resolvconf/resolv.conf.d/head.' | sudo tee --append /etc/resolvconf/resolv.conf.d/head &> /dev/null
     echo 'nameserver 8.8.8.8' | sudo tee --append /etc/resolvconf/resolv.conf.d/head &> /dev/null
     echo 'nameserver 8.8.4.4' | sudo tee --append /etc/resolvconf/resolv.conf.d/head &> /dev/null
    fi
# restarting daemon...
    sudo service resolvconf restart
# Flushing former DNS caches (by security)
    sudo systemd-resolve --flush-caches
    sudo systemctl restart systemd-resolved.service
# and check if DNS changes was done... or not ;)
    nslookup askubuntu.com | grep "Server:"
    read $r