3

I would like to DOWN wlan0 if the Ethernet cable is plugged in. I am using an Ubuntu system - which uses systemd. Thus I would like a systemd-friendly way to do this.

I also want to do the opposite. I would like to UP wlan0 when the Ethernet cable is unplugged.

Question regarding this - Do I also need to update the routing table?


2
  • 1
    Cable events (plugged/unplugged) are not handled by systemd directly, they are rather handled by so-called "network manager services", like NetworkManager, or ifupdown, or perhaps systemd's own optional systemd-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
  • The comment above is an answer -- Cable events are NOT handled by systemd ... BUT there are ways to do it (which you point to). – Xofo May 14 '19 at 21:25
2
+50

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

  1. 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
  1. 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

3
  • Thank you - The solution -must- be systemd specific. I will examine these. – Xofo May 13 '19 at 22:55
  • 2
    Yes i know just writing a complete answer to make the answer helpful for all ;) just give me 10/15 min to finish writing – intika May 13 '19 at 22:57
  • 1
    done, you can now have a global image ;) – intika May 13 '19 at 23:36
1

these might be helpful : Link Link2. Your script might be one-liner (something line. ip link set yourIface down ) Routing table should update automatically , unless you are using some static routes then probably you have to update it.

4

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.