How to Find Default Gateway in Linux

A gateway is a node or a router that acts as an access point to passes network data from local networks to remote networks. There are many ways to find out your gateway in Linux. Here are some of them from Terminal.

You can find default gateway using ip, route and netstat commands in Linux systems.

Using route command

Open up your terminal and type the following commands:

sk@sk:~$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth1
192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth1

The above output shows my default gateway is 192.168.1.1. UG stands for the network link is Up and G stands for Gateway.

Using ip route

Use the following command:

sk@sk:~$ ip route show
default via 192.168.1.1 dev eth1  proto static 
192.168.1.0/24 dev eth1  proto kernel  scope link  src 192.168.1.100  metric 1

Using netstat

Use the following command:

sk@sk:~$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 eth1
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth1

And finally you can view it using the eth config files. If your network interface is eth0, then the command should be:

In RHEL based systems:

[root@server ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
BOOTPROTO=none
NM_CONTROLLED="yes"
ONBOOT=yes
TYPE="Ethernet"
UUID="bcb0a409-d7d4-4f2f-882f-ec46e07e670d"
HWADDR=08:00:27:A6:0C:AC
IPADDR=192.168.1.200
PREFIX=24
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=192.168.1.200
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"

And for Debian based systems, use the following command:

sk@sk:~$ cat /etc/network/interfaces 
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1

That’s it.

Sumber: https://www.unixmen.com/how-to-find-default-gateway-in-linux/

Leave a Reply