How to find the gateway IP address in Linux

Solution 1:

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

Solution 2:

ip route show 0.0.0.0/0 dev eth0 | cut -d\  -f3

is my entry :)


Solution 3:

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


Solution 4:

$ 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.


Solution 5:

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