1

I have an embedded Linux system which is setup to use DHCP for eth0, but also has a link-local IP address configured as a fallback (eth0:1) just in case no IP address was obtained from DHCP.

Is there a simple way to check, from a shell script, which IP address is in use?

The closest I've got is this:

$ ip route get 1
1.0.0.0 via 192.168.0.100 dev eth0  src 192.168.0.133
    cache
$ ifconfig eth0 0.0.0.0   # Simulate no IP obtained from DHCP
$ ip route get 1                                                       
1.0.0.0 via 192.168.0.100 dev eth0  src 169.254.197.131   
    cache

This correctly identifies the IP address in use ('src' field). However this only works if a default gateway is configured, which will not be the case if DHCP was not available for some reason.

How can I find out which IP address is in use even if no default gateway is configured ?

  • 1
    What do you mean by "in use"? That the IP exist in some interface? That the IP is listenning on some port? or that the IP is the endpoint of some existing connection?. Those are all very different propositions. – Arrow Jun 29 '17 at 12:30
  • You might look for valid arp table entries: ip neighbour show dev eth0 nud reachable. You could send out a broadcast on both ips first if needed. – meuh Jun 29 '17 at 12:34
  • @Arrow Which IP address would be "preferred" for example to establish an outgoing connection. That is, the same info I am getting with the ip route get command I am using, except that I need to be able to get this information also when the default gateway is not configured. – Grodriguez Jun 29 '17 at 13:18
1

The command ip route get ADDRESS does work even if no default gateway is configured. For example, if the following addresses are configured on eth0: 10.0.7.139/24, 169.254.1.2/16, and the default route is deleted, then

$ ip route get 10.0.7.222
10.0.7.222 dev eth0 src 10.0.7.139 
    cache

$ ip route get 169.254.55
169.254.55.0 dev eth0 src 169.254.1.2 
    cache

$ ip route get 1
RTNETLINK answers: Network is unreachable

The last answer is correct: there is no way to reach a host with any of the addresses 1.x.x.x, if no route to it exists. It is not the ip route get command that is not working, it is the network configuration that is wrong.

Note that "link-local" means just that, an address that is intended to be used only on the the Ethernet link eth0 is connected to. It is not supposed to be used on an "outgoing" connection.

Edit: clarifying comment added below.

The routing table, and only the routing table, is what determines how a packet starts its journey to its destination. A route is always involved when sending an IP packet, even if the destination is on the local link or even the local host. Routing table entries also contain a source address, and this address of the matching route is chosen as the source IP address of the transmitted packet.

There may be several ip addresses assigned to the same interface, even from the same address range, but only one of them (the primary address) is listed in the matching routing table entry.

Thus ip route get (or indirectly ip route) gives the definitive answer to your question. From the ip-route man page: "this command gets a single route to a destination and prints its contents exactly as the kernel sees it."

  • But these examples do not provide any useful information. Obviously in your examples ip route get 10.0.7.222 will use 10.0.7.139 and ip route get 169.254.55 will use 169.254.1.2. What I am trying to find is what is the "preferred" address. – Grodriguez Jun 30 '17 at 10:07
  • ip route get ADDR shows it, if there is a route to the destination. If there is no route, there is no preferred source address, because there is no way to send a packet to the destination. – Johan Myréen Jun 30 '17 at 12:11
  • And my question is, is there a way to know (not necessarily involving ip route) which address would be used if for both of them there was a valid route to the destination. In other words, which is the "preferred" address if there is more than one candidate. – Grodriguez Jul 3 '17 at 7:15
  • @Grodriguez I added some clarifying text to my answer. – Johan Myréen Jul 3 '17 at 9:09
  • ifconfig eth0:1 192.168.1.101 up; ifconfig eth0:2 192.168.1.102, then lets suppose I want to ping 192.168.1.103. The kernel will pick one of the two. So there must be some concept of "preferred" address, or priorities, or whatever it is called. – Grodriguez Jul 3 '17 at 9:36

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.