8

In my system I have eth0 (which may or may not be connected) and a modem on ppp0 (which always may be up or down). The the case where both interfaces are up and ppp0 is the default route, I'd like to find a way to determine the gateway IP actual address of eth0. I tried "netstat -rn" but in this configuration the output is:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
xx.xx.xxx.xxx   0.0.0.0         255.255.255.255 UH        0 0          0 ppp0
192.168.98.0    0.0.0.0         255.255.255.0   U         0 0          0 eth0
127.0.0.0       0.0.0.0         255.0.0.0       U         0 0          0 lo
0.0.0.0         0.0.0.0         0.0.0.0         U         0 0          0 ppp0

So how do I determine eth0's gateway address? In the above case the actual gateway address is 192.168.98.1.

4
  • What is "eth0's gateway address" supposed to be? Commented Apr 11, 2014 at 16:52
  • 5
    Interfaces don't have gateway addresses, netblocks do. A gateway address tells the kernel where to forward packets destined for some host on some network. If the address is associated with a given interface then that interface will be used, but the address is independent of the interface.
    – Kyle Jones
    Commented Apr 11, 2014 at 16:52
  • So in my application I am supposed to make eth0 the default gateway if it has connectivity. But to check connectivity I make a temporary route to my known good server. But to do this I need to know eth0's gateway address. So how can I determine this? Commented Apr 11, 2014 at 17:04
  • check this: stackoverflow.com/a/15973156/1422630 Commented Aug 20, 2016 at 1:47

3 Answers 3

5

Your mistake is that you're associating the information on the netstat output with the interface rather than the destination. Destination addresses can have associated gateways. When you configure your network, you're associating interfaces and gateways with sets of destination addresses -- so the question you need to be asking is "what's the gateway for destinations in 192.168.98/24?" I'm betting there isn't supposed to be one, that devices in that range are either bridged or directly connected to that ethernet.

Notice that your default route is via ppp0 (the last line is a wildcard route, the mask is all zero so no bits are actually compared, so packets for those addresses are sent via ppp0). So packets for any destination not in 127.xx.xx.xx or 192.168.98.xx will eventually match the wildcard mask and be sent over ppp0. Now, because it's a point-to-point protocol, every packet sent on that interface goes to the only device directly addressable on that link, the one at the other end of it. There's no need to specify a gateway for it because every packet is sent over that interface is sent via that device, whether its Internet destination is that device or not.

Now: if you have a router accessible over eth0 that you use when you can, that router almost certainly has an IP address in the 192.168.98.xx range, at least as likely as not 192.168.98.1, and when that router's powered up and actually connected to the Internet you want your default route, the one for everything not directly connected to a local network, to be default via 192.168.98.1

You might have the newer (that's a relative term, it's been around for a very long time now) ip command in use, try ip route and if that gives you some output try ip route help. Otherwise, try route help.


So, your eth0 interface simply doesn't have a gateway address. There might be another device or several devices connected to that ethernet willing to serve as a gateway. You say your network's got one that responds on the (purely conventional) .1. No law says it has to have that address, and no law says it has to advertise its presence. There are some common ways, the most common of them being for boxes running routing software to also run dhcp servers and tell anyone who speaks dhcp about themselves that way. MolbOrg's answer gives a pretty typical flavor of the setup, but anything anyone can suggest at this point is only going to be more or less likely to match your particular network.

1
  • in my case the gateway ends with .129 that is quite confusing.. it is determined when the usb tethering is enabled but after that I see no way to determine that (for the other interfaces) after a route -n (where the gateways are not configured (0.0.0.0). EDIT: found it! stackoverflow.com/a/15973156/1422630 Commented Aug 20, 2016 at 1:43
1

Assume eth0 is DHCP client interface.
One option is to check the DHCP client lease files dhcpd.leases
Place and name depends on the system; on some Fedora systems, the files under /var/lib/dhclient/ are lease files, where the interesting string is like that :

  option routers 192.168.1.1;

Another option, which worked for me on a funtoo box: dhcpcd -U eth0
prints a nice table, ready to source in scripts

broadcast_address=192.168.1.255
dhcp_lease_time=86400
dhcp_message_type=5
dhcp_server_identifier=192.168.1.1
domain_name_servers='192.168.1.1 192.168.1.101'
ip_address=192.168.1.101
network_number=192.168.1.0
routers=192.168.1.1
subnet_cidr=24
subnet_mask=255.255.255.0

There other options like dhcping, dhclient -n, according to google and manpages, but they fail on my boxes, but may work for you.

1

Linux:

You'll need to open a Terminal. Depending on your Linux distribution, it can be located in the menu items at the top, or at the bottom of your screen. In this example, we will use Fedora. Click Applications > System Tools > Terminal.

When terminal is open, type the following command: ip route | grep default

The output of this should look something like the following:

joe$ ip route | grep default
default via 192.168.1.1 dev eth0 proto static

In this example, again, 192.168.1.1 is your default gateway (router) IP address.

enter image description here

For more information about this topic how to Find Your Gateway IP(linux/win/osx).

1
  • 2
    Despite not speaking to the "when it is not the default gateway" portion of the question, this answer contains the hint I needed (route -n) in order to obtain the gateway IP address for eth0 in a bash script: route -n | grep "^0\.0\.0\.0" | awk '{print $2}' Commented Dec 10, 2018 at 18:29

You must log in to answer this question.

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