diff --git a/README.md b/README.md index e0ae8ef..3c3eda1 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ Le script rĂ©seau cible une installation serveur Debian classique utilisant : - 🔐 une exĂ©cution en root via `sudo` Il ne vise pas les postes avec NetworkManager, interface graphique ou gestion rĂ©seau desktop. +Si `dhcpcd` est prĂ©sent ou si `/etc/resolv.conf` est gĂ©nĂ©rĂ© par `dhcpcd`, le script ajoute aussi un bloc statique dĂ©diĂ© dans `/etc/dhcpcd.conf` afin que les DNS soient conservĂ©s aprĂšs redĂ©marrage. ## 🚀 Utilisation rĂ©seau @@ -98,6 +99,7 @@ Avant modification, le script sauvegarde les fichiers concernĂ©s avec un suffixe /etc/hostname.bak.YYYYMMDD-HHMMSS /etc/hosts.bak.YYYYMMDD-HHMMSS /etc/network/interfaces.bak.YYYYMMDD-HHMMSS +/etc/dhcpcd.conf.bak.YYYYMMDD-HHMMSS /etc/resolv.conf.bak.YYYYMMDD-HHMMSS ``` diff --git a/configure-debian-network.sh b/configure-debian-network.sh index f7836aa..7fe4523 100755 --- a/configure-debian-network.sh +++ b/configure-debian-network.sh @@ -202,6 +202,48 @@ iface ${interface} inet static EOF } +# Indique si dhcpcd semble present et susceptible de gerer resolv.conf. +has_dhcpcd() { + [[ -e /etc/dhcpcd.conf ]] || + [[ -e /lib/systemd/system/dhcpcd.service ]] || + [[ -e /usr/lib/systemd/system/dhcpcd.service ]] || + command -v dhcpcd >/dev/null 2>&1 || + grep -qi 'dhcpcd' /etc/resolv.conf 2>/dev/null +} + +# Met a jour /etc/dhcpcd.conf avec un bloc statique dedie au script. +write_dhcpcd_conf() { + local interface="$1" + local ip_cidr="$2" + local gateway="$3" + local dns_servers="$4" + local tmp_file + + if ! has_dhcpcd; then + return 0 + fi + + touch /etc/dhcpcd.conf + tmp_file="$(mktemp)" + awk ' + $0 == "# BEGIN configure-debian-network.sh" { skip = 1; next } + $0 == "# END configure-debian-network.sh" { skip = 0; next } + skip != 1 { print } + ' /etc/dhcpcd.conf > "${tmp_file}" + + { + printf '\n# BEGIN configure-debian-network.sh\n' + printf 'interface %s\n' "${interface}" + printf 'static ip_address=%s\n' "${ip_cidr}" + printf 'static routers=%s\n' "${gateway}" + printf 'static domain_name_servers=%s\n' "${dns_servers}" + printf '# END configure-debian-network.sh\n' + } >> "${tmp_file}" + + install -m 0644 "${tmp_file}" /etc/dhcpcd.conf + rm -f "${tmp_file}" +} + # Ecrit les serveurs DNS dans /etc/resolv.conf quand ce fichier n'est pas gere par lien symbolique. write_resolv_conf() { local dns_servers="$1" @@ -279,8 +321,10 @@ main() { if [[ "${network_changed}" -eq 1 ]]; then backup_file /etc/network/interfaces + backup_file /etc/dhcpcd.conf backup_file /etc/resolv.conf write_network_interfaces "${interface}" "${ip_cidr}" "${gateway}" "${dns_servers}" + write_dhcpcd_conf "${interface}" "${ip_cidr}" "${gateway}" "${dns_servers}" write_resolv_conf "${dns_servers}" fi @@ -289,6 +333,10 @@ main() { if [[ "${network_changed}" -eq 1 ]]; then read -r -p "Redemarrer le service reseau maintenant ? [y/N]: " restart_answer if [[ "${restart_answer}" =~ ^[yY]$ ]]; then + if has_dhcpcd && systemctl is-active --quiet dhcpcd; then + systemctl restart dhcpcd + echo "Service dhcpcd redemarre." + fi if systemctl restart networking; then echo "Service networking redemarre." else