Chapter 22. Running dhclient exit hooks using NetworkManager a dispatcher script
You can use a NetworkManager dispatcher script to execute dhclient exit hooks.
22.1. The concept of NetworkManager dispatcher scripts Copy linkLink copied to clipboard!
The NetworkManager-dispatcher service executes user-provided scripts in alphabetical order when network events happen. These scripts are typically shell scripts, but can be any executable script or application. You can use dispatcher scripts, for example, to adjust network-related settings that you cannot manage with NetworkManager.
You can store dispatcher scripts in the following directories:
-
/etc/NetworkManager/dispatcher.d/: The general location for dispatcher scripts therootuser can edit. -
/usr/lib/NetworkManager/dispatcher.d/: For pre-deployed immutable dispatcher scripts.
For security reasons, the NetworkManager-dispatcher service executes scripts only if the following conditions met:
-
The script is owned by the
rootuser. -
The script is only readable and writable by
root. -
The
setuidbit is not set on the script.
The NetworkManager-dispatcher service runs each script with two arguments:
- The interface name of the device the operation happened on.
-
The action, such as
up, when the interface has been activated.
The Dispatcher scripts section in the NetworkManager(8) man page provides an overview of actions and environment variables you can use in scripts.
The NetworkManager-dispatcher service runs one script at a time, but asynchronously from the main NetworkManager process. Note that, if a script is queued, the service will always run it, even if a later event makes it obsolete. However, the NetworkManager-dispatcher service runs scripts that are symbolic links referring to files in /etc/NetworkManager/dispatcher.d/no-wait.d/ immediately, without waiting for the termination of previous scripts, and in parallel.
22.2. Creating a NetworkManager dispatcher script that runs dhclient exit hooks Copy linkLink copied to clipboard!
When a DHCP server assigns or updates an IPv4 address, NetworkManager can run a dispatcher script stored in the /etc/dhcp/dhclient-exit-hooks.d/ directory. This dispatcher script can then, for example, run dhclient exit hooks.
Prerequisites
-
The
dhclientexit hooks are stored in the/etc/dhcp/dhclient-exit-hooks.d/directory.
Procedure
Create the
/etc/NetworkManager/dispatcher.d/12-dhclient-downfile with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set the
rootuser as owner of the file:chown root:root /etc/NetworkManager/dispatcher.d/12-dhclient-down
# chown root:root /etc/NetworkManager/dispatcher.d/12-dhclient-downCopy to Clipboard Copied! Toggle word wrap Toggle overflow Set the permissions so that only the root user can execute it:
chmod 0700 /etc/NetworkManager/dispatcher.d/12-dhclient-down
# chmod 0700 /etc/NetworkManager/dispatcher.d/12-dhclient-downCopy to Clipboard Copied! Toggle word wrap Toggle overflow Restore the SELinux context:
restorecon /etc/NetworkManager/dispatcher.d/12-dhclient-down
# restorecon /etc/NetworkManager/dispatcher.d/12-dhclient-downCopy to Clipboard Copied! Toggle word wrap Toggle overflow