Ubuntu: migrate from Ifupdown to Netplan. Step by step.
In Ubuntu 18.04 old good ifupdown is considered obsolete. And you need to migrate your servers to systemd-networkd + netplan. Lots of internet resources describe how to revert back to ifupdown but I am hailing the future! So this manual covers 2 situations, static networking & DHCP. Both are IPv4.
systemctl disable networking.service
apt-get remove ifupdown -y
apt-get install netplan.io libnet-cidr-perl -y
systemctl enable systemd-networkd
DEV=`facter networking.primary`
rm /etc/netplan/*
grep dhcp /etc/network/interfaces
Now follow either DHCP or Static part.
######## DHCP START
echo "network:
version: 2
renderer: networkd
ethernets:
$DEV:
nameservers:
addresses: [8.8.8.8]
dhcp4: yes" | tee /etc/netplan/01-netcfg.yaml
######## DHCP END
###### STATIC START
GW=$( ip ro get 8.8.8.8 | grep -P '(?<=via )[\d\.]+(?= )' -o )
NETMASK=$( facter networking.netmask )
CIDR=$( perl -e 'use Net::CIDR; print \
Net::CIDR::addrandmask2cidr("'$IP'", "'$NETMASK'")."\n"; ' )
IP=$( facter networking.ip )
echo "network:
version: 2
renderer: networkd
ethernets:
$DEV:
addresses:
- $IP/24
gateway4: $GW
nameservers:
addresses: [8.8.8.8]" | tee /etc/netplan/01-netcfg.yaml
###### STATIC END
Then apply new conf & see its status in networkctl
netplan apply
systemctl stop networking.service
sleep 2; networkctl list $DEV
And reboot the server!