Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
networkd-dispatcher bash script to set all IPv6 addresses to preferred_lft 0
#!/bin/bash
# bash v4 required because of associative array below.
# networkd-dispatcher hook script that sets every IPv6 address on the system to
# have preferred_lft 0. Put it in for example in
# /etc/networkd-dispatcher/routable.d/50-set-preferred-lft and make sure it's
# executable and owned by root:root.
#
# There's an associative array below where you can list addresses NOT to
# interfere with.
logname="set-preferred-lft"
declare -A exceptions
# Associative array with keys for each address that you DON'T want to have
# a preferred_lft of 0. Every other v6 address on the system will have
# preferred_lft 0 set.
exceptions["2001:ba8:1f1:f1d7::2"]="1"
logger -t $logname \
"$IFACE/$STATE: Looking for IPv6 addresses to set preferred_lft 0…"
for ip6 in $IP6_ADDRS
do
if [[ -n "${exceptions[$ip6]}" ]]
then
logger -t $logname \
"Not setting preferred_lft for $ip6 as it's in exception list"
else
logger -t $logname \
"Setting preferred_lft 0 for $ip6"
ip address change ${ip6} dev ${IFACE} preferred_lft 0
fi
done
logger -t $logname \
"$IFACE/$STATE: Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.