4 Running Scripts When Network Events Occur

You can configure the system to respond to network events by providing scripts for NetworkManager-dispatcher to run. Use a script, for example, to mount a remote file system when a device is brought up or send a notification when a device loses connectivity.

  1. Save an executable script in /etc/NetworkManager/dispatcher.d/ or one of its subdirectories.
    When NetworkManager-dispatcher runs a script, it passes two arguments to the script:
    • The name of the interface on which an action occurred.
    • The action that occurred.

    In addition, environment variables related to the network are available for you to use in the script.

    See the NetworkManager-dispatcher (8) manual page for a full list of actions and environment variables you can use in a script.

    Subdirectories within /etc/NetworkManager/dispatcher.d/ provide special handling of scripts.

    Subdirectory Details
    pre-up.d Place or symbolically link scripts responding to pre-up or vpn-pre-up actions in this subdirectory.
    pre-down.d Place or symbolically link scripts responding to pre-down or vpn-pre-down actions in this subdirectory.
    no-wait.d Create a symbolic link from the script to this subdirectory if you want the script to run immediately when an action occurs. These scripts run in parallel, and don't wait for scripts already running to end.

    Note:

    Depending on the SELinux policies and security contexts configured on the system, SELinux might prevent scripts from running if they are symbolic links into a subdirectory in /etc/NetworkManager/dispatcher.d/. To ensure that scripts run, either place scripts directly in the subdirectory, or update the SELinux configuration. For more information, see Oracle Linux: Administering SELinux.

    Because NetworkManager-dispatcher runs scripts in /etc/NetworkManager/dispatcher.d/ in alphabetical order, you can prefix the file name with a number to enforce the execution order of scripts. For example: /etc/NetworkManager/dispatcher.d/10-myscript.

  2. Set ownership and permissions for the file.
    The following settings are required:
    1. Change the file owner to root.
      sudo chown root /etc/NetworkManager/dispatcher.d/10-myscript
    2. Disable write access for group and other, and disable setuid.
      sudo chmod 0700 /etc/NetworkManager/dispatcher.d/10-myscript
When network actions occur, NetworkManager.service starts NetworkManager-dispatcher.service, which runs the scripts in /etc/NetworkManager/dispatcher.d/ following these parameters:
  • One script runs at a time.
  • Scripts run in sequence based on the order in which network events occur.
  • After the dispatcher service queues a script, the script always runs, even if a later action makes the script unnecessary.
  • NetworkManager terminates scripts if they run for too long.