CentOS 7 Run Script When Network Interface is Up (Network Manager)

last updated in Categories , , ,

I want to run a shell script or other program immediately after my network interface goes up on a CentOS 7 / RHEL 7 server. I created /sbin/ifup-local, but it is not working on a CentOS 7 box but works perfectly on CentOS 6. I am using NetworkManager with CentOS 7. How do I run ifup-post or ifup-pre scripts?

Introduction: The ifup and friends script do not run if NetworkManager is enabled. The default is to allow NetworkManager on a CentOS or RHEL 7 server. To solve this problem, you can disable NetworkManager and use all existing scripts from CentOS 6.x or earlier. However, if you need to use NetworkManager, you need to place scripts in /etc/NetworkManager/dispatcher.d/ directory. Let us see how to use both methods to run a script or program immediately after your network interface goes up.

CentOS 7 Run Script When Network Interface is Up When Using Network Manager (nm)

In this method # 1, you are going to use a Network Manager (nm) to run a script called ifup-local when interface named wlp4s0 goes up and running.

An example of an ifup-local script using the bash/sh case statment

Create a script called /etc/NetworkManager/dispatcher.d/ifup-local on a CentOS / RHEL 7 box using the nano command or vim command:
$ sudo vim /etc/NetworkManager/dispatcher.d/ifup-local
Append the following code:

#!/bin/sh
# Purpose: Setup additional ip or routing when wlp4s0 goes up under GPL v.3.x+
# Author: Vivek Gite {https://www.cyberciti.biz/
# -----------------------------------------------------------------------------
 
INF="$1" # your current interface name such as eth0, wlp4s0 and so on
STA="$2" # status such as UP or DOWN
 
# Send message to /var/log/messages 
logger "$0 called for interface named $INF with $STA ..."
 
# We just want to work on wlp4s0 interface
# When matched called another script called /root/bin/setup.wlp4s0.ips
case "$INF" in
        wlp4s0) logger "Setting up $INF ip address..."; /root/bin/setup.wlp4s0.ips ;;
esac
exit 0

Save and close the file in vim text editor. Make sure you setup correct permission on your script:
$ chmod +x /etc/NetworkManager/dispatcher.d/ifup-local
Here is a sample /root/bin/setup.wlp4s0.ips displayed using the cat command:
$ sudo cat /root/bin/setup.wlp4s0.ips
Sample outputs:

#!/bin/bash
# Purpose: Setup additional ip or routing under GPL v.3.x+
# Author: Vivek Gite {https://www.cyberciti.biz/}
# -----------------------------------------------------------------------------
#/sbin/iptables -I FORWARD 1 -m state -s 192.168.2.0/24 -d 192.168.122.0/24 --state NEW,RELATED,ESTABLISHED -j ACCEPT
#
INF="$1" # your current interface name such as eth0, wlp4s0 and so on
STA="$2" # status such as UP or DOWN
 
# Send message to /var/log/messages 
logger "$0 called for interface named $INF with $STA ..."
 
if [ "$INF" == "wlp4s0" ]
then
start=201
for i in {0..10}
do
	/usr/sbin/ifconfig wlp4s0:${i} 192.168.2.${start}
	(( start++ ))
done
fi

Both scripts are pretty much self-documenting and quickly developed or modified anyone. Both scripts are pretty much self-documenting and quickly developed or modified anyone. The script /etc/NetworkManager/dispatcher.d/ifup-local run each time an interface named wlp4s0 up. The script-based config remains persistent across reboots and when network manager/service restarts. Here is a sample log from /var/log/messages displayed using tail command or grep command/egrep command generated by logger command:
# tail -f /var/log/messages
# egrep -i 'Setting up|ifup-local' /var/log/messages

CentOS 7 Run Script When Network Interface is Up with nm
Click to enlarge image

A note about creating /sbin/ifdown-local when network interface goes down

When a network interface goes down /sbin/ifdown-local runs. Here is a sample script:

#!/bin/bash
# Purpose: Remove additional ip or routing under GPL v.3.x+
# Author: Vivek Gite {https://www.cyberciti.biz/}
# -----------------------------------------------------------------------------
# take down all ips and delete routing if any
start=201
for i in {0..10}
do
	/usr/sbin/ifconfig wlp4s0:${i} down
	(( start++ ))
done

CentOS 7 disable Network Manager (nm) and run ifup-local script

To disable nm set NM_CONTROLLED to no in /etc/sysconfig/network-scripts/ifcfg-wlp4s0:
# vi /etc/sysconfig/network-scripts/ifcfg-wlp4s0
Append the following
NM_CONTROLLED=no
Save and close the file. If set to ‘no’, NetworkManager will ignore this connection/device and thus it will run /sbin/ifup-local. By defaults NM_CONTROLLED set to ‘yes’. Here is a sample /sbin/ifup-local:

#!/bin/sh
# Tested on a CentOS and RHEL version 5/6/7
# Author: Author: Vivek Gite {https://www.cyberciti.biz/}
# -------------------------------------------------------
#!/bin/sh
IN="$1"
STATUS="$2"
case "$1" in
	eth0)
	logger "$0: Working on eth0 with $STATUS ..."
	/sbin/ethtool -K eth0 gso off
	;;
 
	eth1)
	logger "$0: Working on eth1 with $STATUS ..."
	/sbin/ethtool -G eth1 rx 4096 tx 4096
	/sbin/ethtool -K eth1 gso off
	;;
 
	eth2)
	logger "$0: Working on eth2 with $STATUS ..."
	start=201
	for i in {0..10}
	do
		/usr/sbin/ifconfig eth2:${i} 192.168.2.${start}
		(( start++ ))
	done
 
esac
exit 0

Create /sbin/ifdown-local if you want to run a script when a network interface goes down as described above.

Conclusion

You learned how to run commands persistently on boot or whenever network interface goes up or down. Take a look at syconfig.txt for more info.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at www.nixcraft.com