Dave Byrne
Dave Byrne

Reputation: 439

CentOS Network Interface Post-Up Script Not Executing

I am running CentOS 7.2, and I'm struggling to get a simple script to execute on ifup of any interface.

My /sbin/ifup-local looks like this:

#!/bin/bash
 if [[ "$1" == "eth0" ]]
   then
     exec /vpnup
 fi
[root@oracle2 ~]#

The referenced script /vpnup looks like this:

#!/bin/bash
#
# CompanyX Production L2TP VPN - UP
#
#
echo -e "\n"
echo -e "PLEASE WAIT\n"
echo -e "Dialling Production L2TP VPN... \n"
echo -e ".........................................\n"
ipsec auto --up L2TP-PSK && echo "c qvprodvpn" > /var/run/xl2tpd/l2tp-control
echo -e ".........................................\n"
echo "Connected..."
echo "Adding local static route to manage VPN bound traffic..."
sleep 6s
ip route add 10.10.24.0/24 via 10.10.24.51
echo "Route added..."
echo -e "...\n"

Fairly simple, the script works fine when called at command line. It just dials into a L2TP VPN that I've setup, to get this box access to the production LAN of another segment of their network.

However, if I execute service network restart or indeed systemctl restart network.service, the VPN interface does not come up, nor does the IP route get added. If I manually execute ifdown eth0, and then ifup eth0, it also does not run the script as intended.

If I execute /sbin/ifup-local eth0 the script runs as expected, so I know my script is fine, and I know my ifup-local is fine.

Am I missing something obvious? I've never worked with pre/post up scripts before, but I always figured they were pretty simple. Was I wrong?

Upvotes: 4

Views: 8066

Answers (2)

Eddie C.
Eddie C.

Reputation: 1012

Just in case it might help anyone, to let the ifup-local script be called, execute the following commands:

chmod +x /etc/sysconfig/network-scripts/ifup-local
ln -s /etc/sysconfig/network-scripts/ifup-local /sbin/ifup-local

Using Network Manager

As the network-scripts are deprecated, one might consider configuring a hook for NetworkManager in /etc/NetworkManager/dispatcher.d. For example:

touch    /etc/NetworkManager/dispatcher.d/eth0-listener.sh
chmod +x /etc/NetworkManager/dispatcher.d/eth0-listener.sh

Then put something like the following inside eth0-listener.sh:

#!/usr/bin/env bash

interface=$1
event=$2


if [[ ${interface} != "eth0" ]] || [[ ${event} != "up" ]]
then
  # Nothing to do. We are not interested in the event nor the interface. 
  exit 0
fi


# Your initialisation commands go here:
# For example:
# ip route add x.x.x.x/y via z.z.x.x

Upvotes: 0

mysteryegg
mysteryegg

Reputation: 511

Ensure your ifcfg-eth0 script includes

NM_CONTROLLED=no

Otherwise, calling systemctl restart network or ifup eth0 will not execute ifup-pre-local, ifup-eth, ifup-post, ifup-local, etc. for eth0. They will still be called for lo, though.

Upvotes: 7

Related Questions