21

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.

4 Answers 4

44

Use ip route get <ip>.

3
  • 1
    And to get only the interface: ip -o route get $ipaddress | perl -nle 'if(/dev\s+(\S+)/) {print $1}'
    – mivk
    Jul 6, 2014 at 13:32
  • 2
    or ip -o route get $ipaddress | awk '{ print $5 }' Mar 23, 2015 at 10:53
  • ip -6 route get 240b:c020:104:5422:bb27:2:1d:134 for example doest print anything. But its pingable
    – Nasir
    Aug 4, 2021 at 1:17
0

what about route -C

2
  • route is deprecated. use iproute2.
    – madCode
    Mar 8, 2016 at 3:42
  • Please make the answer somewhat more complete (e.g. explain). If I try the command I just get a header.
    – U. Windl
    Jun 14, 2022 at 13:03
0

I use netstat -Wcatnp . You should try this command.

1
  • Recent Linuxes dropped netstat in favor of ss.
    – U. Windl
    Jun 14, 2022 at 13:00
0

Even easier, I'd say nm-tool and if you only want the relevant line nm-tool | grep Device. That should output:

- Device: wlan0  [My router name] -----------------------------------------------
2
  • 1
    What is nm-tool and in what package is it provided? Something related to NetworkManager? Jun 17, 2020 at 11:39
  • Yes, it required NetworkManager (which is not installed universally).
    – U. Windl
    Jun 14, 2022 at 13:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .