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.
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.
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
route -n | awk '{if($4=="UG")print $2}'
Dec 20, 2011 at 7:16
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
ip route show 0.0.0.0/0 dev eth0 | cut -d\ -f3
is my entry :)
You can get the system's default gateway from the output of netstat -r
or route
$ 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.
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.
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}'
anyone shorter than this? =)
ip r | awk '/^def/{print $3}'
ip r | awk 'END{print $3}'
is three characters shorter.
Dec 20, 2011 at 7:13
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
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?)
netstat -rn|sed -n '/^0.0.0.0/{ s/^[0. ]*//; s/ .*$//; p; }
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 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.