Ubuntu Netplan

Fo decades ifup and ifdown were the gold standard to configure network interfaces in Unix and Linux systems.
Ubuntu 18.04 LTS (Canonical) in 2018 migrated from ifupdown to cloud-init and netplan, which requires a different skill set to manage.

cloud-init

cloud-init setups network in a mysterious fashion that uses netplan network configuration.

Optional: completely disable cloud.init:

delete all .yaml files in /etc/netplan
sudo nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
add one line to the file:
network: {config: disable}

Mask out the following cloud-init services (ghosts buster):

sudo systemctl mask cloud-init.service
sudo systemctl mask cloud-init-local.service
sudo systemctl mask cloud-config.service
sudo systemctl mask cloud-final.service

sudo cloud-init.clean


netplan - DHCP

delete /etc/network/interfaces that may be left behind by previous Ubuntu configurations.

only 1 yaml file is needed in /etc/netplan, other files will cause problems:
delete or comment out all the lines in these trouble makers.

Very important: determine the logical name of your installed Ethernet card, in the past, it was eth0, but now it is not.
sudo lshw -class network | grep logical   (remember the logical name of your Ethernet chip).

yaml is extremely fussy about indentation. Use spaces, don't use tab.

sudo nano/etc/netplan/config.yaml

network:
  version: 2
  ethernets:
    eth0:     (use the logical name of your network card)
      dhcp4: true
    nameservers:
      addresses:
        - 8.8.8.8
        - 8.8.4.4


Omit the namservers block if you like to use your ISP provided DNS (nameservers)

After saving the config.yaml file,
sudo netplan generate
sudo netplan apply
(optional) sudo reboot



netplan - Static IP

delete /etc/network/interfaces that may be left behind by previous Ubuntu configurations.

only 1 yaml file is needed in /etc/netplan, other files will cause problems:
delete or comment out all the lines in these trouble makers.

Very important: determine the logical name of your installed Ethernet card, in the past, it was etho, but now it is not.
sudo lshw -class network | grep logical   (remember the logical name of your Ethernet chip).

yaml is extremely fussy about indentation. Use spaces, don't use tab.

Your IP address and default gateway will be different. They are highlighted in red.

sudo nano/etc/netplan/config.yaml

network:
  version: 2
  ethernets:
    eth0:  (use the logical name of your network card)
      addresses:
        - 192.168.1.4/24
      nameservers:  (vertically align with addresses)
        addresses:
          - 8.8.8.8
          - 2001:4860:4860::8888   (Google's public IPv6 DNS)
      routes:   (vertically align with addresses)
        - to: default
          via: 192.168.1.254    (default gateway-router, yours may be different)


After saving the config.yaml
sudo netplan generate
sudo netplan apply
(optional) sudo reboot



Check network setup:
ip address
resolvectl



Notes:

cat /etc/resolv.conf  shows nameserver 127.0.0.53 (it is a stub resolver), don't panic and edit that file or remove the symlink. It is there by design to point to the real DNS servers via magic. Leave it alone.

To find out where the real DNS servers are, issue this command: resolvectl

Instead of using Google's public DNS servers, one can choose Cloudfare, Quad9 or private DNS servers.


Troubleshooting:

if the above /etc/resolv.conf symbolic link is accidentally broken, restore it:
sudo ln -s  /run/systemd/resolve/stub-resolv.conf   /etc/resolv.conf

sometimes the resolvconf package can cause conflict with netplan, delete the package, to calm things down:
sudo apt purge resolvconf