route
route
The route command in Linux is used to view and manipulate the IP routing table. This command displays the current routing table and allows you to add or remove routes. However, like ifconfig, the route command is considered deprecated in favor of the ip command from the iproute2 package.
View the Routing Table:
routeor
route -nThe
-noption prevents the command from attempting to resolve IP addresses to hostnames, making the output quicker to generate and easier to read.Add a Default Gateway:
route add default gw 192.168.1.1This command sets the default gateway to
192.168.1.1.Add a Route to a Specific Network:
route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.1This command adds a route to the network
192.168.2.0with a netmask of255.255.255.0, routing through the gateway192.168.1.1.Add a Host Route:
route add -host 192.168.1.2 gw 192.168.1.1This command adds a route for a specific host
192.168.1.2through the gateway192.168.1.1.Delete a Route:
route del -net 192.168.2.0 netmask 255.255.255.0This command removes the route to the network
192.168.2.0.
Example of route Command Output
route Command OutputWhen you run route -n, you might see output similar to this:
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 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0Using ip Command for Routing
ip Command for RoutingThe ip command provides more functionality and is the modern replacement for route. Here are equivalent commands using ip:
View the Routing Table:
ip route showAdd a Default Gateway:
ip route add default via 192.168.1.1Add a Route to a Specific Network:
ip route add 192.168.2.0/24 via 192.168.1.1Add a Host Route:
ip route add 192.168.1.2 via 192.168.1.1Delete a Route:
ip route del 192.168.2.0/24
Conclusion
While the route command is still available and useful for simple routing table manipulations, it is recommended to use the ip command for more advanced and modern network configuration tasks. The ip command offers a unified and consistent interface for managing both IP addresses and routes, making it a more powerful and flexible tool for network administrators.
help
Usage: route [-nNvee] [-FC] [<AF>] List kernel routing tables
route [-v] [-FC] {add|del|flush} ... Modify routing table for AF.
route {-h|--help} [<AF>] Detailed usage syntax for specified AF.
route {-V|--version} Display version/author and exit.
-v, --verbose be verbose
-n, --numeric don't resolve names
-e, --extend display other/more information
-F, --fib display Forwarding Information Base (default)
-C, --cache display routing cache instead of FIB
breakdown
Last updated