Short Version
# Configure networking
sudo vim /etc/netplan/*.yaml
### Sample content ###
network:
version: 2
renderer: networkd
ethernets:
ens192:
dhcp4: false
addresses: [10.10.10.99/24]
gateway4: 10.10.10.1
nameservers:
addresses: [8.8.8.8,1.1.1.1]
#######################
sudo netplan apply
# Change the hostname
hostname=linux03
sudo hostnamectl set-hostname $hostname
# Update the /etc/hosts file
sed -i '/127.0.1.1/ c\127.0.1.1 '"$hostname"'' /etc/hosts
# Update cloud.cfg
sed -i '/preserve_hostname:/ c\preserve_hostname: true' /etc/cloud/cloud.cfg
Long Version
# Checking for the network config file
#root@webserver01:/home/rambo# ls /etc/netplan
#01-network-manager-all.yaml
# view the file's contents
#root@webserver01:/home/rambo# cat /etc/netplan/*.yaml
#------------------------------------------------------------
# Let NetworkManager manage all devices on this system
#network:
# version: 2
# renderer: NetworkManager
################### START SETTING STATIC IP ###################
# make backups of the original yaml file(s)
for i in /etc/netplan/*.yaml ; do sudo mv "$i" "$i.bak" ; done
# Change the netplan contents
gateway="10.10.10.1"
address=[10.10.10.5/24]
dns=[8.8.8.8,4.4.2.2]
primaryInterface=$(ip addr show | awk '/inet.*brd/{print $NF; exit}')
sudo bash -c "cat << EOF > /etc/netplan/manualconfig.yaml
network:
ethernets:
$primaryInterface:
addresses: $address
dhcp4: false
gateway4: $gateway
nameservers:
addresses: $dns
renderer: networkd
version: 2
EOF"
# Apply the config
sudo netplan apply
# sudo systemctl restart network-manager # optional
# sudo systemctl restart system-networkd # optional: Ubuntu server version
################### DON'T WASTE TIME WITH THIS ###################
# Update: this instruction doesn't work as such settings will not survive a reboot. It's saved here for gaiety (gay) purposes
# Install net-tools
sudo apt install net-tools -y
# Check current networking config
ifconfig
# Set Static Variables
network=192.168.1.0
netMask=255.255.255.0
defaultInterface=eno1
staticIP=192.168.0.5
gateway=192.168.0.1
convertNetMaskToCidr() {
# function obtained from https://stackoverflow.com/questions/50413579/bash-convert-netmask-in-cidr-notation/50414560
bits=0
for octet in $(echo $1| sed 's/\./ /g'); do
binbits=$(echo "obase=2; ibase=10; ${octet}"| bc | sed 's/0//g')
let bits+=${#binbits}
done
echo "/${bits}"
}
cidr=$(convertNetMaskToCidr $netMask)
# Configure Interface
sudo ifconfig $defaultInterface $staticIP netmask $netmask
sudo ip route add $network$cidr dev $defaultInterface
sudo route add default gw $gateway $defaultInterface
Note: this supersedes the instructions for Ubuntu 18.04
Categories: