Ce script configure automatiquement l'interface réseau Host-Only du serveur SRV-LIN1-02 avec une IP statique, configure la passerelle et résout les noms de domaine avec le DNS. Il met également à jour le fichier /etc/hosts avec les informations correctes du serveur.
⚙️ Caractéristiques du script
Renomme le serveur en SRV-LIN1-02.
Configure l'interface réseau Host-Only avec une IP statique.
Définit la passerelle comme.
Met à jour le fichier resolv.conf avec le serveur DNS.
Modifie le fichier /etc/hosts avec l'IP correcte pour SRV-LIN1-02.
Affiche un récapitulatif de la configuration appliquée et demande confirmation avant de redémarrer le service réseau.
🚀 Commande d'exécution rapide
Pour exécuter ce script de manière automatisée sur votre serveur Debian, utilisez la commande suivante. Cela installera curl si nécessaire, puis téléchargera et exécutera le script en une seule étape :
#!/bin/bash
############################################################################################################
# Script Name : cpnv_lin1_srv02_network_setup.sh
# Author : Rui Monteiro (rui.monteiro@eduvaud.ch)
# Created : 2024-09-28
# Last Updated : 2024-11-08
#
# Version : 1.4
#
# Description : This script automates the network configuration of SRV-LIN1-02, including setting up
# the Host-Only network interface with a static IP, configuring the gateway, and setting
# up DNS resolution using resolv.conf. It also renames the machine to SRV-LIN1-02.
#
# Features : - Configures the Host-Only interface with a static IP.
# - Sets the gateway and DNS nameservers.
# - Configures resolv.conf to ensure proper DNS resolution.
# - Renames the machine to SRV-LIN1-02.
#
# Usage : Run the script with sudo privileges:
# sudo bash -c "$(curl -fsSL https://gitlab.com/Ruimmp/cpnv-es/-/raw/LIN1/1.%20Param%C3%A9trage%20du%20R%C3%A9seau%20et%20Services%20Syst%C3%A8mes/cpnv_lin1_srv02_network_setup.sh)"
# The script will automatically configure the network interfaces and services.
# After completion, reconnect using the new IP address assigned to SRV-LIN1-02.
#
# System Tested : Debian 12.6.4
#
# Prerequisites : - Debian 12.6.4 or compatible Debian-based distribution.
# - Sudo privileges for system configuration.
# - Internet connection for package installations (if necessary).
#
# Dependencies : This script requires the following packages:
# - resolvconf (For DNS resolution management, if installed).
#
# Return Values : On successful completion, the script:
# - Configures the Host-Only interface with the static IP.
# - Renames the server to SRV-LIN1-02.
# - Sets up DNS resolution with the correct nameservers.
# - Provides the system's current network configuration.
#
# Notes : - Ensure that the network is properly configured and the DNS server is reachable.
# - The script modifies /etc/resolv.conf to ensure proper DNS configuration.
############################################################################################################
# Global variables
DNS_SERVER="10.10.10.11"
HOST_ONLY_INTERFACE=$(ip -o link | grep 'ens.*:' | awk -F: '{gsub(/ /,"",$2); print $2}' | grep -v 'lo' | head -n 1)
# Network configuration variables
LOCAL_DOMAIN="lin1.local"
SRV1_IP="10.10.10.11"
SRV2_IP="10.10.10.22"
# Server names
SRV2_HOSTNAME="SRV-LIN1-02"
# Function to update the system
update_system() {
echo "Updating system packages..."
apt-get update && apt-get upgrade -y
}
# Function to rename the machine to SRV-LIN1-02
rename_machine() {
echo "Renaming the machine to $SRV2_HOSTNAME..."
hostnamectl set-hostname $SRV2_HOSTNAME
echo "$SRV2_HOSTNAME" >/etc/hostname
echo "Machine renamed to $SRV2_HOSTNAME."
}
# Function to configure network interfaces
configure_network_interfaces() {
echo "Configuring network interfaces..."
cat <<EOF >/etc/network/interfaces
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface (Host-Only)
auto $HOST_ONLY_INTERFACE
allow-hotplug $HOST_ONLY_INTERFACE
iface $HOST_ONLY_INTERFACE inet static
address $SRV2_IP/24
gateway $SRV1_IP
dns-nameservers $DNS_SERVER
EOF
}
# Function to configure resolv.conf and prevent auto-updates
configure_resolv_conf() {
echo "Configuring resolv.conf..."
# Overwrite the resolv.conf file with the desired nameserver
echo "domain localdomain" >/etc/resolv.conf
echo "search localdomain" >>/etc/resolv.conf
echo "nameserver $DNS_SERVER" >>/etc/resolv.conf
# Prevent resolv.conf from being automatically updated
echo "Preventing automatic updates to resolv.conf..."
sudo sed -i '/domain-name, domain-name-servers, domain-search, host-name,/d' /etc/dhcp/dhclient.conf
}
# Function to update /etc/hosts with the static IP and FQDN for SRV2
update_local_hostname() {
echo "Updating /etc/hosts with the correct IP and FQDN for $SRV2_HOSTNAME..."
# Check if there is an existing entry for SRV2's IP and FQDN, and update or add it
if grep -q "$SRV2_IP" /etc/hosts; then
sed -i "s/$SRV2_IP.*/$SRV2_IP $SRV2_HOSTNAME.$LOCAL_DOMAIN/g" /etc/hosts
else
# If not present, add the new entry
echo "$SRV2_IP $SRV2_HOSTNAME.$LOCAL_DOMAIN" >>/etc/hosts
fi
echo "/etc/hosts updated with $SRV2_IP $SRV2_HOSTNAME.$LOCAL_DOMAIN."
}
# Function to apply network configuration changes
apply_network_configuration() {
echo "Applying network configurations..."
echo "Reconnect using the new IP address: $SRV2_IP"
systemctl restart networking.service
}
# Function to display final information
display_info() {
echo "#######################################"
echo "# Network Setup Summary #"
echo "#######################################"
# Check if the machine hostname is correctly set
echo ""
echo "Hostname Verification:"
if [ "$(hostname)" = "$SRV2_HOSTNAME" ]; then
echo " Hostname is set to $SRV2_HOSTNAME (configured correctly)"
else
echo " Error: Hostname is not set correctly"
fi
# Check Host-Only Interface configuration in /etc/network/interfaces
echo ""
echo "Host-Only Interface ($HOST_ONLY_INTERFACE):"
if grep -q "address $SRV2_IP" /etc/network/interfaces; then
echo " IP Address: $SRV2_IP (configured correctly in /etc/network/interfaces)"
else
echo " Error: IP Address not configured correctly in /etc/network/interfaces"
fi
# Check if DNS Server is correctly set in resolv.conf
echo ""
echo "DNS Server Configuration:"
if grep -q "nameserver $DNS_SERVER" /etc/resolv.conf; then
echo " DNS Server: $DNS_SERVER (configured correctly)"
else
echo " Error: DNS Server not correctly configured in /etc/resolv.conf"
fi
# Check if /etc/hosts was updated
echo ""
echo "/etc/hosts File:"
if grep -q "$SRV2_IP" /etc/hosts; then
echo " /etc/hosts updated with IP $SRV2_IP and hostname $SRV2_HOSTNAME"
else
echo " Error: /etc/hosts not updated correctly"
fi
echo ""
echo "############################################################"
echo "# Summary of network configuration changes above #"
echo "############################################################"
}
# Function to wait for user confirmation before reconnection
wait_for_user_confirmation() {
echo ""
echo "#######################################"
echo "# Press Enter to confirm the changes #"
echo "# Your SSH session will be terminated #"
echo "# Reconnect using the new IP address #"
echo "#######################################"
read -p ""
}
# Main function to run all configurations
main() {
rename_machine
update_local_hostname
update_static_ip_hostname
configure_network_interfaces
configure_resolv_conf
display_info
wait_for_user_confirmation
apply_network_configuration
}
# Execute the script
main
#######################################
# Network Setup Summary #
#######################################
Hostname Verification:
Hostname is set to SRV-LIN1-02 (configured correctly)
Host-Only Interface (ens33):
IP Address: 10.10.10.22 (configured correctly in /etc/network/interfaces)
DNS Server Configuration:
DNS Server: 10.10.10.11 (configured correctly)
/etc/hosts File:
/etc/hosts updated with IP 10.10.10.22 and hostname SRV-LIN1-02
############################################################
# Summary of network configuration changes above #
############################################################
#######################################
# Press Enter to confirm the changes #
# Your SSH session will be terminated #
# Reconnect using the new IP address #
#######################################