For decades ifup and ifdown were the 'gold standard' to configure network interfaces in Unix and Linux systems.
Ubuntu (Canonical) migrated from ifupdown to cloud-init and netplan, which requires a different skill set to manage them.
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: disabled}
Mask four cloud-init services:
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
Clean up the mess:
sudo cloud-init clean
DHCP (netplan)
sudo nano /etc/netplan/config.yaml
(only 1 yaml file is needed in /etc/netplan, other files in /etc/netplan will cause problems, delete/comment out all the lines in these trouble files).
(yaml requires extremely fuzzy and careful indentation, use spaces, don't use tab)
network:
version: 2
ethernets:
eth0:
dhcp4: true
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
If you want to use the DNS servers provided by your ISP/DHCP server, omit the nameservers section (4 lines).
After saving the config.yaml file,
$sudo netplan generate
$sudo netplan apply
(optional) $sudo reboot
Static IP (netplan)
-delete /etc/network/interfaces that may be left behind by previous Ubuntu configurations.
(only 1 yaml file is needed in /etc/netplan, other files in /etc/netplan will cause problems, delete/comment out all the lines in these trouble files).
-Very Important: determine the logical name of your installed Ethernet card, it may not be eth0:
$sudo lshw -class network | grep logical
(yaml requires extremely fuzzy and careful indentation, use spaces, don't use tab)
sudo nano /etc/netplan/config.yaml
#Note your IP address and default gateway will be different
network:
version: 2 (indent 2 spaces)
ethernets:
eth0: (indent 2 spaces, use your logical name instead of eth0)
addresses: (indent 2 spaces)
- 192.168.1.4/24 (indent 2 spaces)
nameservers: (vertical align with addresses)
addresses: (indent 2 spaces)
- 8.8.8.8 (indent 2 spaces)
- 2001:4860:4860::8888 (Google DNS IPv6)
routes: (vertical align with address)
- to: default (ident 2 spaces)
via: 192.168.1.254 (default gateway-router)
After saving config.yaml:
$sudo netplan generate
$sudo netplan apply
(optional) $sudo reboot
Check network setup:
$ip address (similar to ifconfig)
$resolvectl (similar to nslookup or dig)
Notes:
-$cat /etc/resolv.conf will show nameserver 127.0.0.53 (it is a stub resolver). Don't edit that file or remove the symlink, it is there by design to point to the real DNS servers, magically revealed/disclosed by the resolvectl command.
-Instead of using Google public DNS nameservers, one can choose Cloudfare, Quad9 or private DNS servers.
Troubleshooting
-If the symbolic link is accidentally broken, restore it:
$sudo ln -s /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf
-Sometimes the resolvconf package causes conflicts with netplan, delete resolvconf package to calm things down:
$sudo apt purge resolvconf