How do I run a script or program immediately after my network interface goes up?
Environment
- Red Hat Enterprise Linux 4, 5, 6, 7, 8
initscriptsused to manage networking (not NetworkManager)
Issue
- How do I run a script or program immediately after my network interface goes up?
Resolution
Create the file /sbin/ifup-local and add the commands to run after the network is up.
An example for one physical interface:
#!/bin/bash
if [ "$1" == "eth0" ]; then
/sbin/ethtool -G eth0 rx 4096 tx 4096
fi
For bonding and bridging, you need to reference the virtual device being started, not the underlying physical devices:
#!/bin/bash
if [ "$1" == "br0" ]; then
/sbin/ethtool -G eth0 rx 4096 tx 4096
fi
#!/bin/bash
if [ "$1" == "bond0" ]; then
/sbin/ethtool -G eth0 rx 4096 tx 4096
/sbin/ethtool -G eth1 rx 4096 tx 4096
fi
- Make
/sbin/ifup-localexecutable
chmod +x /sbin/ifup-local
Note The ifup-local script is readable only by the initscripts and not by NetworkManager. To run a custom script using NetworkManager, create it under the dispatcher.d/ directory. Refer to Running Dispatcher scripts in the RHEL documentation for more information.
Root Cause
The network control scripts in /etc/sysconfig/network-scripts allows for adding scripts that will run after the device is up.
For example, looking at the /etc/sysconfig/network-scripts/ifup-eth file, the last few lines are:
...
echo ${dhcp6_pid[0]} > /var/run/dhcp6c_${DEVICE}.pid
fi;
fi
if [ "${IPX}" = yes ]; then
/etc/sysconfig/network-scripts/ifup-ipx ${DEVICE}
fi
exec /etc/sysconfig/network-scripts/ifup-post ${CONFIG} ${2}
A script /etc/sysconfig/network-scripts/ifup-post is the last item to be executed. Looking at that script:
...
if [ -x /sbin/ifup-local ]; then
/sbin/ifup-local ${DEVICE}
fi
exit 0
Notice the last few lines search for a file called /sbin/ifup-local. If that file is found, it is executed.
The file /sbin/ifup-local is the one that is available to run programs immediately after the interface is up.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.