Route Command
Understanding the Route Command
The route command is a powerful utility in Linux and
other Unix-like operating systems used to view and manipulate the IP
routing table. The routing table is crucial for directing network
traffic; it contains information about how to reach different
network destinations. Understanding and effectively using the
route command is essential for network administration,
troubleshooting, and configuring network connectivity.
Key Operations with the Route Command
This section details common operations performed using the
route command, including displaying the routing table,
adding new routes, deleting existing ones, and modifying route
parameters.
Displaying the IP Routing Table
To view the current IP routing table, simply execute the
route command without any arguments. This will show you
the active routes, including destination networks, netmasks,
gateways, and interfaces.
route
Adding a New Route
You can add a new route to a specific network using the
add option. This requires specifying the network
address, its netmask, the gateway IP address, and the network
interface to use.
route add -net {network} netmask {netmask} gw {gateway} {interface}
Adding a Default Gateway
A default gateway is used when the system doesn't have a specific route for a destination. It directs all unknown traffic to a specified gateway.
route add default gw {gateway} {interface}
Deleting a Route
To remove a specific route from the routing table, use the
del option, providing the same parameters used when
adding the route.
route del -net {network} netmask {netmask} gw {gateway} {interface}
Deleting the Default Gateway
You can remove the default gateway by specifying
default with the del option.
route del default
Adding a Route to a Host
For directing traffic to a specific IP address (host) rather than a
network, use the -host option.
route add -host {destination-host} gw {gateway} {interface}
Changing Route Metrics
The metric value influences which route is chosen when multiple
routes exist to the same destination. You can change a route's
metric using the change option.
route change -net {network} netmask {netmask} gw {gateway} {interface} metric {metric}
Continuous Monitoring of the Routing Table
To continuously monitor changes in the routing table, you can use
the watch command with route -n. The
-n flag displays IP addresses numerically, which is
often more convenient.
watch -n 1 route -n