ubuntu 20 uses netplan as default ip configuration utility

usefule netplan commands:
- netplan get #shows the actual configuration
- /etc/netplan #in this configuration directory the netplan yaml file is located
- netplan try #test the new configuration
- netplan apply #apply the configuration

#a sample bash script to set some new parameters comes here:

changeIP.sh:
#!/bin/bash

configfile="/etc/netplan/00-installer-config.yaml"

# make a backup
cp $configfile $configfile.save.`date +%Y%m%d%H%M`
# Changes dhcp from 'yes' to 'no'
sed -i "s/dhcp4: yes/dhcp4: no/g" $configfile
# Retrieves the NIC information
nic=`ifconfig | awk 'NR==1{print $1}'`
# Ask for input on network configuration
read -p "Enter the static IP of the server (example 192.168.2.20/24): " staticip
read -p "Enter the IP of your gateway: " gatewayip
read -p "Enter the IP of your nameservers (seperated by a coma if > 1): " nameserversip
echo
cat > $configfile <<EOF
network:
version: 2
ethernets:
$nic
addresses:
- $staticip
gateway4: $gatewayip
nameservers:
addresses: [$nameserversip]
EOF
sudo netplan apply
echo ">>> new settings are now activated"
echo

computer2know :: thank you for your visit :: have a nice day :: © 2024