When you are troubleshooting a complex networking issue in Linux—such as an inability to reach the public internet while local network file sharing still works—your first diagnostic step should be verifying your default gateway.
The default gateway is the IP address of the router (or firewall) that your Linux machine sends traffic to when it does not know the exact destination. If your machine is configured with the wrong gateway IP, it will essentially be trapped on its local subnet.
Method 1: Using the Modern ‘ip route’ Command
Historically, Linux administrators used the route -n or netstat -rn commands. However, these tools belong to the net-tools package, which has been officially deprecated for over a decade and is no longer installed by default on modern distributions like Ubuntu 22.04 or RHEL 9.
The modern, universally supported method uses the iproute2 suite.
- Open your Linux terminal.
- Execute the following command:
ip route show
- Press Enter.
The terminal will output your routing table. Look specifically at the very first line. It will always begin with the word default.
The output will look similar to this: default via 192.168.1.1 dev eth0 proto dhcp metric 100
The IP address located immediately after the word via (in this example, 192.168.1.1) is the exact IP address of your default gateway.
Method 2: Filtering for Only the IP Address
If you are writing an automated bash script and need the terminal to print only the IP address of the gateway without any of the extra interface data (like dev eth0), you can combine the ip route command with awk to filter the text.
- Execute the following exact command:
ip route show default | awk '{print $3}'
This command asks the system for the default route, pipes the output into the awk text processor, and instructs it to print only the third word in the sentence. The terminal will simply output 192.168.1.1 and nothing else, giving you a perfectly clean variable to use in your scripts.