raspberry pi: make routes persistent with dhcpcd

routing decisions…

so, the other day I needed to add a static route to my raspberry’s route table. the critter is running raspbian 9 (“jessie”). this version of debianesque linux uses dhcpcd for the network configuration.

you can find a whole lot of info on how to configure the box with a static ip address. however, there are few examples for configuring a static routing table entry that will persist after a reboot.

as we all know by now, you can enter a manual route using the command:

# /sbin/route add -net 10.1.2.0/24 gw 10.1.1.100

where 10.1.1.100 is the router on your local network behind which the target network 10.1.2.0 with the netmask 255.255.255.0 (the “/24”) is to be found!

this entry will be lost after a reboot. in order to make it persistent, you have to edit/create a so-called “exit hook”. that’s a file that gets executed by dhcpd at various occasions, e.g. after the network interface has been configured with a valid address.

here’s how to do it:

edit the exit hook file. the command will create it, if it’s not there already:

# sudo vi /etc/dhcpcd.exit-hook

add the routing command, just as you’d type it on the command line (first line is for documentation):

## adding the persistent route from the example above:
/sbin/route add -net 10.1.2.0/24 gw 10.1.1.100

now, in order to activate the hook file, you can try:

# sudo service dhcpcd restart

for a definitive test issue the reboot command.

if something goes wrong, you can debug the dhcpcd hooks by editing the file /lib/dhcpcd/dhcpcd-run-hooks and change the line

: ${syslog_debug:=false}
to
: ${syslog_debug:=true}

you can then “tail /var/syslog” in order to see the debug output.

happy routing!

5 thoughts on “raspberry pi: make routes persistent with dhcpcd

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program.More information on Akismet and GDPR.

This site uses Akismet to reduce spam. Learn how your comment data is processed.