How to use the command route (with examples)

How to use the command route (with examples)

  • Osx
  • December 25, 2023

The route command is used to manually manipulate the routing tables in a Linux system. It requires root privileges and allows users to add, delete, or display the routing information. The following article will illustrate different use cases of the route command with examples.

Use case 1: Add a route to a destination through a gateway

Code:

sudo route add "destination_ip_address" "gateway_address"

Motivation:

Adding a route to a destination through a gateway is useful when you want to specify a particular path for outgoing network traffic. This can help optimize network traffic by ensuring that it takes the most efficient route.

Explanation:

  • sudo: This command is used to execute the route command with administrative privileges.
  • route: The command to manipulate the routing tables.
  • add: Specifies that we want to add a new route.
  • "destination_ip_address": The IP address of the destination you want to add a route for.
  • "gateway_address": The IP address of the gateway through which the traffic will be routed.

Example output:

Destination  Gateway      Genmask       Flags Metric Ref  Use Iface
192.168.1.0  192.168.0.1  255.255.255.0 UG    0      0    0   eth0

In this example, we added a route to the destination IP address “192.168.1.0” through the gateway address “192.168.0.1”. The output shows the new route added to the routing table.

Use case 2: Add a route to a /24 subnet through a gateway

Code:

sudo route add "subnet_ip_address/24" "gateway_address"

Motivation:

This use case is helpful when you want to add a route to an entire subnet instead of a specific IP address. It allows for routing traffic to multiple devices within a network range.

Explanation:

  • sudo: Run the command with administrative privileges.
  • route: The command to manipulate the routing tables.
  • add: Specifies that we want to add a new route.
  • "subnet_ip_address/24": The IP address of the subnet you want to add a route for, followed by “/24” to indicate a subnet of size 24.
  • "gateway_address": The IP address of the gateway through which the traffic will be routed.

Example output:

Destination      Gateway      Genmask        Flags Metric Ref  Use Iface
192.168.0.0      192.168.1.1  255.255.255.0  UG    0      0    0   eth1

In this example, we added a route to the subnet IP address “192.168.0.0/24” through the gateway address “192.168.1.1”. The output shows the new route added to the routing table.

Use case 3: Run in test mode (does not do anything, just print)

Code:

sudo route -t add "destination_ip_address/24" "gateway_address"

Motivation:

Running the route command in test mode allows you to see the proposed changes to the routing table without actually modifying it. This is useful when you want to verify the correctness of the command before making any changes.

Explanation:

  • sudo: Run the command with administrative privileges.
  • route: The command to manipulate the routing tables.
  • -t: Specifies the test mode, which prints the proposed changes but does not modify the routing table.
  • add: Specifies that we want to add a new route.
  • "destination_ip_address/24": The IP address of the destination or subnet you want to add a route for, followed by “/24” to indicate a subnet of size 24.
  • "gateway_address": The IP address of the gateway through which the traffic will be routed.

Example output:

Propose to add the following routes:
Destination      Gateway      Genmask        Flags Metric Ref  Use Iface
192.168.0.0      192.168.1.1  255.255.255.0  UG    0      0    0   eth1

In this example, running the command in test mode prints the proposed changes without modifying the routing table.

Use case 4: Remove all routes

Code:

sudo route flush

Motivation:

Removing all routes can be necessary when you want to clear the existing routing table completely. This can be useful in situations where you want to start from a clean state or when troubleshooting network connectivity issues.

Example output:

No output will be displayed if the command is successful.

Explanation:

  • sudo: Run the command with administrative privileges.
  • route: The command to manipulate the routing tables.
  • flush: Removes all routes from the routing table.

Use case 5: Delete a specific route

Code:

sudo route delete "destination_ip_address/24"

Motivation:

Deleting a specific route can be useful when you no longer need a particular route and want to remove it from the routing table. This can help improve network performance by removing unnecessary routes.

Explanation:

  • sudo: Run the command with administrative privileges.
  • route: The command to manipulate the routing tables.
  • delete: Specifies that we want to delete a route.
  • "destination_ip_address/24": The IP address of the destination or subnet you want to delete the route for, followed by “/24” to indicate a subnet of size 24.

Example output:

No output will be displayed if the command is successful.

Use case 6: Lookup and display the route for a destination (hostname or IP address)

Code:

sudo route get "destination"

Motivation:

Looking up and displaying the route for a destination can help in troubleshooting network connectivity issues. It allows you to verify the routing information and see the path that network traffic would take to reach the destination.

Explanation:

  • sudo: Run the command with administrative privileges.
  • route: The command to manipulate the routing tables.
  • get: Specifies that we want to lookup and display the route for a destination.
  • "destination": The hostname or IP address of the destination you want to lookup.

Example output:

   route to: destination
destination: x.x.x.x
   mask: 255.255.255.255
gateway: gateway_address
  interface: eth0
      flags: <UP,GATEWAY,DONE>

In this example, the output displays the route information for the given destination. It shows the destination IP address, subnet mask, gateway address, interface, and flags associated with the route.

Conclusion:

The route command provides a flexible way to manually manipulate the routing tables in a Linux system. It allows users to add, delete, or display routes to control the flow of network traffic. By understanding and utilizing the different use cases of the route command, you can effectively manage and optimize network connectivity.

Tags :

Related Posts

How to use the command "git update-index" (with examples)

How to use the command "git update-index" (with examples)

Git update-index is a command used to manipulate the index in Git.

Read More
How to use the command 'gnucash' (with examples)

How to use the command 'gnucash' (with examples)

GnuCash is personal and small-business financial accounting software that helps users keep track of their financial transactions, create budgets, and generate various types of financial reports.

Read More
How to use the command "texcount" (with examples)

How to use the command "texcount" (with examples)

“texcount” is a command that counts the number of words in a TeX document, while omitting macros.

Read More