Detecting whether netplan is managing network config (shell)

10,398

You may be able to check for the ifstate file from ifupdown, which will tell you whether that's what was used to configure the network:

/run/network/ifstate

If that's there and it contains text, you can expect that ifupdown is in use. If it's not present, the system is likely managed via netplan (but that won't tell you whether it's NetworkManager or systemd)

Then, if you want to know what backend is used for a particular interface, you can ask netplan, and parse the output to get the information you need:

netplan generate --mapping enp3s0

It outputs text like this:

id=enp3s0, backend=NetworkManager, set_name=(null), match_name=enp3s0, match_mac=(null), match_driver=(null)

That way you can also know using what matching rules the interface was configured (you use the "final" name of an interface to check the mapping, it tells you what matching rule is in the netplan YAML that matches for it).

Share:
10,398

Related videos on Youtube

Arni J
Author by

Arni J

Updated on September 18, 2022

Comments

  • Arni J
    Arni J over 1 year

    I would like to figure out in a shell script in an LXD container, whether the container is being managed by Netplan. This can be a bit of an unknown as I have a mix of 16.04 systems 18.04 systems and upgraded 18.04 systems.

    In order to write correct network config to the container, I need to detect if the config is static in /etc/network/interfaces or in a Netplan yaml file.

    I could really use something like netplan status but since that command does not exist I need alternatives.

    I have asked around a fair bit, and there seems to be no official way.The suggestions I have had so far:

    1. cat /etc/network/interfaces and look for the Netplan info message there. I don't like this approach, as this would obviously be prone to breakage.
    2. Someone suggested I could just look for /etc/netplan - but that will not work on upgraded systems as there Netplan is disabled but still installed.
    3. Another suggestion was the inverse: that I could check if ifupdown or systemd is managing the network. Problem is, I am not sure how to do this in a practical sense.
  • Arni J
    Arni J about 6 years
    Thank you for your thorough reply. I am now looking for /run/network/ifstate to ascertain what network config to write, and everything seems to work well :)
  • Stephen Warren
    Stephen Warren almost 4 years
    On my 20.04 system, /run/network/ifstate exists and contains text (lo=lo), yet I'm fairly sure that netplan is in use. Does the answer need to be modified not to say "and contains text", but rather "and contains the interface name in question"? Or am I wrong, an my system actually doesn't use netplan? netplan generate --mapping eth0 does show backend=networkd so I'm pretty sure that means netplan is in use...