askvity

How to Get Routing Tables?

Published in Networking 3 mins read

You can obtain routing tables primarily using command-line tools available on your operating system.

Here's how to access and interpret routing table information:

1. Using netstat (Commonly Used)

The netstat command (network statistics) provides a wealth of network-related information, including the routing table.

  • Command: netstat -rn

  • Explanation:

    • netstat: Invokes the network statistics utility.
    • -r: Specifies that you want to display the routing table.
    • -n: Displays network addresses as numbers (IP addresses) rather than attempting to determine symbolic host names. This makes the output faster and more reliable.
  • Example Output (Linux/macOS):

    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         192.168.1.1     0.0.0.0         UG    100    0        0 en0
    10.0.0.0        192.168.1.1     255.255.255.0   UG    100    0        0 en0
    127.0.0.0       127.0.0.1       255.0.0.0       U     0      0        0 lo0
    192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 en0
    • Destination: The network or host the route is for.
    • Gateway: The IP address of the next hop router (if any). A gateway of 0.0.0.0 indicates a directly connected network.
    • Genmask: The network mask for the destination network.
    • Flags: Flags that describe the route (e.g., U for Up, G for Gateway, H for Host).
    • Metric: The cost of the route (lower is often preferred).
    • Ref: Number of references to this route (deprecated).
    • Use: Number of packets that have used this route.
    • Iface: The network interface used for this route.

2. Using route (Alternative Command)

The route command is another utility for displaying and manipulating the routing table.

  • Command: route -n or route -s (depending on the system and desired output)

  • Explanation:

    • route: Invokes the routing table utility.
    • -n: Displays network addresses numerically.
    • -s: Displays routing statistics (may be different or unavailable on some systems). -n is the more common and widely supported option for simply displaying the table.
  • Example Output (Linux):

    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 wlan0
    192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0

3. Windows Command Prompt (route print)

On Windows systems, use the following command:

  • Command: route print

  • Example Output (Windows):

    ===========================================================================
    Interface List
     16...00 ff ff ff ff ff ......VirtualBox Host-Only Ethernet Adapter
      1...........................Software Loopback Interface 1
    ===========================================================================
    
    IPv4 Route Table
    ===========================================================================
    Active Routes:
    Network Destination        Netmask          Gateway       Interface  Metric
              0.0.0.0          0.0.0.0      192.168.1.1    192.168.1.5     25
            127.0.0.0        255.0.0.0         On-link         127.0.0.1    331
            127.0.0.1  255.255.255.255         On-link         127.0.0.1    331
          127.255.255.255  255.255.255.255         On-link         127.0.0.1    331
          192.168.1.0    255.255.255.0      192.168.1.5    192.168.1.5     271
          192.168.1.5  255.255.255.255         On-link         127.0.0.1    276
        192.168.1.255  255.255.255.255         On-link         127.0.0.1    276
            224.0.0.0        240.0.0.0         On-link         127.0.0.1    331
            224.0.0.0        240.0.0.0      192.168.1.5    192.168.1.5     271
      255.255.255.255  255.255.255.255         On-link         127.0.0.1    331
      255.255.255.255  255.255.255.255      192.168.1.5    192.168.1.5     271
    ===========================================================================
    Persistent Routes:
      None
  • The Windows output is more verbose and provides a clear breakdown of each route.

4. Explanation of Routing Tables

The routing table is a critical component of network communication. It contains the rules that your device (computer, router, etc.) uses to decide where to send network traffic. Each entry in the table specifies a destination network, the gateway (next hop) to use to reach that network, and other related information. When your device needs to send data to a particular IP address, it consults the routing table to find the best path to reach the destination. If a specific route to the destination exists, it is used. If not, the default gateway (route with Destination 0.0.0.0 and Netmask 0.0.0.0) is used.

In summary, to view routing tables, use netstat -rn (Linux/macOS), route -n (Linux), or route print (Windows). These commands provide insights into your system's network configuration and routing decisions.

Related Articles