Richard Bernecker

Random Solutions to Random Problems

Configuring a Persistent GRE Tunnel via systemd

Although Linux’s ip command suite provides all of the tools to configure the vast majority of interface types, those changes will not survive a system reboot. The easiest solution to this problem is to build a service using systemd that will run on system boot.

To build a service based on bash commands, you’ll want to build one or more script files that will be called by the service that we create. In my case, I built two scripts: One to build a GRE tunnel and one to tear it back down. Both are listed below.

ip tunnel add gre1 mode gre local remote ttl 255
ip addr add dev gre1 #This IP is used for BGP Peering
ip link set gre1 up

ip link set gre1 down
ip tunnel del gre1

The script is called when the service starts and the script is called on system shutdown or whenever the service is stopped. We create the following file to add our service to systemd.

Location: /etc/systemd/system/gre.service

Description=Create gre1 for FRR #Specifies what systemd unit must be running before this service starts. We need the network running prior to creating a GRE tunnel

Type=oneshot #Type used where the service executable is not constantly running.
RemainAfterExit=yes #Have systemd show the service as active so long as the service was started but not explicitly stopped.


Once we have the scripts and service in place, we need to make systemd aware of the new service we’ve created and enable it to start at system boot as shown below.

sudo systemctl daemon-reload
sudo systemctl enable gre.service
sudo systemctl start gre.service

That’s it! You’ve now created a service that will build a GRE tunnel for you whenever the box reboots. This approach will work for any service that is based on scripts rather than a persistent executable.