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

In CentOS 7, you can use the NetworkManager service to run a script when a network interface is brought up. The NetworkManager service provides a number of hooks that can be used to execute scripts at different points in the network configuration process.

Here’s an example of how you can use the NetworkManager service to run a script when a network interface is brought up:

  1. Create a script that you want to run when the network interface is brought up. For example, if you want to run a script called my-script.sh you can create it in the /etc/NetworkManager/dispatcher.d/ directory.
#!/bin/bash
# your commands here
  1. Make the script executable:
chmod +x /etc/NetworkManager/dispatcher.d/my-script.sh
  1. The NetworkManager service will execute all scripts in the /etc/NetworkManager/dispatcher.d/ directory that have a name of the form *.sh when a network interface is brought up.
  2. When the script is run, NetworkManager will pass the name of the interface as the first argument.
  3. If the script exits with a non-zero exit code, NetworkManager will log an error message but will otherwise continue to operate normally.

Please note that the above instructions are based on the assumption that NetworkManager is being used as the network management service on your system. If your system is using a different service, the instructions may vary.

Also, you can check the script by bringing the network interface up manually using ifup command.

ifup eth0

This will execute the script that you created and passed as an argument with the name of the interface.

Leave a Comment