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 |
From my current location.
Code:
netstat -r 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? |
Thank you, Cliff. How could I get the gateway in numeric IP format?
|
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}' |
Thanks. This is exactly what I need!
|
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?
|
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 |
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. |