Execute a script after a VPN connection is started

I'm starting a VPN connection using Network Manager. Once the connection is established I have to change MTU in order it to work properly. For example:

sudo ifconfig ppp0 mtu 777

It is very annoying to execute this command every time I open a VPN connection. Is there any idea to create a script that would execute this command automatically once the connection is established (ppp0 interface is up)?


Solution 1:

Create a script in /etc/network/if-up.d, containing

#!/bin/sh

if [ "$IFACE" = "ppp0" ]; then
    ifconfig ppp0 mtu 777
fi

and make it executable.

Solution 2:

Here's a modernized (using ip instead of ifconfig) and generic (openconnect on the command-line uses tun0, Network Manager uses vpn0 through the network applet; the numbers may vary if another network is already active) variant of @enzotib's excellent answer, tested on Ubuntu 20.04:

#!/bin/bash

if [[ "$IFACE" =~ ^(tun|vpn)[0-9] ]]; then
    ip link set "$IFACE" mtu 777
fi