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.
makeGRE.sh
#!/bin/bash
ip tunnel add gre1 mode gre local 192.168.5.2 remote 192.168.10.5 ttl 255
ip addr add 10.10.10.3/31 dev gre1 #This IP is used for BGP Peering
ip link set gre1 up
delGRE.sh
#!/bin/bash
ip link set gre1 down
ip tunnel del gre1
The makeGRE.sh script is called when the service starts and the delGRE.sh 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
[Unit]
Description=Create gre1 for FRR
After=network.target #Specifies what systemd unit must be running before this service starts. We need the network running prior to creating a GRE tunnel
[Service]
Type=oneshot #Type used where the service executable is not constantly running.
ExecStart=/root/makeGRE.sh
ExecStop=/root/delGRE.sh
User=root
RemainAfterExit=yes #Have systemd show the service as active so long as the service was started but not explicitly stopped.
[Install]
WantedBy=multi-user.target
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.