This article is relevant for Ubuntu versions Ubuntu 20.04 or later using /etc/netplan configuration.

The configuration applicable can be done with editor nano in file is in file /etc/netplan/50-cloud-init.yaml:

sudo nano /etc/netplan/50-cloud-init.yaml

The configuration for WiFi is in section wifis: and the configuration for cable LAN is in section ethernets:

The indentation needs to be correct for the .yaml to work. Example to set the ethernet connection to a static IP 192.168.1.10 connected to router 192.168.1.1:

network:
    ethernets:
        eth0:
          addresses:
          - 192.168.1.10/24
        gateway4: 192.168.1.1
          nameservers:
          addresses: [192.168.1.1]
          optional: true

Example configuration for WiFi obtaining the IP address from DHCP:

network:
wifis: wlan0: dhcp4: true optional: true access-points: "home network": password: "123456789"

To apply the netplan changes, enter:

sudo netplan apply

Alternatively, if you run into some issues use:

sudo netplan --debug apply