Classic Solution:
For debian based systems like ubuntu commands based on the network state can be set as a rule under /etc/network/interfaces
; this answer demonstrate the case of two interface eth0
and wlan0
- With sudo, open
/etc/network/interfaces
with your text editor (that file contain rules and settings for your network interfaces also note that network-manager is supposed not to manage interfaces listed on this file)
sudo notepadqq /etc/network/interfaces
- Use, (
pre-up
and pre-up
) or (post-down
and post-up
) according to your needs to turn down the wlan0 interface when the network wire is plugged/unplugged you can use ifconfig wlan0 down
or ip link set wlan0 down
(the example is using ifconfig)... example:
auto eth0
allow-hotplug eth0
iface eth0 inet static
address 192.168.1.7
netmask 255.255.255.0
gateway 192.168.1.1
post-up ifconfig wlan0 down
post-down ifconfig wlan0 up
Systemd Alternative Solution:
Official documentation here explain what is supported by systemd and thus a direct easy solution like the classic one is not implemented as of 05/2019
Systemd Alternative Solution:
It's also possible to use the dispatcher to trigger a script as explained on this answer, that one or this article
Systemd Alternative Solution:
As you linked in the comment this answer can be an alternative solution implemented with a service
Related:
Network target here and here could be used to run other service according to the state of the network
systemd
directly, they are rather handled by so-called "network manager services", likeNetworkManager
, orifupdown
, or perhaps systemd's own optionalsystemd-networkd
service, although this latter is currently not sophisticated enough to follow cable events and thus requires an additional dedicated service. Thus you can perhaps specify which network manager(s) is acceptable for your setup ? – LL3 May 14 '19 at 15:13