how to find the gateway used for routing

47

8

This is in unix environment. I have multiple routes in a host (visible with 'ip route show' command). If I am pinging an address, how can I find out which gateway was used to route the tracffic?

I tried using traceroute command, but it does not show the immediate gateway.

From below output, 10.58.227.1 is my default gateway.

# ip r l
10.58.227.0/24 dev front_eth1  proto kernel  scope link  src 10.58.227.231
169.254.0.0/17 dev bond0  proto kernel  scope link  src 169.254.0.4
default via 10.58.227.1 dev front_eth1  proto gated

When I do traceroute to an external address, the gateway used (default gateway 10.58.227.1) is not shown in output.

# traceroute -n -I 10.63.21.118
traceroute to 10.63.21.118 (10.63.21.118), 30 hops max, 40 byte packets
 1  10.58.112.1  0.507 ms  1.008 ms  1.017 ms
 2  10.63.21.118  0.228 ms  0.233 ms  0.234 ms

Is there any option to view the same information as given by traceroute command, including the gateway used for routing?

divya

Posted 2012-03-12T08:31:39.070

Reputation:

Answers

76

You can use the ip route get <address> command to ask the kernel to report the route it would use to send a packet to the specified address:

$ ip route get 4.2.2.1
4.2.2.1 via 192.168.0.1 dev eth0  src 192.168.0.121 
    cache 
$ 

192.168.0.1 is my default route. If I ask for an address that would not go over the default route:

$ ip route get 192.168.0.116
192.168.0.116 dev eth0  src 192.168.0.121 
    cache 
$ 

sarnold

Posted 2012-03-12T08:31:39.070

Reputation: 2 988

Random side question - If 192.168.0.1 is "default route" or "gateway"... what name would be used to refer to 192.168.0.121 ? Just "source" ? – dtmland – 2016-08-02T23:25:51.873

@sarnold, please confirm whether it needs internet on that server. – kd12 – 2017-07-04T07:31:31.937

@kd12 you only need an Internet connection on that server if you wish to route packets to the Internet. You can set up IP networks without being connected to the Internet if you wish; the routing tables don't know and don't care, it all works the same. :) – sarnold – 2017-07-07T20:26:37.057

Thaks a lot. :) That solves the issue. – None – 2012-03-12T08:40:39.160