LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   How Do I Find My Gateway Address (https://www.linuxquestions.org/questions/linux-networking-3/how-do-i-find-my-gateway-address-4175445149/)

Brandon9000 01-11-2013 10:02 AM

How Do I Find My Gateway Address
 
Everyone says to use:

route -n

or

netstat -nr

but no one explains which line to take or why.

Thanks.

Brandon

camorri 01-11-2013 10:16 AM

From my current location.

Code:

netstat -r
Kernel IP routing table
Destination    Gateway        Genmask        Flags  MSS Window  irtt Iface
default        c-71-203-6-1.hs 0.0.0.0        UG        0 0          0 eth0
71.203.6.0      *              255.255.254.0  U        0 0          0 eth0
loopback        *              255.0.0.0      U        0 0          0 lo

The second line, 'default, next column is the current gateway.

How to know what should be there depends on where you are connected.
When I took that info, I was directly connected to the modem supplied by Comcast, so the gateway addr was set when my system did a DHCP request for an IP address. I got an IP, and the Gateway plus the DNS server addresses.

If I were connected at home, the IP address for gateway is set in mt router config. I got the IP address from the ISP. All the ones I have dealt with provide that if you talk to them, or look on their web site. At home, the gateway is set to the IP address of the router connection on the lan side ( not the ISP side ).

Does that help?

Brandon9000 01-11-2013 10:18 AM

Thank you, Cliff. How could I get the gateway in numeric IP format?

colucix 01-11-2013 10:20 AM

Check the line with UG flags. U means the route is up, G means it's a gateway. The following should retrieve the gateway:
Code:

route -n | awk '$4=="UG"{print $2}'

Brandon9000 01-11-2013 10:34 AM

Thanks. This is exactly what I need!

Brandon9000 01-11-2013 10:36 AM

Wait, will the default gateway always show 0.0.0.0 for the destination? If there is more than one gateway, is there some way to distinguish the default and still get an IP?

camorri 01-11-2013 11:53 AM

The gateway will be a dotted decimal number in most cases. Your ISP will provide it. If you connect to a network you do not control ( school, public network etc ) the DHCP request will provide the gateway IP.

No it should not stay at 0.0.0.0

jnihil 01-12-2013 01:36 AM

if you have more than one default gateway, the one with the lowest metric will get used:

$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.254 0.0.0.0 UG 0 0 0 eth0
0.0.0.0 192.168.42.129 0.0.0.0 U 500 0 0 usb0


handy for back-up routes.


All times are GMT -5. The time now is 05:17 AM.