How do I find out my gateway IP for a computer or a network device that allows or controls access to another computer or network under Linux / UNIX-like operating systems? How can I find out my Linux Gateway / Router IP address using the CLI?
A gateway is a network point that acts as an entrance to another network. On the Internet, a node or stopping point can be either a gateway node or a host (end-point) node. Both the computers of Internet users and the computers that serve pages to users are host nodes. The computers that control traffic within your company’s network or at your local Internet service provider (ISP) are gateway nodes. In the network for an enterprise, a computer server acting as a gateway node is often also acting as a proxy server and a firewall server. A gateway is often associated with both a router, which knows where to direct a given packet of data that arrives at the gateway, and a switch, which furnishes the actual path in and out of the gateway for a given packet.
Command to find out my Linux Gateway / Router IP address using ip command
The latest version of all Linux distribution based upon systemd. These Linux distros user can use the ip command to find out router IP address.
From the above output it is clear that 10.105.28.1 is the default gateway IP address.
How to find default Gateway IP in Linux
Some computers might have multiple default gateways. For example, OpenVPN client software might setup gateway for VPN traffic. Another case is static gateway IP address set up for different networks on your system:
ip r | grep default
In this above example 10.8.0.1 and 192.168.2.254 are the default gateway IP address. The gateway with lowest Metric is the first to be searched and used as the default gateway. In this case, 10.8.0.1 has the 50 as metric cost and 192.168.2.254 has the 100 as metric cost. Linux will always use the one with the lowest cost first. So all traffic will be routed using 10.8.0.1 gateway IP address.
Find out Gateway / router IP address under Linux / UNIX using route command
You need to use the router command command. This command can manipulate the kernel’s IP routing tables. It can also be used to print gateway/router IP address. Type the following command to see default gateway:
$ route -n
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 ra0 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 ra0
192.168.1.1 is gateway IP address for our computer. The flag U indicates that route is up and G indicates that it is gateway. You can print gateway name, enter:
Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.0 * 255.255.255.0 U 0 0 0 ra0 default wrt 0.0.0.0 UG 0 0 0 ra0
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 * 255.255.255.0 U 0 0 0 ra0
default wrt 0.0.0.0 UG 0 0 0 ra0
Second column prints Gateway hostname / IP address. In our example wrt is gateway.
netstat command example to find out gateway/router IP on Unix/FreeBSD/OpenBSD/macOS
Open a terminal and type the following command:
$ netstat -r -n
Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 192.168.1.254 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
route and netstat command demo