How to run a startup script automatically after a network interface is up on CentOS

In some cases, you may want to run a specific command or a custom script right after a given network interface (e.g., eth0) is up. For example, you want to configure interface-specific iptables rules or QoS settings. Follow this guideline to learn how to set up a startup script for a specific network interface on CentOS.

On CentOS, network interface related scripts are found in /etc/sysconfig/network-scripts. Among them is ifup-post which is supposed to be called right after any network interface is brought up online. In this script, you will find the following code snippet toward the end.

if [ -x /sbin/ifup-local ]; then
    /sbin/ifup-local ${DEVICE}

The code snippet means that if there exists a script called ifup-local in /sbin, the script gets executed with an interface name argument. On vanilla CentOS system, no such script exists. So in order to run a startup script automatically after a network interface is up, create an executable script /sbin/ifup-local, and put in there any command or script you want to run. For example:

$ sudo vi /sbin/ifup-local
if [[ "$1" == "eth0" ]]
then
  echo "this part will be executed right after eth0 is up."
  echo "so you can put any startup command for eth0 here"
else
  #DO_NOTHING
fi
$ sudo chmod +x /sbin/ifup-local

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

Leave a comment

Your email address will not be published. Required fields are marked *

Current day month ye@r *