J. Polfer Asked: 2009-06-25 10:22:29 +0800 CST2009-06-25 10:22:29 +0800 CST 2009-06-25 10:22:29 +0800 CST How to find the gateway IP address in Linux 772 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. linux scripting shell tcpip gateway 12 Answers Voted Best Answer l0c0b0x 2009-06-25T10:31:09+08:002009-06-25T10:31:09+08:00 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 MikeyB 2009-06-25T11:26:54+08:002009-06-25T11:26:54+08:00 ip route show 0.0.0.0/0 dev eth0 | cut -d\ -f3 is my entry :) Kamil Kisiel 2009-06-25T10:25:46+08:002009-06-25T10:25:46+08:00 You can get the system's default gateway from the output of netstat -r or route wzzrd 2009-06-25T10:30:40+08:002009-06-25T10:30:40+08:00 $ 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. Martin M. 2009-06-25T10:40:08+08:002009-06-25T10:40:08+08:00 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}' TCampbell 2009-06-25T10:26:57+08:002009-06-25T10:26:57+08:00 The output from route -n or netstat -rn, and search for the destination 0.0.0.0. ThorstenS 2011-12-20T22:32:52+08:002011-12-20T22:32:52+08:00 anyone shorter than this? =) ip r | awk '/^def/{print $3}' href_ 2021-05-26T23:43:24+08:002021-05-26T23:43:24+08:00 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. Andrew Rafael M. Banas 2009-10-14T23:48:57+08:002009-10-14T23:48:57+08:00 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?) xinthose 2017-09-08T06:18:11+08:002017-09-08T06:18:11+08:00 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
To print out only the default gw IP:
To print out route information on all interfaces:
or
is my entry :)
You can get the system's default gateway from the output of
netstat -r
orroute
The 0.0.0.0 is your default gateway, pointing to 192.168.1.254 at my place.
I prefer the iproute package:
The output from route -n or netstat -rn, and search for the destination 0.0.0.0.
anyone shorter than this? =)
Since iproute2 4.14.1, you can also output JSON for a lot of commands.
So this would work:
In your case:
Note that you need jq for this, but depending on your environment that might already be available - I know it was in my case.
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?)
If you would like to get the gateway from the interfaces file: