20

I'm trying to find the default Gateway of a machine, but I see multiple interfaces.

Also, when I find my IP address from the below website, it gives me a different IP address. I thought the default Gateway and external IP would be the same? Correct me if I'm wrong.

https://www.privateinternetaccess.com/pages/whats-my-ip/
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.2.2        0.0.0.0         UG    0      0        0 eth0
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
10.2.0.0        192.168.255.9   255.255.255.0   UG    0      0        0 tun0
192.168.33.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.255.1   192.168.255.9   255.255.255.255 UGH   0      0        0 tun0
192.168.255.5   0.0.0.0         255.255.255.255 UH    0      0        0 tun4
192.168.255.5   0.0.0.0         255.255.255.255 UH    0      0        0 tun6
192.168.255.5   0.0.0.0         255.255.255.255 UH    0      0        0 tun5
192.168.255.5   0.0.0.0         255.255.255.255 UH    0      0        0 tun2
192.168.255.9   0.0.0.0         255.255.255.255 UH    0      0        0 tun0
192.168.255.13  0.0.0.0         255.255.255.255 UH    0      0        0 tun3
192.168.255.13  0.0.0.0         255.255.255.255 UH    0      0        0 tun1
$ netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         10.0.2.2        0.0.0.0         UG        0 0          0 eth0
10.0.2.0        *               255.255.255.0   U         0 0          0 eth0
10.2.0.0        192.168.255.5   255.255.255.0   UG        0 0          0 tun6
192.168.33.0    *               255.255.255.0   U         0 0          0 eth1
192.168.255.1   192.168.255.5   255.255.255.255 UGH       0 0          0 tun6
192.168.255.5   *               255.255.255.255 UH        0 0          0 tun6
192.168.255.5   *               255.255.255.255 UH        0 0          0 tun3
192.168.255.5   *               255.255.255.255 UH        0 0          0 tun2
192.168.255.5   *               255.255.255.255 UH        0 0          0 tun4
192.168.255.9   *               255.255.255.255 UH        0 0          0 tun5
192.168.255.13  *               255.255.255.255 UH        0 0          0 tun0
192.168.255.13  *               255.255.255.255 UH        0 0          0 tun1

3 Answers 3

27

destination 0.0.0.0 and genmask 0.0.0.0 matches any other packet which is not defined in routing tables (last way out), so your default gateway is: 10.0.2.2.

You can also use:

$ ip r | grep ^def
default via 192.168.1.1 dev enp3s0  proto static  metric 100

I thought the default Gateway and external ip will be same.

Not true, for example you can forward your traffic to your router with an internal IP address but your the router will replace the source address with its own public IP (NAT).

3
  • 5
    Or to get just the IP: ip route | awk '/default/ {print $3; exit}' Sep 27, 2020 at 8:02
  • To find out the default ipv6 gateway (or gateway6), use this command ip -6 r | grep ^def
    – user585571
    Aug 19, 2021 at 11:59
  • ip r did it for me. Thanks!
    – ttt
    Oct 23, 2021 at 22:06
4

1st Method:

ip route show or ip route or ip r

enter image description here

You will get some rows , the row where it says 'default' is the row that gives you information on default router ip.

To narrow down the result and finding/displaying only 'default' row you can filter it like this.

ip r | grep ^def

enter image description here

2nd Method:

route -n

enter image description here

Under Gateway column you can see your desired gateway ip.

3rd Method:

netstat -rn

enter image description here

Same as above, you can find your gateway ip under Gateway column.

Result: In my case, Gateway ip address is 192.168.0.1

1

The easiest way is to pull down the network menu and select Connection Information and look at the gateway address there.

Otherwise, in terminal you can type netstat -rn and the first line which says default is where you'll see the gateway address.

1
  • netstat -r does not give default gateway address .. netstat -rn does
    – Shamsail
    Jun 11, 2021 at 6:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .