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:
- What IP addresses are assigned to what interfaces.
- What routes are assigned to what interfaces.
- 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.
- 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.
ip addr
andip route
from one of the Pis you are referring to.