What command can you use to find the Gateway IP Address (ie. home router address) for eth0 in Linux?

I need to get the IP address from a command line app to use in a shell script.

  • Are you looking for the external IP address of the router? In the past I've used a screen scraping script to get that sort of thing from the router setup pages. Most home routers have a browser based setup that is easy to access from the inside. It's hard to give a general solution but you can use curl or wget to fetch the page and then use grep & awk to get the IP address.
    – Tom Bascom
    Commented Jun 24, 2009 at 18:29
  • I should have been clearer in my question. Basically, I want to find the gateway IP address that is set to eth0 on a box that has a single ethernet interface. Coincidentally, this should be the IP address of a home, basic, Linksys-type or so, NAT router. I need it so I can ping it in a shell script.
    – J. Polfer
    Commented Jun 24, 2009 at 18:43
  • Seems more complex than what I need, but a cool, guaranteed-to-work idea though.
    – J. Polfer
    Commented Jun 24, 2009 at 18:44

12 Answers 12


To print out only the default gw IP:

route -n | grep 'UG[ \t]' | awk '{print $2}'

To print out route information on all interfaces:

route -n


netstat -rn
  • Wouldn't UG require the interface to be up? I think just G would be better.
    – J. Polfer
    Commented Jun 24, 2009 at 18:39
  • Wanted to let you know - I'm actually using this answer, but with just a G in the grep pattern; didn't have iptools on the machine i need to run this on.
    – J. Polfer
    Commented Jun 24, 2009 at 21:09
  • 2
    You can save yourself the grep, awk can also filter: route -n | awk '{if($4=="UG")print $2}' Commented Dec 20, 2011 at 7:16
  • I would suggest something like route -n|awk '$4~/G/{print $2; exit(0)}' for the first gateway, but ip route get IP is better, because it lists the route which would be really taken to the given IP, which is difficult to find with route alone
    – Tino
    Commented May 24, 2013 at 13:32
  • route is becoming obsoleted. Commented Jan 15, 2022 at 11:58
ip route show dev eth0 | cut -d\  -f3

is my entry :)

  • I would improve this one: ip route show | awk '{print $3}' Commented Jun 24, 2009 at 19:38
  • I dunno... mine's still fewer keystrokes :)
    – MikeyB
    Commented Jun 24, 2009 at 20:57
  • 1
    And cut is ~20 times lighter binary than awk.
    – K3---rnc
    Commented Oct 4, 2015 at 12:06
  • If I leave off the "dev eth0" does it work for either eth0 or wlan0 ? (I would try this if I had a long enough lan cable) Commented May 16, 2018 at 19:39
  • Its worked for me for openwrt os :) Commented May 19, 2020 at 14:22

You can get the system's default gateway from the output of netstat -r or route

  • netstat is becoming obsoleted. ss is the new replacement. Commented Jan 15, 2022 at 11:58
$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface U         0 0          0 virbr1 U         0 0          0 virbr2   U         0 0          0 wlan0   U         0 0          0 virbr0         UG        0 0          0 wlan0

The is your default gateway, pointing to at my place.


Since iproute2 4.14.1, you can also output JSON for a lot of commands.

So this would work:

ip -j route show dev <interface> | jq -r '.[0].gateway'

In your case:

ip -j route show dev eth0 | jq -r '.[0].gateway'

Note that you need jq for this, but depending on your environment that might already be available - I know it was in my case.


I prefer the iproute package:

# get the default route
ip route list | awk ' /^default/ {print $3}'
# get the default route but limit on eth0 (output may be empty)
ip route list dev eth0 | awk ' /^default/ {print $3}'
  • @sheepsimulator: Unfortunately, this is a bad assumption to make. Supermathie's suggestion is better. Commented Jun 24, 2009 at 19:34
  • @Dominic Eidson - Why? There's only one physical ethernet interface on the machine.
    – J. Polfer
    Commented Jun 24, 2009 at 19:40
  • 2
    exactly what is a bad assumption? Commented Jun 24, 2009 at 23:03

The output from route -n or netstat -rn, and search for the destination


anyone shorter than this? =)

ip r | awk '/^def/{print $3}'
  • 2
    Challenge accepted: ip r | awk 'END{print $3}' is three characters shorter. Commented Dec 20, 2011 at 7:13
  • nice, but on my local debian/squeeze box with KVM bridges my default gw is in line one. strange but true. So your cool solution doesn`t work everywhere :-/ (anyhow +1 )
    – ThorstenS
    Commented Dec 20, 2011 at 7:27
  • ip is not a generic UNIX command - so again it won't work everywhere.
    – Mei
    Commented Dec 20, 2011 at 15:55
  • you are right, but in $Subject stands Linux and not UNIX ;) @David: wow, you have really nice blog!
    – ThorstenS
    Commented Dec 20, 2011 at 17:06
  • 2
    read x x i x< <(ip r g 1), result is in variable $i for further use
    – Tino
    Commented May 24, 2013 at 13:18

If you would like to get the gateway from the interfaces file:

gateway=$(grep -P '^\tgateway' /etc/network/interfaces)
gateway=$(echo ${gateway:9})    # Remove "  gateway "   
echo "gateway = $gateway"
if [ $gateway == "" ]; then
    echo "gateway is blank"
    exit -1
  • +1 for mentioning files, since those can sometimes have a more canonical/true answer you are actually interested in, while the interface itself could have been configured any number of things. For example, on some systems you can look into /var/lib/dhclient/dhclient--eth0.lease to get the gateway that DHCP shared for eth0, even if something/someone else has set another gateway. This doesn't come up unless you're manually tinkering or doing something really weird, but it's definitely part of a complete answer.
    – mtraceur
    Commented Jun 9 at 16:29
netstat -rn |awk '{if($1=="") print $2}'

this will cleanly print the gateway IP. (what would linux scripting be without awk?)

  • Then you'd have to use sed instead... netstat -rn|sed -n '/^{ s/^[0. ]*//; s/ .*$//; p; }
    – Mei
    Commented Dec 20, 2011 at 15:53

My one liner command to get the default gateway IP address :

route | grep default | awk '{print $2}'

Open Terminal on your Linux OS It’s usually located in the top bar or bottom bar, depending on the Linux distribution you use Once you have opened the Terminal window, enter the following commands “ip route | grep default” Now wait a second for the output and note your default gateway address on the screen Your gateway address should look something like Once you know it, you can start configuring your router via its web admin panel.

If this didn't fix your network, you may need a new router.

You must log in to answer this question.

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