How to see on Linux what network interface and source IP address is used for a route to a specific destination host?

25,702

Use ip route get <ip>.

Share:
25,702

Related videos on Youtube

sandoz
Author by

sandoz

Updated on September 17, 2022

Comments

  • sandoz
    sandoz almost 2 years

    If I have multiple network interfaces (here: 2) on a Linux machine (here: Debian Lenny). How do I see, over what network interface (NIC) a route to a specific destination host is going and what source IP address is used by default?

    I have though of using

    ping -I nic1 desthost.example.com
    ping -I nic2 desthost.example.com
    

    too see if both ways are possible. (Here: Both ways are possible)

    I looked up the routing table

    ip route show
    

    But it's quite complex, so I thought, there must by a small simple tool, to just tell me:

    "To destination host desthost.example.com it takes interface nicX and source IP address 10.0.0.1"

    What is the simplest way of getting this information?

    (And I'd rather not use tcpdump and set the interfaces in promiscous mode.)

    Thanks.

  • mivk
    mivk almost 10 years
    And to get only the interface: ip -o route get $ipaddress | perl -nle 'if(/dev\s+(\S+)/) {print $1}'
  • ShadowFlame
    ShadowFlame over 9 years
    or ip -o route get $ipaddress | awk '{ print $5 }'
  • madCode
    madCode over 8 years
    route is deprecated. use iproute2.
  • Per Lundberg
    Per Lundberg about 4 years
    What is nm-tool and in what package is it provided? Something related to NetworkManager?
  • Nasir
    Nasir almost 3 years
    ip -6 route get 240b:c020:104:5422:bb27:2:1d:134 for example doest print anything. But its pingable
  • Admin
    Admin about 2 years
    Recent Linuxes dropped netstat in favor of ss.
  • Admin
    Admin about 2 years
    Yes, it required NetworkManager (which is not installed universally).
  • Admin
    Admin about 2 years
    Please make the answer somewhat more complete (e.g. explain). If I try the command I just get a header.