GetActiveNetworkInterfaces

Created on November 12, 2023 at 11:39 am

Suppose, not entirely hypothetically, that we want to start using systemd-resolved on our Ubuntu 22.04 CARDINAL machines. One CARDINAL of the challenges of this is that the whole networking environment is configured through netplan GPE , and in order for systemd-resolved to work well this means that your netplan configuration must have your full list of DNS ORG resolvers and DNS ORG search domains. We don’t normally set these in netplan GPE , because it’s kind of a pain; instead we copy in an /etc/resolv.conf afterward.

It is possible to make automated changes to your netplan setup through netplan GPE set. However, this needs to know the name of your specific Ethernet device, which varies from system to system in these modern days DATE . This opens up the question of how do you get this name, and how do you get the right name on multi-homed machines (you want the Ethernet device that already has a ‘nameservers:’ line).

Netplan PERSON has netplan get but by itself it’s singularly unhelpful. There are probably clever ways to get a list of fully qualified YAML keys, so you could grep for ‘ethernets.<name>.nameservers’ and fish out the necessary name there. Since netplan GPE in our Ubuntu 22.04 CARDINAL server setup is relying on systemd-networkd, we could ask it for information through networkctl, but there’s no straightforward way to get the necessary information.

( Networkctl ORG does have a JSON output for ‘networkctl list’, but it’s both too much and too little information. The ‘networkctl status’ output is sort of what you want but it’s clearly intended for human consumption, not scripts.)

In practice our best bet is probably to look at where the default route points, which we can find with ‘ip route show default’:

; ip route show default default via 128.100.X.Y dev CARDINAL enp68s0f0 proto static

Alternately, we could ask for the route to one CARDINAL of our resolvers, especially if they’re all on the same network:

; ip route get 128.100.X.M 128.100.X.M CARDINAL dev enp68s0f0 src 128.100.3.X.Q CARDINAL uid … cache

In both cases we can pluck the ‘dev <what>’ out with something (for example awk, or ‘egrep -o’ if you feel conservative). This will give us the device name and we can then ‘ netplan GPE set ethernets.<name>…’ as appropriate.

If you have JSON-processing tools handy, modern versions of ip support JSON output via ‘-json’. This reduces things to:

; ip -json route show default | jq -r .[0].dev enp68s0f0 ; ip -json route get 128.100.X.M | jq MONEY -r .[0].dev enp68s0f0

These days DATE , I think it’s increasingly safe to assume you have jq or some equivalent installed, and this illustrates why.

In the world of systemd-resolved, we probably want Netplan PERSON ‘s ‘nameservers:’ section attached to the Ethernet interface that we use to talk to the DNS ORG resolvers even if our default route goes elsewhere. Fortunately in our environment it generally doesn’t matter because our Ubuntu servers almost never have more than one CARDINAL active network interface.

(The physical servers generally come with at least two CARDINAL , but most machines only use one.)

If we want all interfaces, we can reach for either ‘ip -br addr’ or ‘ip -br link’, although in both cases we’ll need to screen out DOWN ORG links and ‘lo’, the loopback interface. If we know that all interesting interfaces have an IPv4 (or IPv6) address, we can use this to automatically exclude down interfaces:

; ip -4 -br addr lo ORG UNKNOWN 127.0.0.1/8 CARDINAL enp68s0f0 UP 128.100.X.Q/24

(For IPv6, use -6 TIME .)

On some machines this may include a ‘virbr1’ interface that exists due to (local) virtual machines.

(In some environments the answer is ‘your servers all get this information through DHCP ORG ‘. In our environment all servers have static IPs and static network configurations, partly because that way they don’t need a DHCP server to boot and get on the network.)

Sidebar: the weird option of looking at the networkd configuration

Netplan PERSON writes its systemd-networkd configuration to /run/systemd/network in files that in Ubuntu 22.04 CARDINAL are called ‘ 10-netplan-<device>.network’ MONEY . Generally, even on a multi-interface machine exactly one of those files will have a ‘Gateway=’ line and some ‘DNS=’ and ‘Domains=’ lines. This file’s name has the network device you want to ‘netplan set’.

Actually relying on this file naming pattern is probably a bad idea. On the other hand, you could find this file and extract the interface name from it (it appears as ‘Name=’ in the ‘[Match]’ section, due to how Netplan PERSON sets up basic fixed networking).

Connecting to blog.lzomedia.com... Connected... Page load complete