Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to get the default gateway, using the destination

i used this command: netstat -rn | grep

and it returns this list:

Destination - Gateway      - Genmask         - Flags - MSS - Window - irtt - Iface   -      - - UH    - 0     0        0    - tun0  -      -     - U     - 0     0        0    - eth0     - -         - UG    - 0     0        0    - eth0

My goal here is to ping the default gateway using destination; thus, that is ""; but this one returns a list because of using 'grep'.

Question is: How do i get the default gateway only? I will need it for my bash script to identify if net connection is up or not.

share|improve this question
up vote 15 down vote accepted
DEFAULT_ROUTE=$(ip route show default | awk '/default/ {print $3}')
ping -c 1 $DEFAULT_ROUTE

This should solve your problem.

share|improve this answer
thanks a works. =) – Suezy Jul 30 '09 at 7:38
@Suezy Ha! See? awk! – Jeremy Powell Jul 30 '09 at 7:54
lol..yea. =) thanks too. – Suezy Jul 30 '09 at 7:57
close single-quote / close bracket mismatch on the end I think - works great otherwise though! – Mark K Cowan Aug 21 '13 at 20:01

ip route show default should do the trick for the default gateway, or you can use ip route get <someip> to see what the route for a particular IP address is.

share|improve this answer
i tried ip route and it displays the list for all the gateways.. and yes, the default one is also displayed on the bottom. thanks for that. =) But my goal here is to get the IP address used for that default gateway. Using bash script, I will ping that one to check if its up or not. – Suezy Jul 30 '09 at 7:05
I get four entries for "show default". "ip route show default | awk '/default/ {print $3}'" should get you the IP address alone. – David Pashley Jul 30 '09 at 7:06
 ip route show default

This command is much more quicker then ip route | grep default on system with many (10000+) routes

Look for example from my router with full BGP and manual default route as backup

router:~# time ip route show default
default via XXX.XXX.192.254 dev eth0.123 

real    0m0.707s
router:~# time ip route | grep default
default via XXX.XXX.192.254 dev eth0.123 

real    0m8.596s
  • because ip route show default show all routes for me not only default route
  • because ip route get not accept default or as parameter
share|improve this answer
+1 for demonstrating direct native filtering via iproute2! there are so many cases where this amazing utility suite is more accurate, reliable, and performant than (admittedly creative) legacy Linux textfu – RubyTuesdayDONO Mar 9 at 15:39

This is how I do it:

GATEWAY_DEFAULT=$(ip route list | sed -n -e "s/^default.*[[:space:]]\([[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\).*/\1/p")
share|improve this answer

If you want to do this using netstat (so it'll work on something that's NOT Linux) the general solution is:

netstat -rn | grep '^\(default\|0\.0\.0\.0\)' | awk '{print $2}'

This shows the routing tables, using IPs rather than resolving hostnames (netstat -rn),
looks for the default gateway (lines beginning with or default),
and prints the gateway host (second field of the results).

The proliferation of \ characters in the regular expression are significant:

  • \( and \) specify a group
  • \| is the alternation character (the thing on the left or the thing on the right matches)
  • the dots in the IP address are represented by \. - meaning the literal character . as opposed to a Regular Expression . (matching any single character)
share|improve this answer
grep -e or egrep would also do the trick without needing the brackets and the pipe to be escaped egrep '^(default|0\.0\.0\.0)' – Mark K Cowan Aug 21 '13 at 20:04
 ip route show default | grep default | awk {'print $3'}

Does it here

share|improve this answer

The VERY easy way... ask the routing table how it gets to an IP outside of it's network

Google DNS ==

ip route get

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.