Handle dhcpcd DNS configuration

This commit is contained in:
2026-05-22 14:07:14 +02:00
parent 16eb86db69
commit b5b8f83b3c
2 changed files with 50 additions and 0 deletions
+48
View File
@@ -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