How can I find the default gateway of a machine?
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
Solution 1:
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).
Solution 2:
1st Method:
ip route show
or
ip route
or
ip r
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
2nd Method:
route -n
Under Gateway
column you can see your desired gateway ip.
3rd Method:
netstat -rn
Same as above, you can find your gateway ip under Gateway
column.
Result: In my case, Gateway ip address is 192.168.0.1