Set routes on VPN startup using NetworkManager

From Levy

Revision as of 13:45, 3 February 2020 by Louis (talk | contribs) (Created page with "For my internet access being secure I like to be behind a VPN. However, if I connect to a VPN, my default route gets replaced with the VPN default route. As a result of this I...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

For my internet access being secure I like to be behind a VPN. However, if I connect to a VPN, my default route gets replaced with the VPN default route. As a result of this I cannot access my storage network any more. Setting the routes in the KDE Network Manager results in the VPN not connecting any more. So, I've created a small script that sets a route to my storage network after a VPN connection is established.

In order to make this work, create the script /etc/NetworkManager/dispatcher.d/30-vpn with the following content:

if [ "$2" = "vpn-up" ]; then  
       ip route add 192.168.1.0/24 via 172.16.0.10 metric 10
fi  
if [ "$2" = "vpn-down" ]; then  
       ip route del 192.168.1.0/24 via 172.16.0.10 metric 10
fi  
exit $?

This script is automatically executed by NetworkManager whenever a connection goes up or down. As we specify to only take action on either "vpn-up" or "vpn-down" it does exactly what needs to be doing: (un)set the route to the storage LAN when the state of the VPN connection changes.