0

I am using several RPix with Debian Buster. All have ETHx and WLANx adatpers installed and are ready to work.

For example: I am using network adpaters WLAN0 and ETH0 - both have static IP-Adresses. Some are physically connected via Ethernet only, others are connected by WiFi only, the rest have both adapters active.

What is the easiest bash command, to find out (independently what the physical connection situation is), which ip address is being used for connection (momentarily)?

Thanks in advance & BR

1
  • You should edit into this the output from ip addr and ip route from one of the Pis you are referring to.
    – goldilocks
    May 28, 2021 at 15:33

1 Answer 1

1

On GNU/Linux systems like Debian the canonical tools for this are from the iproute2 utilities; they are included in a normal base install. These are mostly contained in a single executable, ip, which uses subcommands for specific tasks.

"Which ip address is being used for connection" is not as simple a question as it might seem particularly if you have multiple interfaces configured. There are three things you need to know:

  1. What IP addresses are assigned to what interfaces.
  2. What routes are assigned to what interfaces.
  3. The address the connection is to. This doesn't necessarily need to be exact, depending on the answer to #2, "some address on the internet" might do (not all IP addresses are on what we consider the internet proper).

To see all addresses, use ip address (for short, ip addr or ip a). An example from a Pi 4:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether dc:a6:32:6b:06:a2 brd ff:ff:ff:ff:ff:ff
    inet 10.8.42.2/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::dea6:32ff:fe6b:6a2/64 scope link 
       valid_lft forever preferred_lft forever
3: wlanA: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether dc:a6:32:6b:06:a3 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.124/24 brd 192.168.100.255 scope global noprefixroute wlanA
       valid_lft forever preferred_lft forever
    inet6 fe80::dea6:32ff:fe6b:6a3/64 scope link 
       valid_lft forever preferred_lft forever

The first one, lo, refers to the local loopback, which is a pure software entity that allows networking within the operating system (there is actually a lot of that and it is necessary to the proper functioning of the system) without the need for a physical interface. The base address here is almost always 127.0.0.1, an example of a IP that does not belong to the internet.

The next one in the example is the physical ethernet interface, eth0 (note the names are essentially arbitrary and can be anything, eth0 is just a convention). This has an ip address of 10.8.42.2, with the unusual CIDR suffix of /32, meaning there can only be one address on that network; a bit more about that below. This address is also not on the internet. It is part of the ranges reserved for private networks.

Last is the wifi interface, wlanA, whose address is 192.168.100.124 with a suffix of /24 indicating a network with room for 256 distinct addresses. Worth noting at this point that since this is also a private network, the Pi actually does not have any "internet proper" address assigned to it, which is normal when you are on a LAN behind a router.

Which interface and address is being "used for connection" depends on which connection you are referring to. This is what routing is about, and the system routing table correlates destination address ranges to the interfaces just mentioned. This is output by ip route (or ip r for short):

default via 192.168.100.1 dev wlanA 
10.8.42.1 dev eth0 scope link src 10.8.42.2 
192.168.100.0/24 dev wlanA proto dhcp scope link src 192.168.100.124 metric 303 

The default route is the one that is used if a destination address doesn't fit in any of the other explicit ranges. In this case, there are two explicit ranges, although one (10.8.42.1) is actually only one address. This is because the ethernet in this case is being used for a direct connection to another machine, so there are no other possibilities there (although if it were being used as an uplink there could be).1

The other explicit range is 192.168.100.0/24, the WLAN. Anything destined for it goes through the wifi interface. Notice src address for these two corresponds to the Pi's own addresses as shown by ip addr.

The vast majority of ip addresses obviously won't fit in either of those two ranges, including the entire internet (since 192.168... and 10.8... are private subnet ranges). Any connection there will use the default route, which in this case is also on the wifi interface. Although there is no src address given, we already know the wifi interface only has one, so that is the one which applies. If it the interface had multiple addresses (which is possible but unusual), the routing would be slightly more complicated.

The manual pages for iproute2 use dashes for the individual subcommands, eg. man ip-address and man ip-route for the two above. You will also find years worth of tutorials and discussion around the web about how to use these, what all the details in the output mean, etc. as well as about the general topic of routing.


  1. Very tangential (you can skip this): A better practice with the eth0 interface in this case would probably be to give it a range of 10.8.42.0/30, allowing for four address (necessary to include 10.8.42.2) or /31 and use .0 and .1 instead of .1 and .2. This would save me having to set an explicit route, but in the end it all works much the same anyway.
4
  • Thanks very much for you answer. I have to apologize - evntually I was not clear enough: In general I am familar with the said before - nevertheless thanks very much. Just to simplify: At one of my RPix boards I have forgotten, whether I have connected via Ethernet or Wifi - as all IP-Adresses are static, the command IP adress does not help me, as it shows the static IP-Adresses. It would be different (easier) when the IPs had been assigned by the Router's DHCP mechansim. I am just looking for a simple command which tells me, whether board is momentarily network connected by ETH0 or WLAN0 ?
    – Karl
    May 28, 2021 at 15:04
  • This is an answer to that question. There is nothing in here regarding the difference between a static and dynamic/DHCP assigned address because for everything said above it does not matter. The command which actually shows which connection uses what interface is ip route. In the example above, connections fall into 3 categories, and the routes show which interface each category is currently using. The "network connected" (your words) must have an IP address range in order to exist. There is no network that includes everything...
    – goldilocks
    May 28, 2021 at 15:29
  • ...If the pis are all behind a router and by "network connected" you mean the internet, then this is discussed above. It is almost always the "default route" unless you have done something very weird, or are using different interfaces for different internet addresses. If so, that is the kind of information you need to include in the question. If not, again, it is the default route.
    – goldilocks
    May 28, 2021 at 15:30
  • Note that this does mean not the routes actually work for anything. It just indicates what will be used to try and make a connection. If you want to test actual connectivity you must use ping or something that tries to connect to something.
    – goldilocks
    May 28, 2021 at 15:30

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