Configuring Network Settings on Ubuntu Using a Bash Script

Are you looking to streamline your network configuration process on Ubuntu? Look no further! We’ve got a simple yet powerful bash script that will help you manage your network settings effortlessly, designed specifically for Ubuntu systems. This script will find out the version you are using and write the .yaml file accordingly.

Why Use This Script?

Setting up network configurations manually can be time-consuming and prone to errors. Our bash script automates the process, ensuring accuracy and efficiency. Whether you need to assign static IP addresses, update DNS servers, or modify gateway settings, this script has got you covered.

Features:

Automatically detects network interface from the default gateway.
Displays current network information including hostname, IP address, gateway, and network interface.
Provides the option to change IP address to static, along with prompts for new IP address, network mask, and gateway.
Allows customization of DNS servers, with the option to use primary and secondary DNS provided or specify new ones.
Generates and applies network configuration changes to the appropriate YAML file.
Simplifies the application of network settings with a single command.
How to Use:

To use this script, simply open any text editor and Copy and paste the provided bash script.

Save it to a file (e.g., network_config.sh) using any text editor like nano … , make it executable.

chmod +x network_config.sh

and run it

./network_config.sh

It will handle the rest

Follow the prompts to make changes to your network settings as needed.
Sit back and relax as the script updates your network configuration seamlessly.

Disclaimer:

This script is intended for use on Ubuntu systems. Use caution when modifying network settings, as incorrect configurations can disrupt network connectivity.

#!/bin/bash
clear
# Function to check if user has sudo rights
check_sudo() {
    if sudo -n true 2 >/dev/null; then
        echo "User has sudo rights."
        return 0
    else
        echo "User does not have sudo rights. Please run the script with sudo."
        return 1
    fi
}

# Function to get Ubuntu version
get_ubuntu_version() {
    if [ -f /etc/os-release ]; then
        . /etc/os-release
        if [ -n "$VERSION_ID" ]; then
            UBUNTU_VERSION="$VERSION_ID"
        else
            UBUNTU_VERSION="Unknown"
        fi
    else
        UBUNTU_VERSION="Unknown"
    fi
}

# Check if the user has sudo rights
if ! check_sudo; then
    exit 1
fi

# Get Ubuntu version
get_ubuntu_version

# Display Ubuntu version
echo "Ubuntu version: $UBUNTU_VERSION"

# If the version is up to 20.xx, execute the network configuration script
#if [[ "$UBUNTU_VERSION" =~ ^(1[6-9].|20.) ]]; then
# Display the network configuration script author and description
echo -e "\nNetwork Configuration Script"
echo "This script was written by Inder Kochar - Alfa Intelli Group, India / Australia. For more information, check out https://inder.work"

# Execute the network configuration script
# Display general network information
echo "=== Current Network Information ==="
hostname=$(hostname)
echo "Hostname: $hostname"
ip_address=$(hostname -I)
echo "IP Address: $ip_address"
gateway=$(ip route | awk '/default/ {print $3}')
echo "Gateway: $gateway"

# Get network interface
network_interface=$(ip route | awk '/default/ {print $5}')
echo "Network Interface: $network_interface"

# Store network information in memory variables
network_info="$ip_address"
network_mask=$(ip -o -4 addr show dev $network_interface | awk '$3 == "inet" {print $4}')
gateway_info="$gateway"

# Store default DNS information
dns_primary="1.1.1.1"
dns_secondary="9.9.9.9"

# Ask user if they want to change IP address to static
read -p "Do you want to change IP address to static (Y/N)? " change_ip
change_ip=${change_ip^^} # Convert to uppercase

if [ "$change_ip" = "Y" ]; then
   echo -e "\nCurrent network settings:"
   echo "IP Address: $ip_address"
   echo "Network Mask: $network_mask"
   echo "Gateway: $gateway"

# Prompt for new network settings
   read -p "Enter new IP address (e.g., 192.168.1.4): " new_ip_address
   read -p "Enter network mask (e.g., 24 for /24 not 255.255.255.x): " new_network_mask
   read -p "Enter gateway (e.g., 192.168.1.1): " new_gateway

   echo "Default DNS Addresses:"
   echo "Primary DNS: $dns_primary"
   echo "Secondary DNS: $dns_secondary"
# Ask user if they want to change DNS
   read -p "Do you want to use any other DNS (Y/N)? " change_dns
   change_dns=${change_dns^^} # Convert to uppercase

    if [ "$change_dns" = "Y" ]; then
    # Prompt for new DNS servers
      read -p "Enter new primary DNS: " new_dns_primary
      read -p "Enter new secondary DNS: " new_dns_secondary
      dns_primary="$new_dns_primary"
      dns_secondary="$new_dns_secondary"
    fi

# Update network settings in YAML file
    if [[ "$UBUNTU_VERSION" =~ ^(22\..*|[3-9][0-9]\..*) ]]; then
# Ubuntu version is 22.xx or higher
#cat <<EOF | sudo tee /etc/netplan/00-installer-config.yaml > /dev/null
    cat <<EOF | sudo tee /etc/netplan/00-installer-config.yaml > /dev/null
network:
  version: 2
  renderer: networkd
  ethernets:
    $network_interface:
      dhcp4: no
      addresses: [$new_ip_address/$new_network_mask]
      routes:
        - to: default
          via: $new_gateway
      nameservers:
        addresses: [$dns_primary, $dns_secondary]
EOF
    else
# Ubuntu version is up to 20.xx
    cat <<EOF | sudo tee /etc/netplan/00-installer-config.yaml > /dev/null
network:
  version: 2
  renderer: networkd
  ethernets:
    $network_interface:
      dhcp4: no
      addresses: [$new_ip_address/$new_network_mask]
      gateway4: $new_gateway
      nameservers:
        addresses: [$dns_primary, $dns_secondary]
EOF
    fi

# Apply the network settings
    sudo netplan apply

echo "Network settings updated successfully."
    elif [ "$change_ip" = "N" ]; then
    echo "No changes made to network settings."
    else
        echo "Invalid input. Please enter Y or N."
    fi
fi

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More post