WEIGEL FORUM
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Login

    Debian || script to change hostname,IP config and sourcelist

    Linux
    1
    2
    18
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • DerMeldosD
      DerMeldos
      last edited by

      #!/bin/bash
      
      # Function to replace text in a file
      replace_in_file() {
          local file="$1"
          local search="$2"
          local replace="$3"
          sed -i "s|$search|$replace|g" "$file"
      }
      
      # Initialize variables to store the changes
      network_changes=""
      sources_list_changed=false
      hostname_changes=""
      
      # 1. Edit network configuration
      echo "Please enter the new IP address for ens18:"
      read new_ip
      echo "Please enter the new gateway address:"
      read new_gateway
      echo "Please enter the new nameserver address:"
      read new_nameserver
      
      network_changes+="iface ens18 inet dhcp -> iface ens18 inet static"$'\n'
      network_changes+="address xxx -> address $new_ip"$'\n'
      network_changes+="gateway xxx -> gateway $new_gateway"$'\n'
      network_changes+="nameserver xxx -> nameserver $new_nameserver"$'\n'
      
      # Backup the original interfaces file
      cp /etc/network/interfaces /etc/network/interfaces.bak
      
      # Update the network configuration
      cat > /etc/network/interfaces << EOF
      # This file describes the network interfaces available on your system
      # and how to activate them. For more information, see interfaces(5).
      
      source /etc/network/interfaces.d/*
      
      # The loopback network interface
      auto lo
      iface lo inet loopback
      
      # The primary network interface
      allow-hotplug ens18
      iface ens18 inet static
              address $new_ip
              gateway $new_gateway
              nameserver $new_nameserver
      EOF
      
      # 2. Edit sources.list
      sources_list_changes="# deb cdrom:[Debian GNU/Linux 11.2.0 _Bullseye_ - Official amd64 DVD Binary-1 20211218-11:13]/ bullseye contrib main"
      sources_list_changes+="\ndeb [trusted=yes] http://deb.debian.org/debian/ bullseye main"
      sources_list_changes+="\ndeb-src [trusted=yes] http://deb.debian.org/debian/ bullseye main"
      sources_list_changes+="\ndeb [trusted=yes] http://security.debian.org/debian-security bullseye-security main contrib"
      sources_list_changes+="\ndeb-src [trusted=yes] http://security.debian.org/debian-security bullseye-security main contrib"
      sources_list_changes+="\ndeb [trusted=yes] http://deb.debian.org/debian/ bullseye-updates main contrib"
      sources_list_changes+="\ndeb-src [trusted=yes] http://deb.debian.org/debian/ bullseye-updates main contrib"
      
      # Check if changes were made to the sources.list
      if ! cmp -s /etc/apt/sources.list <(echo "$sources_list_changes"); then
          sources_list_changed=true
      fi
      
      # Apply the changes to the sources.list
      echo "$sources_list_changes" > /etc/apt/sources.list
      
      # 3. Edit hostname and hosts file
      echo "Please enter the new hostname for the machine (e.g., server1):"
      read new_hostname
      
      hostname_changes+="New hostname: $new_hostname"$'\n'
      hostnamectl set-hostname "$new_hostname"
      replace_in_file "/etc/hosts" "127.0.1.1.*" "127.0.1.1\t$new_hostname"
      
      # Summary of changes
      echo -e "Summary of changes:\n"
      echo -e "Network configuration changes:"
      echo -e "$network_changes"
      
      if $sources_list_changed; then
          echo "Sources.list changed."
      fi
      
      echo -e "Hostname change:"
      echo -e "$hostname_changes"
      
      # Confirmation prompt
      echo -n "Do you want to reboot the system now? (Y/N): "
      read reboot_choice
      
      if [[ $reboot_choice =~ ^[Yy]$ ]]; then
          echo "Rebooting the system..."
          reboot
      else
          echo "Changes have been applied. You may need to reboot the system for all changes to take effect."
      fi
      
      
      1 Reply Last reply Reply Quote 0
      • DerMeldosD
        DerMeldos
        last edited by

        Script migrated to Github to add functionality more easily.

        https://github.com/DerMeldos/WEIGEL-Script-Collection/blob/main/vm-config-edit.sh

        1 Reply Last reply Reply Quote 0
        • First post
          Last post
        Powered by NodeBB | Contributors