18

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.

J. Polfer
  • 449
  • 2
  • 5
  • 9
  • 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 Jun 24 '09 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 Jun 24 '09 at 18:43
  • Seems more complex than what I need, but a cool, guaranteed-to-work idea though. – J. Polfer Jun 24 '09 at 18:44

12 Answers12

30

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

or

netstat -rn
Low power
  • 99
  • 3
l0c0b0x
  • 11,697
  • 6
  • 46
  • 76
  • Wouldn't UG require the interface to be up? I think just G would be better. – J. Polfer Jun 24 '09 at 18:39
  • Either way, this is spot on. Many Thanx. – J. Polfer Jun 24 '09 at 18:40
  • 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 Jun 24 '09 at 21:09
  • 2
    You can save yourself the grep, awk can also filter: `route -n | awk '{if($4=="UG")print $2}'` – Kenny Rasschaert Dec 20 '11 at 07: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 May 24 '13 at 13:32
  • `route` is becoming obsoleted. – John Greene Jan 15 '22 at 11:58
16
ip route show 0.0.0.0/0 dev eth0 | cut -d\  -f3

is my entry :)

MikeyB
  • 38,725
  • 10
  • 102
  • 186
5

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

Kamil Kisiel
  • 11,946
  • 7
  • 46
  • 68
3
$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.199.0   0.0.0.0         255.255.255.240 U         0 0          0 virbr1
192.168.200.0   0.0.0.0         255.255.255.240 U         0 0          0 virbr2
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 wlan0
192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr0
0.0.0.0         192.168.1.254   0.0.0.0         UG        0 0          0 wlan0

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

wzzrd
  • 10,269
  • 2
  • 32
  • 47
3

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}'
Martin M.
  • 6,428
  • 2
  • 24
  • 42
2

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

So this would work:

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

In your case:

ip -j route show 0.0.0.0/0 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.

href_
  • 151
  • 3
2

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

TCampbell
  • 2,014
  • 14
  • 14
2

anyone shorter than this? =)

ip r | awk '/^def/{print $3}'
ThorstenS
  • 3,084
  • 18
  • 21
0

netstat -rn |awk '{if($1=="0.0.0.0") 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 '/^0.0.0.0/{ s/^[0. ]*//; s/ .*$//; p; }` – Mei Dec 20 '11 at 15:53
0

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

#!/bin/bash
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
fi
xinthose
  • 135
  • 1
  • 10
0

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

route | grep default | awk '{print $2}'
sys0dm1n
  • 111
  • 3
-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 192.168.1.1. 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.

Sven
  • 97,248
  • 13
  • 177
  • 225