A default gateway is a network device that forwards traffic from a Linux system to destinations outside the local network. In most home and small office networks, the default gateway is a router that connects the local network to the internet or another network. This tutorial provides 2 methods how to get the default gateway IP address on Linux.
Method 1 - ip command
The ip utility can display the routing information configured on a Linux system. The following command finds the default route and prints the network interface along with its gateway IP address:
ip route | awk '/^default/ {print $5, $3}'
Output example:
ens33 192.168.0.1
Here, ens33 represents the network interface, while 192.168.0.1 is the IP address of the default gateway.
Method 2 - /proc/net/route file
Linux also exposes routing information through the /proc/net/route file. The following command identifies the default route and converts the gateway value from hexadecimal notation into a standard IPv4 address:
awk '$2 == "00000000" {
printf "%s %d.%d.%d.%d\n", $1,
strtonum("0x" substr($3, 7, 2)),
strtonum("0x" substr($3, 5, 2)),
strtonum("0x" substr($3, 3, 2)),
strtonum("0x" substr($3, 1, 2))
}' /proc/net/route
The output will show the same result as the previous command, with the network interface followed by the default gateway IP address.
Leave a Comment
Cancel reply