Skip to content

Instantly share code, notes, and snippets.

@DennisTT
Last active November 16, 2022 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DennisTT/74cdb9e4141a3717e4c93368e71257dd to your computer and use it in GitHub Desktop.
Save DennisTT/74cdb9e4141a3717e4c93368e71257dd to your computer and use it in GitHub Desktop.
Script to change hostname and write network configuration for a Ubuntu 22.04 VM guest on an OVH dedicated server
#!/bin/bash
# Setup OVH VM guest networking on Ubuntu 22.04
# Change hostname
read -p "Enter new fully-qualified domain name: " fqdn
hostname=${fqdn%%.*}
echo "Changing FQDN to $fqdn, hostname to $hostname"
sudo hostnamectl set-hostname $hostname
sudo sed -i "s/\(^127\.0\.1\.1.*$\)/127.0.1.1\t$fqdn $hostname/g" /etc/hosts
# Configure IP addresses
read -p "Enter IPv4 address: " ipv4addr
read -p "Enter IPv4 gateway (xxx.yyy.zzz.254 of the host): " ipv4gateway
read -p "Enter IPv6 address: " ipv6addr
read -p "Enter IPv6 gateway: (gggg:hhhh:iiii:jjff:ff:ff:ff:ff of the host) " ipv6gateway
echo "Writing new IP address configurations"
sudo bash -c "cat > /etc/netplan/01-dt-setup.yaml" <<EOL
network:
version: 2
ethernets:
eth0:
dhcp4: false
optional: false
addresses:
- $ipv4addr/32
- $ipv6addr/64
routes:
- to: default
via: $ipv4gateway
on-link: true
- to: default
via: $ipv6gateway
on-link: true
nameservers:
addresses:
- 8.8.8.8
- 1.1.1.1
eth1:
dhcp4-overrides:
use-routes: false
EOL
echo "Removing existing cloud-init configuration"
sudo rm /etc/netplan/50-cloud-init.yaml
sudo bash -c "echo \"network: {config: disabled}\" > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg"
echo "Applying network config"
sudo netplan apply
# Resize filesystem
echo "Resizing partition"
sudo growpart /dev/sda 2
echo "Resizing filesystem"
sudo resize2fs /dev/sda2
echo "Done - suggested to reboot"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment