route
Command: Managing the IP Routing Table
Summary
The route
command is a powerful utility used to display and manipulate the IP routing table on Linux systems. It allows you to view existing routes, add new routes, and delete existing ones, enabling control over how network traffic is directed.
Introduction
The route
command is fundamental for network administrators and anyone who needs to understand or modify how their system forwards network packets. The routing table contains information about which network interface should be used to reach a specific destination network or host. Without a properly configured routing table, network communication will fail. This command allows you to view, add, delete, and modify these routes. It's a critical tool for diagnosing network issues and setting up complex network configurations.
Use case and Examples
Displaying the Routing Table
This command displays the current routing table. The-n
option prevents reverse DNS lookups, making the output faster and easier to read. The output shows destination networks, gateways, netmasks, and the interface used to reach each destination. Adding a Route to a Specific Network
This command adds a route to the network 192.168.5.0/24, specifying that traffic destined for this network should be routed through the gateway 192.168.1.1.sudo
is required because modifying the routing table requires administrative privileges. Adding a Default Gateway
This command sets the default gateway to 192.168.1.1. The default gateway is used for traffic destined for networks not explicitly defined in the routing table.Deleting a Route
This command deletes the route to the network 192.168.5.0/24.Adding a route to a specific host
This command adds a route for a single host10.10.10.10
and specifies that traffic destined for it should be routed through 192.168.1.1
Commonly used flags
Flag | Description | Example |
---|---|---|
-n | Shows numerical addresses instead of trying to determine symbolic host names. This is faster and avoids DNS lookup issues. | route -n |
add | Adds a new route to the routing table. Requires specifying the destination network or host and the gateway (if applicable). | sudo route add -net 10.0.0.0 netmask 255.255.255.0 gw 192.168.1.1 |
del | Deletes an existing route from the routing table. Requires specifying the destination network or host to be removed. | sudo route del -net 10.0.0.0 netmask 255.255.255.0 |
-net | Specifies that the destination is a network. Requires a network address and netmask. | route add -net 192.168.1.0 netmask 255.255.255.0 gw 10.0.0.1 |
-host | Specifies that the destination is a single host. | route add -host 192.168.1.10 gw 10.0.0.1 |
gw | Specifies the gateway to use for the route. | route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.1 |
default | Specifies the default gateway. | route add default gw 192.168.1.1 |