How to Use the Command 'ip route show' (with examples)
The ip route show
command is a powerful and versatile tool used for managing and displaying a system’s IP routing table. The routing table is a data table stored in a router that lists the routes (and in some cases, metrics) to particular network destinations. The command provides detailed insights into the paths that data packets take across networks, allowing network administrators to manage and troubleshoot network routing effectively.
Use case 1: Display the Routing Table
Code:
ip route show
Motivation: This basic command is essential for network administrators or users who want an overview of the current IP routing table. By executing this command, you can quickly access all the active routing information on a machine, making it easier to identify routes and assess network connectivity issues.
Explanation:
ip
: Invokes the IP utility, which is part of the iproute2 package used to manage network interfaces and addresses.route
: Specifies that we’re interested in operations related to routing (as compared to interfaces or neighbors).show
: Tells the utility to display the current state of the routing table.
Example Output:
default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100
Use case 2: Display the Main Routing Table
Code:
ip route show main|254
Motivation: The main routing table is where typical unicast routes reside. Viewing this table allows network professionals to verify the primary set of routes used by the system for decision-making in sending packets, which is crucial for standard network operations.
Explanation:
main|254
: Specifies the main routing table explicitly, identified by the number254
, which is the default and most actively used table.
Example Output:
default via 192.168.1.1 dev eth0
192.168.10.0/24 dev eth0 proto kernel scope link src 192.168.10.5
Use case 3: Display the Local Routing Table
Code:
ip route show table local|255
Motivation: The local routing table contains routes for the local network (loopback and broadcast routes). Investigating this table is beneficial for diagnosing issues with local network communications and ensuring proper loopback setup.
Explanation:
local|255
: Indicates the local routing table, identified by255
, which contains entries for the local system.
Example Output:
broadcast 192.168.10.0 dev eth0 proto kernel scope link src 192.168.10.5
local 192.168.10.5 dev eth0 proto kernel scope host src 192.168.10.5
Use case 4: Display All Routing Tables
Code:
ip route show table all|unspec|0
Motivation: Viewing all routing tables offers comprehensive insights into the entire routing landscape configured on a network device. This is highly advantageous for complex environments with multiple routing rules and policies, enabling a full-scale analysis and debugging of routing issues.
Explanation:
table all|unspec|0
: Requests the display of all routing tables, including custom and policy-based routing tables.
Example Output:
Table 254 (main):
default via 192.168.1.1 dev eth0
Table 255 (local):
broadcast 192.168.10.0 dev eth0 proto kernel scope link src 192.168.10.5
Use case 5: List Routes from a Given Device Only
Code:
ip route show dev eth0
Motivation: When managing a system with multiple network interfaces, it’s often necessary to inspect routing information specific to a single device. This can help diagnose device-specific network connectivity issues or confirm that routing is correctly configured for a particular interface.
Explanation:
dev eth0
: Limits the output to routes associated with the deviceeth0
, providing focused information pertinent to that interface alone.
Example Output:
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100
default via 192.168.1.1 dev eth0
Use case 6: List Routes Within a Given Scope
Code:
ip route show scope link
Motivation: Routes are classified by their scope, which defines their reach in terms of network size or topology. Viewing routes with the link
scope helps identify routes with direct link-level adjacency, useful for troubleshooting networks with direct connections and no intermediate routing.
Explanation:
scope link
: Filters the output to include only those routes within thelink
scope, meaning they are directly connected routes.
Example Output:
192.168.10.0/24 dev eth0 proto kernel scope link src 192.168.10.2
Use case 7: Display the Routing Cache
Code:
ip route show cache
Motivation: The routing cache (also known as the Routing Information Base - RIB) holds recently used paths for quicker packet forwarding decisions. Although newer kernels don’t typically use this cache, viewing it can still be useful in legacy systems for optimizing performance by analyzing recent routing decisions.
Explanation:
cache
: Directs the command to display cached routing information, highlighting the most recent routes used by the system.
Example Output:
cache ip4: dst 192.168.10.20 via 192.168.10.1 dev eth0
Use case 8: Display Only IPv6 or IPv4 Routes
Code:
ip -6|-4 route show
Motivation: With the transition from IPv4 to IPv6, it’s often necessary to handle routing policies specific to one protocol. This command helps you specifically focus on either IPv4 or IPv6 routes separately, allowing for more efficient route management in dual-stack networks.
Explanation:
-6|-4
: A flag that specifies the IP version for which routing information should be displayed:-6
for IPv6 routes and-4
for IPv4 routes.
Example Output (for IPv4):
default via 192.168.0.1 dev wlan0
192.168.0.0/24 dev wlan0 proto kernel scope link src 192.168.0.104
Conclusion
The ip route show
command, with its various subcommands and options, provides invaluable capabilities for network troubleshooting, management, and analysis. Whether you’re working within a simple home network or configuring enterprise-grade network systems, mastering its use cases aids significantly in understanding and controlling network behavior.