route Command in Linux



The route command is a fundamental utility in Unix/Linux systems used for managing and configuring the IP routing table. This tool is useful for network administrators who need to control and optimize the routing of data packets within a network.

The routing table is a critical component in a network environment, as it holds the information needed to direct network traffic to its appropriate destination. By using the route command, administrators can add, delete, or modify routes, ensuring that data flows efficiently and securely across the network.

Table of Contents

Here is a comprehensive guide to the options available with the route command −

Installing route Command

In most modern Linux distributions, the route command is part of the net-tools package, which may not be installed by default. To install net-tools, use your distribution's package manager −

For Debian/Ubuntu-based systems

sudo apt install net-tools

For Red Hat / CentOS-based systems

sudo yum install net-tools

For Fedora-based systems

sudo dnf install net-tools

Syntax of route Command

The basic syntax for the route command is as follows −

route [options] [add | del | flush] [destination] [gateway] [netmask] [interface]

Where −

  • options − Various flags and parameters that alter the behavior of the route command.
  • add | del | flush − Specifies the action to be performed (add, delete, or flush routes).
  • destination − The target network or host to which packets are routed.
  • gateway − The gateway through which packets should be sent.
  • netmask − The subnet mask for the destination network.
  • interface − The network interface to be used for the route.

route Command Options

Here are several options that can be used with the route command on Linux −

Option Description
-v, --verbose Provides detailed information about the actions being performed by the route command.
-n, --numeric Displays numerical addresses instead of resolving hostnames, speeding up the command output by avoiding DNS lookups.
-e, --extend Shows additional details about each route, similar to the extended output of the netstat -r command.
-F, --fib Operates on the kernel routing table, which is the primary routing table used by the system.
-C, --cache Allows you to view and manipulate the routing cache, which contains temporary routing entries based on recent traffic.

Examples of route Command in Linux

The following examples illustrate how the route command can be effectively used −

  • Adding a New Route
  • Deleting an Existing Route
  • Displaying the Routing Table Verbosely
  • Displaying the Routing Table Numerically
  • Displaying Extended Routing Information
  • Displaying the Routing Cache
  • Displaying the Forwarding

Adding a New Route

To add a new route to the 192.168.18.0 network through the gateway 192.168.18.1, you can use the following command −

sudo route add -net 192.168.18.0 netmask 255.255.255.0 gw 192.168.18.10

This command adds a route to the 192.168.18.0 network with the specified netmask and gateway.

route Command in Linux1

Deleting an Existing Route

In case you want to delete a route to the 192.168.1.0 network, use the del option −

sudo route del -net 192.168.18.0 netmask 255.255.255.0

This command removes the specified route from the routing table.

route Command in Linux2

Displaying the Routing Table Verbosely

To show the current routing table with detailed information, employ the -v option −

route -v

This command provides verbose output, showing more details about each route.

route Command in Linux3

Displaying the Routing Table Numerically

For displaying the routing table with numerical addresses, utilize the -n option −

route -n

This command reveals the routing table with numerical addresses, avoiding DNS lookups.

route Command in Linux4

Displaying Extended Routing Information

To see extended information about the routing table, you can use the -e option −

route -e

This command shows additional details about each route, providing more comprehensive information.

route Command in Linux5

Displaying the Routing Cache

If you need to view the routing cache, employ the -C option −

route -C

This command displays the routing cache, which contains temporary routing entries based on recent traffic.

route Command in Linux6

Displaying the Forwarding Information Base

To display the Forwarding Information Base, which is the default behavior, utilize the -F option −

route -F

This command operates on the kernel routing table, which is the primary routing table used by the system.

route Command in Linux7

Conclusion

The route command is a powerful tool for managing the IP routing table in Unix/Linux systems. By understanding and utilizing its various options, you can effectively control the routing of network packets within your infrastructure.

Whether you need to add, delete, or flush routes, specify gateways, display the routing table, or view the routing cache, the route command provides the necessary functionality.

With this comprehensive guide, you should now be well-prepared to use the route command effectively in your Linux environment.

Advertisements