On a multihomed Linux machine, how can I find out what network interface will be used to send a packet to a specific host? I need to do this programmatically and I'd rather not parse and interpret the routing table myself.

  • That's what routing table is for. I don't imagine it otherwise. – GioMac Aug 17 '13 at 18:02

Use ip route for this. For instance:

ip route show to match
  • Perfect! Thank you. – Rob H Aug 17 '13 at 19:49

Shorter option:

ip route get

Yes, as Michael Hampton suggests, use ip route. If you only want the interface, use this

ip -o route get $ip | perl -nle 'if ( /dev\s+(\S+)/ ) {print $1}'

For example:

# ip=
# iface=$( ip -o route get $ip | perl -nle 'if ( /dev\s+(\S+)/ ) {print $1}' )
# echo $iface

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.