0

How can I know what the router's address is when I am on multiple interface? I know by using netstat -rn I can have the dominating interface's gateway, which is the router's address. But what about other interface.

For my case, I am on eth0 and wlan0 on different router, OS chooses eth0 as default (and I cannot find wlan0's gateway), but is there a universal way that I can query an interface's router address?

$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 wlan0
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U         0 0          0 wlan0

$ ifconfig
eth0      Link encap:Ethernet  HWaddr b4:b5:2f:32:f3:22  
      inet addr:192.168.1.132  Bcast:192.168.1.255  Mask:255.255.255.0
      inet6 addr: fe80::b6b5:2fff:fe32:f322/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:22 errors:0 dropped:0 overruns:0 frame:0
      TX packets:53 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:3035 (3.0 KB)  TX bytes:9857 (9.8 KB)
      Interrupt:17 Memory:d4400000-d4420000 

lo        Link encap:Local Loopback  
      inet addr:127.0.0.1  Mask:255.0.0.0
      inet6 addr: ::1/128 Scope:Host
      UP LOOPBACK RUNNING  MTU:65536  Metric:1
      RX packets:569 errors:0 dropped:0 overruns:0 frame:0
      TX packets:569 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0 
      RX bytes:53950 (53.9 KB)  TX bytes:53950 (53.9 KB)

wlan0     Link encap:Ethernet  HWaddr 60:67:20:00:56:6c  
      inet addr:192.168.2.115  Bcast:192.168.2.255  Mask:255.255.255.0
      inet6 addr: fe80::6267:20ff:fe00:566c/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:8152 errors:0 dropped:0 overruns:0 frame:0
      TX packets:4030 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:6316155 (6.3 MB)  TX bytes:574275 (574.2 KB)
1

I think you want the ip route command. If you invoke ip route on my server, which has two ethernet cards, "enp3s0" and "enp3s8", you get this:

1001 % ip route
default via 10.0.0.1 dev enp3s8
10.0.0.0/24 dev enp3s8  proto kernel  scope link  src 10.0.0.3
192.168.1.0/24 dev enp3s0  proto kernel  scope link  src 192.168.1.148

That shows you all the routes. It seems you can get a route for a specific device name like this:

[root@splunge ~]# ip route list dev enp3s8
default via 10.0.0.1
10.0.0.0/24  proto kernel  scope link  src 10.0.0.3

The "default via" line shows you the router's IP address.

That's all on an Arch linux box. ip is part of the iproute2 package, and it's /usr/bin/ip. On an older RHEL box, it's /sbin/ip, and I have no idea what package it's from.

  • Do you know how I can get the router address for enp3s0 in your case? Which is not the dominant connection right now. – Lingyuan He Apr 2 '15 at 2:58
  • @LingyuanHe - You could do: ip route | grep enp3s0 | awk '{print $9}' – Bruce Ediger Apr 2 '15 at 22:26
1

You can use traceroute and specify the device to use with -i, then the next hop will be the router connected to that device.

  • Only if the next hop is already in the routing table. This doesn't help discover it. – roaima Mar 10 '15 at 21:57
0

Best way I found for that matter is to print the dhcp configuration files, found on /var/lib/dhcp/dhclient.(interface).leases, and look for the routers definition there

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.