A gateway is a network node used in telecommunications that connects two networks with different transmission protocols together. Gateways serve as an entry and exit point for a network as all data must pass through or communicate with the gateway prior to being routed.
All the possible ways to find default gateway in Linux
The most commonly used tools are: ip, and netcat. We will see how check the default gateway using each tool with examples.
To find the default gateway or Router IP address, simply run:
# ip r OR ip route OR ip route show
[root@Centos ~]# ip r
default via 10.0.0.1 dev eth0 proto dhcp metric 100
10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.5 metric 100
168.63.129.16 via 10.0.0.1 dev eth0 proto dhcp metric 100
169.254.169.254 via 10.0.0.1 dev eth0 proto dhcp metric 100
[root@Centos ~]# ip route show
default via 10.0.0.1 dev eth0 proto dhcp metric 100
10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.5 metric 100
168.63.129.16 via 10.0.0.1 dev eth0 proto dhcp metric 100
169.254.169.254 via 10.0.0.1 dev eth0 proto dhcp metric 100
[root@Centos ~]# ip route
default via 10.0.0.1 dev eth0 proto dhcp metric 100
10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.5 metric 100
168.63.129.16 via 10.0.0.1 dev eth0 proto dhcp metric 100
169.254.169.254 via 10.0.0.1 dev eth0 proto dhcp metric 100
[root@Centos ~]#
Did you see the line “default via 10.0.0.1 “ in the above output? This is the default gateway. So my default gateway is 10.0.0.1
If you want to display ONLY the default gateway and exclude all other details from the output, you can use awk command with ip route like below.
[root@Centos ~]# ip route | awk '/^default/{print $3}'
10.0.0.1
The route command is used to show and manipulate routing table in older Linux distributions, for example RHEL 6, CentOS 6.
If you’re using those older Linux distributions, you can use the route command to display the default gateway.
Please note that the route tool is deprecated and replaced with ip route command in the latest Linux distributions. If you still want to use route for any reason, you need to install it.
route
command with -n
flag to display the gateway IP address or router IP address in your Linux system:
[root@Centos ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.1 0.0.0.0 UG 100 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
168.63.129.16 10.0.0.1 255.255.255.255 UGH 100 0 0 eth0
169.254.169.254 10.0.0.1 255.255.255.255 UGH 100 0 0 eth0
[root@Centos ~]#
As you see in the above output, the gateway IP address is 10.0.0.1 You will also see the two letters “UG” under Flags section. The letter “U” indicates the interface is UP and G stands for Gateway.
Netstat prints information about the Linux networking subsystem. Using netstat tool, we can print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships in Linux and Unix systems.
Netstat is part of net-tools package, so make sure you’ve installed it in your Linux system.
To print the default gateway IP address using netstat
command, run:
[root@Centos ~]# netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 10.0.0.1 0.0.0.0 UG 0 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
168.63.129.16 10.0.0.1 255.255.255.255 UGH 0 0 0 eth0
169.254.169.254 10.0.0.1 255.255.255.255 UGH 0 0 0 eth0
[root@Centos ~]#