NetworkManager Dispatcher Scripts for Linux Automation

NetworkManager dispatcher scripts provide an automated, event-driven mechanism for executing custom commands and programs whenever network state changes occur in a Linux operating system. This article explains the purpose of these scripts, how they function, their execution environment, and how administrators use them to streamline network operations, manage system services, and enforce dynamic security policies.

What Are NetworkManager Dispatcher Scripts?

NetworkManager dispatcher scripts are executable files placed in the /etc/NetworkManager/dispatcher.d/ directory. NetworkManager executes these scripts automatically in response to specific network events, such as an interface establishing a connection, disconnecting, or changing IP configurations. Rather than manually running commands or relying on continuous background polling, administrators use dispatcher scripts to trigger actions only when an actual state transition happens.

How Dispatcher Scripts Function

When a network transition occurs, the nm-dispatcher daemon executes the scripts within the directory in alphabetical order. To execute safely, each script must be owned by root, have execute permissions (chmod 755), and must not be writable by any user other than root.

NetworkManager passes two primary positional parameters to each script:

  1. $1 (Interface Name): The name of the network interface involved (e.g., eth0, wlan0).
  2. $2 (Action/Event): The specific event that triggered the script (e.g., up, down, pre-up, vpn-up, vpn-down).

In addition to positional arguments, NetworkManager exposes dynamic connection details through environment variables, including CONNECTION_UUID, CONNECTION_ID, IP4_ADDRESS_0, and DEVICE_IP_IFACE. These variables allow scripts to identify specific Wi-Fi networks, VPN profiles, or Ethernet ports and act accordingly.

Core Automation Use Cases

By leveraging dispatcher scripts, Linux administrators achieve low-overhead, precise automation that reacts instantly to changing network topologies without requiring user intervention.