LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 01-11-2013, 10:02 AM   #1
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Rep: Reputation: Disabled
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
 
Old 01-11-2013, 10:16 AM   #2
camorri
LQ 5k Club
 
Registered: Nov 2002
Location: Somewhere inside 9.9 million sq. km. Canada
Distribution: Slackware 15.0, current, slackware-arm-currnet
Posts: 6,215

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
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?
 
Old 01-11-2013, 10:18 AM   #3
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Original Poster
Rep: Reputation: Disabled
Thank you, Cliff. How could I get the gateway in numeric IP format?
 
Old 01-11-2013, 10:20 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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}'
 
Old 01-11-2013, 10:34 AM   #5
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Original Poster
Rep: Reputation: Disabled
Thanks. This is exactly what I need!
 
Old 01-11-2013, 10:36 AM   #6
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Original Poster
Rep: Reputation: Disabled
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?
 
Old 01-11-2013, 11:53 AM   #7
camorri
LQ 5k Club
 
Registered: Nov 2002
Location: Somewhere inside 9.9 million sq. km. Canada
Distribution: Slackware 15.0, current, slackware-arm-currnet
Posts: 6,215

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
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
 
Old 01-12-2013, 01:36 AM   #8
jnihil
Member
 
Registered: Dec 2012
Location: inside the matrix
Distribution: Debian, Xubuntu, Gentoo, Antergos
Posts: 90

Rep: Reputation: 27
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.
 
  


Reply

Tags
gateway, networking



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Howto find gateway address through code /usr/src Linux - Networking 9 03-18-2013 12:39 PM
find out gateway address daedlock Linux - Networking 1 04-16-2012 02:25 AM
how do i find my gateway address on eth0? seth Linux - Networking 10 03-21-2010 03:42 AM
Router gateway address being picked up as primary DNS address under SuSe Hubmasterflex Linux - Networking 13 10-18-2008 01:40 PM
What is Gateway address and what is Network address? abefroman Linux - Networking 1 03-17-2004 08:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 02:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration