Last active
August 1, 2024 02:24
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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: true | |
dhcp4-overrides: | |
use-routes: false | |
EOL | |
sudo chmod 600 /etc/netplan/01-dt-setup.yaml | |
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
#10 maybe handy
#28 #29 just wrong? How are you dealing with your subnet mask / bit count??
I am on phone so might have missed something?