4

Because I am using Bionic Beaver/Ubuntu 18.04, the network settings in /etc/network/interfaces are being ignored and the settings in /etc/NetworkManager/system-connections/'eth0' are being used for i) static ip ii) gateway etc.

I wish to restart the sshd service every time the interface comes up. It doesn't work in the normal place /etc/network/interfaces since I'm using Gnome. Where can I place a NetworkManager script to be run every time a specific interface comes up?

2
  • 1
    /etc/network/interfaces no longer controls networking in 18.04. It is managed by netplan. I suggest that you transfer your settings there. netplan.io/examples
    – chili555
    Jan 21, 2019 at 14:47
  • @chili555 not 100% accurate. The /etc/netplan/*.yaml file can be set to use NetworkManager, just like the good old days.
    – heynnema
    Jan 21, 2019 at 18:43

2 Answers 2

8

The solution is to create dispatchers scripts in /etc/NetworkManager/dispatcher.d. For example, you could log events in journald by placing the following script at /etc/NetworkManager/dispatcher.d/log-iface-events.sh:

#!/usr/bin/env bash

interface=$1
event=$2

echo "$interface received $event" | systemd-cat -p info -t dispatch_script

Remember to give it execution permissions:

chmod +x /etc/NetworkManager/dispatcher.d/log-iface-events.sh

The bad news is that scripts are no longer tied to a given interface or events such as up or down. Hence, you must check all of that in your script. If you want this script to run only for eth0, you must filter that by hand putting something like the following in your script:

[[ $interface == "eth0" ]] || return 0

For example:

#!/usr/bin/env bash

interface=$1
event=$2

if [[ $interface != "eth0" ]] || [[ $event != "up" ]]
then
  return 0
fi

# place your commands bellow this line

Will run only if it is dealing with up events for eth0 interface.

You can have many scripts. According to man 8 networkmanager, scripts will run in alphabetical order. This seems to include the scripts in subdirectories. You MUST read this manpage.

7
  • 1
    Inside my /etc/NetworkManager/dispatcher.d I have 3 sub-directories and one executable named 01-ifupdown. Exactly where should I put my commands? :/
    – corinjg
    Jan 21, 2019 at 13:56
  • @JSStuball I made changes to address your question. Jan 21, 2019 at 15:15
  • Please could you(/someone) address 1) how the choice of script filename affects behaviour, and 2) why the 01-ifupdown is the sole executable file in the dispatcher.d directory after installation?
    – corinjg
    Jan 21, 2019 at 16:31
  • @JSStuball I addressed the first point. About the second, it seems to bridge the functionalities of the old /etc/network/if-up.d and /etc/network/if-up.d with network-manager, but I suspect it will be deprecated soon. Jan 27, 2019 at 2:59
  • I am trying to write a script using this, but my NetworkManager only reacts to event := down when nmcli detects the connection loss, and event := up when nmcli confirms a DHCP configuration has been accepted. How can I make the dispatcher react at the moment where nmcli switches from state := unavailable to state := connecting ?
    – KlaymenDK
    Jun 2, 2020 at 10:53
0

network-manager-connection-action connects to dbus and listen for connections state changes. Running using user's privileges.

Shameless plug, I'm the author

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.