Skip to content

Instantly share code, notes, and snippets.

@recklessop
Created December 4, 2018 16:58
Show Gist options
  • Save recklessop/f28ddb4c39e1261ff3d0bfaf017f3b18 to your computer and use it in GitHub Desktop.
Save recklessop/f28ddb4c39e1261ff3d0bfaf017f3b18 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Creates a backup
cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bk_`date +%Y%m%d%H%M`
# Changes dhcp from 'yes' to 'no'
sed -i "s/dhcp4: yes/dhcp4: no/g" /etc/netplan/01-netcfg.yaml
# 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 in CIDR notation: " staticip
read -p "Enter the IP of your gateway: " gatewayip
read -p "Enter the IP of preferred nameservers (seperated by a coma if more than one): " nameserversip
echo
cat > /etc/netplan/01-netcfg.yaml <<EOF
network:
version: 2
renderer: networkd
ethernets:
$nic
addresses:
- $staticip
gateway4: $gatewayip
nameservers:
addresses: [$nameserversip]
EOF
sudo netplan apply
echo "==========================="
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment