I would like to add a static route using a systemd service file on system boot.
I have a DMZ network on a separate 192.168 class C network reachable via my default gateway. I also use OpenVPN to route my laptop's internet traffic, which is connected automatically by NetworkManager along with my Wi-Fi connection. Problem being, when OpenVPN connects and adds a new default route to the routing table, the DMZ is no longer reachable. Obviously, I can overcome this by manually adding a static route, but I would like to do this automatically on boot or when OpenVPN connects.
I have tried to create a systemd service file to do this on boot. The service file tries to execute
ip route add and as such needs to run after the network is up:
#01-static-route.service [Unit] Description=Static route to the DMZ Wants=network-online.target After=network-online.target [Service] Type=oneshot # Add a route to the DMZ network since the vpn will change our default gateway ExecStart=/usr/bin/ip route add 192.168.0.0/24 via 192.168.1.1 [Install] WantedBy=multi-user.target
I've enabled the NetworkManager-wait-online.service as follows:
#systemctl enable NetworkManager-wait-online.service
When the system boots, the service file fails and
systemctl status 01-static-route.service returns the following information:
# systemctl status 01-static-route.service ● 01-static-route.service - Static route to the DMZ Loaded: loaded (/etc/systemd/system/01-static-route.service; enabled; vendor preset: disabled) Active: failed (Result: exit-code) since Mon 2015-04-13 16:40:11 BST; 28s ago Process: 558 ExecStart=/usr/bin/ip route add 192.168.0.0/24 via 192.168.1.1 (code=exited, status=2) Main PID: 558 (code=exited, status=2) Apr 13 16:40:11 archlap ip: RTNETLINK answers: Network is unreachable Apr 13 16:40:11 archlap systemd: 01-static-route.service: main process exited, code=exited, status=2/INVALIDARGUMENT Apr 13 16:40:11 archlap systemd: Failed to start Static route to the DMZ. Apr 13 16:40:11 archlap systemd: Unit 01-static-route.service entered failed state. Apr 13 16:40:11 archlap systemd: 01-static-route.service failed.
I assume the
ip route add command is failing because there is no network connection when it is run.
Could someone please explain the correct procedure to add a persistent static route using systemd (or any other method) to overcome this problem?