Files
debian-unattended/postinstall/init.sh
Bryan Joshua Pedini 798af69292 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.
2026-08-26 21:10:36 +02:00

57 lines
1.6 KiB
Bash

#!/usr/bin/env bash
# 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}')
NETMASK=$(echo ${IP_ADDRESS} | sed 's/.*\///')
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
iface lo inet loopback
auto ${NIC}
iface ${NIC} inet static
address ${IP_ADDRESS}/${NETMASK}
gateway ${GATEWAY}
dns-nameservers ${DNS_SERVERS}
EOF
# Tabs instead of spaces, neat the file
sed -i 's/ /\t/gm' /etc/network/interfaces
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
done
# Hosts file
cat << EOF > /etc/hosts
127.0.0.1 localhost localhost.localdomain
::1 localhost localhost.localdomain
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
${IP_ADDRESS} $(hostname --fqdn) $(hostname)
EOF
# Tabs instead of spaces, neat the file
sed -i 's/ /\t/gm' /etc/hosts
# Personalizations
wget -O - https://45r.it/serversetup | bash
# Cleanup and reboot
rm -rf /etc/systemd/system/multi-user.target.wants/postinstall-init.service /var/lib/systemd/system/postinstall-init.service /debian-postinstall-init.sh /.wget-hsts
reboot