Fix resolv.conf being wiped by dhcpcd on first reboot.

Wait for network-online, drop DHCP while still dhcp so dhcpcd exits
before writing static resolv.conf. Do not touch dhcpcd.conf.
This commit is contained in:
2026-08-26 21:10:36 +02:00
parent 83a4876f49
commit 798af69292
2 changed files with 10 additions and 4 deletions

View File

@@ -1,8 +1,5 @@
#!/usr/bin/env bash
# Sometimes the network is slow, wait a bit
sleep 10
# Get the current DHCP network address, gateway and DNS servers
NIC=$(ip link | grep "^2:" | awk '{print $2}' | tr -d ":")
IP_ADDRESS=$(ip address show ${NIC} | grep -E "inet " | awk '{print $2}')
@@ -11,6 +8,10 @@ IP_ADDRESS=$(echo ${IP_ADDRESS} | sed 's/\/.*//')
GATEWAY=$(ip route | grep "default" | awk '{print $3}')
DNS_SERVERS=$(awk '/^nameserver/ {print $2}' /etc/resolv.conf | xargs)
# Tear down DHCP first, while interfaces is still dhcp, so dhcpcd is gone
# before we write a static resolv.conf (otherwise it rewrites it on exit)
ifdown "${NIC}" || true
# Network interfaces (static address)
cat << EOF > /etc/network/interfaces
auto lo
@@ -25,7 +26,10 @@ EOF
# Tabs instead of spaces, neat the file
sed -i 's/ /\t/gm' /etc/network/interfaces
# write a static resolv.conf
ifup "${NIC}" || true
# write a static resolv.conf after dhcpcd is gone so it survives reboot
rm -f /etc/resolv.conf
for ns in ${DNS_SERVERS}; do
echo "nameserver ${ns}" >> /etc/resolv.conf