Linux route command
The route
command in Linux is used to display and manipulate the IP routing table, which defines the paths that packets take when they are sent across the network. It is used to add, delete, or modify routing entries in the system's routing table, as well as to display the current routing information.
While route
has been deprecated on many modern Linux distributions in favor of the ip route
command, it is still available on many systems and can be useful for legacy scripts or troubleshooting.
Basic Syntax of route
:
Commonly Used route
Commands and Options
Display the Routing Table:
- To view the current IP routing table, you can use the
route
command without any options:
Sample Output:
- Explanation:
- Destination: The destination network or IP address.
- Gateway: The gateway through which packets are routed.
- Genmask: The subnet mask.
- Flags: Routing flags such as
U
for Up (route is in use) andG
for Gateway. - Metric: The "cost" of using this route (lower is better).
- Iface: The network interface through which packets are sent (e.g.,
eth0
,eth1
).
- To view the current IP routing table, you can use the
Add a Route:
- You can add a route to a specific destination network or IP address via a gateway using the
route add
command.
- Example: Add a route to the network
192.168.3.0/24
via the gateway192.168.1.1
.
Sample Output:
- This command will add a route that sends packets destined for the
192.168.3.0/24
network through the gateway192.168.1.1
.
- You can add a route to a specific destination network or IP address via a gateway using the
Delete a Route:
- To delete a specific route, use the
route del
command.
- Example: Delete the route to the
192.168.3.0/24
network via the gateway192.168.1.1
.
Sample Output:
- To delete a specific route, use the
Add a Default Route:
- A default route is used when there is no specific route for a destination. To add a default route, use the following command:
- Example: Add a default route via the gateway
192.168.1.1
.
Sample Output:
- This command will add a default route that directs all traffic with no specific route to the gateway
192.168.1.1
.
Change the Gateway for a Route:
- To change the gateway for an existing route, you first delete the old route and then add the new one.
- Example: Change the gateway for the
192.168.3.0/24
network from192.168.1.1
to192.168.1.2
.
Sample Output:
Show the Routing Table in Numeric Format:
- You can display the routing table with numerical addresses (no DNS lookup) by using the
-n
option:
Sample Output:
- Explanation: The numerical addresses will be displayed without attempting to resolve them to hostnames.
- You can display the routing table with numerical addresses (no DNS lookup) by using the
Example Scenarios:
Display the current routing table:
Add a new route to the
192.168.4.0/24
network via the gateway192.168.1.1
:Delete a specific route to the
192.168.4.0/24
network:Add a default route via the gateway
192.168.1.1
:Show the routing table in numeric format (without DNS resolution):
Summary of Common route
Options:
Option | Description |
---|---|
route | Display the current routing table |
route add | Add a new route to the routing table |
route del | Delete a route from the routing table |
route add default | Add a default route (for all unknown destinations) |
-n | Display the routing table with numeric addresses (no DNS lookup) |
-net | Add or delete a route to a network (use with netmask ) |
gw | Specify the gateway for the route |
-host | Add or delete a route to a specific host (not a network) |
Conclusion:
The route
command is a powerful tool for managing the routing table in Linux. It allows you to add, remove, and modify routes to control how traffic is routed across the network. Although it has been largely replaced by ip route
on modern systems, route
is still commonly used for troubleshooting and basic route management. Understanding how to use it is essential for network administrators and those working with network configurations.