How to Find Default Gateway in Ubuntu Using Terminal Commands and Network Tools


10 views

For most Ubuntu networking troubleshooting, these three terminal commands will quickly reveal your default gateway:

# Method 1: Using ip route (recommended)
ip route show default

# Method 2: Using netstat (legacy systems)
netstat -rn

# Method 3: Using route command
route -n

The typical output from ip route show default looks like:

default via 192.168.1.1 dev wlp3s0 proto dhcp metric 600

Where 192.168.1.1 is the default gateway IP address, wlp3s0 is the network interface, and dhcp indicates automatic configuration.

For system administrators who prefer GUI tools:

# Install graphical network tools
sudo apt install network-manager-gnome

# Launch network connections GUI
nm-connection-editor

Here's a Bash script snippet to extract just the gateway IP:

#!/bin/bash
GW=$(ip route show default | awk '/default/ {print $3}')
echo "Default Gateway: $GW"

# For multiple interfaces:
ip -4 route list 0/0 | awk '{print $3}'

Gateway information can also be found in these configuration files:

# For systems using netplan
/etc/netplan/*.yaml

# For traditional ifupdown
/etc/network/interfaces

When gateway discovery fails, try these diagnostic commands:

# Check if gateway is reachable
ping -c 4 $(ip r | grep default | cut -d ' ' -f 3)

# Verify DNS resolution
nslookup google.com

# Full network interface info
ip addr show


The default gateway is a crucial networking concept that serves as the access point or IP router that enables communication between devices on different networks. When working with Ubuntu systems, particularly in server administration or network troubleshooting scenarios, knowing how to find this information is essential.

The most modern and recommended approach is using the ip route command from the iproute2 suite:

ip route show default | awk '/default/ {print $3}'

Or alternatively:

ip route | grep default

For systems with older networking tools installed:

netstat -rn | grep '^0.0.0.0' | awk '{print $2}'

Another legacy method that still works on many systems:

route -n | grep 'UG[ \t]' | awk '{print $2}'

For systems using NetworkManager:

nmcli device show | grep IP4.GATEWAY

Here's a more robust Bash function you can use in scripts:

get_default_gateway() {
    local gateway
    gateway=$(ip route show default 2>/dev/null | awk '/default/ {print $3}')
    if [[ -z "$gateway" ]]; then
        gateway=$(route -n 2>/dev/null | awk '$1 == "0.0.0.0" {print $2}')
    fi
    echo "$gateway"
}

DEFAULT_GATEWAY=$(get_default_gateway)
echo "Default Gateway: ${DEFAULT_GATEWAY}"

If you're not seeing a default gateway, check:

  • Network interface status with ip link show
  • DHCP client logs if using automatic configuration
  • Network configuration files in /etc/netplan/ (Ubuntu 18.04+)

For a complete picture of your network routing:

ip route list table all

This shows all routing tables and rules, including the main table where the default gateway resides.