Linux

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

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:

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:

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:

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:

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.

Show More

Related Articles

Leave a Reply

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

Close