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:
- 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.
# your commands here
- Make the script executable:
chmod +x /etc/NetworkManager/dispatcher.d/my-script.sh
- 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. - When the script is run, NetworkManager will pass the name of the interface as the first argument.
- 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.