19

The BSD route command has a feature that will show what route will be selected for a given host. For example:

/Users/mhaase $ route get google.com
   route to: iad23s07-in-f8.1e100.net
destination: iad23s07-in-f8.1e100.net
    gateway: 10.36.13.1
  interface: en0
      flags: <UP,GATEWAY,HOST,DONE,WASCLONED,IFSCOPE,IFREF>
 recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire
       0         0         0         0         0         0      1500         0 

I occasionally find this useful if I am manually messing with routing tables to make sure that the routing rules are working as expected.

The GNU version of route does not have this same "get" subcommand. Is there some equivalent or alternative for GNU/Linux?

1
  • 1
    I was looking for the /BSD/ equivalent of ip ro but this page still suffices :) Feb 17 '16 at 15:07
26

There is

ip route get 74.125.137.100

but it doesn't do hostname resolution (which I think is a good thing). The command is usually available from iproute or iproute2 packages.

7
  • 3
    ip route get "$(dig google.com +short)" for hostname resolution.
    – jordanm
    Oct 31 '12 at 17:28
  • 5
    @jordanm, More like: dig google.com +short | xargs -tn1 ip route get Oct 31 '12 at 20:16
  • 1
    @StéphaneChazelas, please let me know this above linux command required an internet or it fetches the result from any routing table/cache ?
    – kd12
    Jul 3 '17 at 6:51
  • The command 'dig' does a DNS lookup. That may require network access unless the DNS entry is in the local cache.
    – WillW
    Mar 16 '18 at 16:28
  • 1
    What a nonsense(?) — according to your view on UNIX philosophy ping shouldn't have ability to resolve by DNS name as well. It should be chain-nested with xargs just as with example above. — Pretty absurd take on UNIX philosophy… but, well, ok, thanks for explanation.
    – poige
    Apr 2 '18 at 16:21

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.