Skip to content

Instantly share code, notes, and snippets.

@delgh1
Last active August 5, 2023 08:59
Show Gist options
  • Save delgh1/f57ae97ac2a7ae5ccd095a70f39437b7 to your computer and use it in GitHub Desktop.
Save delgh1/f57ae97ac2a7ae5ccd095a70f39437b7 to your computer and use it in GitHub Desktop.
A script to switch from dhcpcd to systemd-networkd on Raspberry Pi OS
#!/bin/bash
# Use systemd-networkd instead of dhcpcd as the DHCP client for Raspberry Pi OS
# remember to use wpa_passphrase to add SSID & PW to /etc/wpa_supplicant/wpa_supplicant.conf
# checking sudo privilege
if [[ "$EUID" -ne 0 ]]; then
echo "This script must be run as root" 1>&2 # redirect stdout to stderr
exit 1
fi
###systemd-resolved###
mv /etc/resolv.conf /etc/resolv.conf.bak
ln -sf ../run/systemd/resolve/stub-resolv.conf /etc/resolv.conf
mkdir -p /etc/systemd/network
############################
cat << EOF > /etc/systemd/network/20-eth.network
[Match]
Name=eth*
[Network]
DHCP=true
IPv6AcceptRA=true
[DHCPv4]
RouteMetric=20
ClientIdentifier=mac
UseDomains=true
[IPv6AcceptRA]
RouteMetric=20
EOF
##########
cat << EOF > /etc/systemd/network/30-wlan.network
[Match]
Name=wlan*
[Network]
DHCP=true
IPv6AcceptRA=true
[DHCPv4]
RouteMetric=30
ClientIdentifier=mac
UseDomains=true
[IPv6AcceptRA]
RouteMetric=30
EOF
#############################
#cp /lib/systemd/system/systemd-networkd-wait-online.service /lib/systemd/system/systemd-networkd-wait-online.service.bak
#sed -i 's/systemd-networkd-wait-online$/systemd-networkd-wait-online --any/' /lib/systemd/system/systemd-networkd-wait-online.service
####### override systemd-networkd-wait-online to avoid startup delay and retain this config after systemd upgrade
mkdir -p /etc/systemd/system/systemd-networkd-wait-online.service.d
cat << EOF > /etc/systemd/system/systemd-networkd-wait-online.service.d/wait-for-only-one-interface.conf
[Service]
ExecStart=
ExecStart=/usr/lib/systemd/systemd-networkd-wait-online --any
EOF
########
####wireless####
# https://wiki.archlinux.org/title/wpa_supplicant#At_boot_(systemd)
ln -s /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
####sytstem services####
systemctl disable --now dhcpcd
systemctl enable --now systemd-networkd systemd-resolved wpa_supplicant@wlan0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment