how to change DNS server permanently on Ubuntu 20.04?

If your current DNS server is still your router (i.e. 192.168.1.1), although you have declared the desired nameservers in /etc/netplan/[network-mager].yaml or via the GUI of NetworkManager, there are at least two solutions to try:

  1. You may configure these settings using the already mentioned GUI:

    a) Choose a connection (from the Wired or Wireless tab) and click Edit. b) Click on the IPv4 Settings tab c) Choose 'Automatic (DHCP) addresses only' instead of just 'Automatic (DHCP)'. d) Enter the DNS servers in the “DNS servers” field, separated by spaces (e.g. 208.67.222.222 for OpenDNS). e) Click “Apply.”

Please, note that 'Automatic (DHCP) addresses only' means that the network you are connecting to uses a DHCP server to assign IP addresses but you want to assign DNS servers manually.

  1. or, if your DNS settigs are messed up by multiple programs updating it, you can use resolvconf:
sudo apt install resolvconf 
sudo systemctl enable --now resolvconf.service

then, edit /etc/resolvconf/resolv.conf.d/head and insert your desired nameservers as:

nameserver 8.8.8.8 
nameserver 8.8.4.4

Finally, to update /etc/resolv.conf run:

sudo resolvconf -u