How to use the command 'ip route' (with examples)

How to use the command 'ip route' (with examples)

The ip route command is used for managing the IP routing table in Linux. It provides various options to add, delete, and modify routes in the routing table. The routing table contains information about the network paths that packets take when traveling from one IP address to another.

Use case 1: Display the routing table

Code:

ip route show | list

Motivation: This use case is helpful when you want to view the routing table to check the existing routes.

Explanation: The ip route show command displays the routing table. The list argument is an alternative to show and provides the same functionality.

Example output:

default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10

Use case 2: Add a default route using gateway forwarding

Code:

sudo ip route add default via gateway_ip

Motivation: When you want to set a default route to forward all packets with unknown destinations to a specific gateway, this use case becomes useful.

Explanation: The ip route add default command is used to add a default route. The via gateway_ip argument specifies the gateway IP address through which the packets will be forwarded.

Example output: No output is displayed if the command is successful.

Use case 3: Add a default route using eth0

Code:

sudo ip route add default dev eth0

Motivation: This use case allows you to set the default route using a specific network interface (eth0 in this example) instead of a gateway IP address.

Explanation: The ip route add default command is used to add a default route. The dev eth0 argument specifies the network interface through which the packets will be sent.

Example output: No output is displayed if the command is successful.

Use case 4: Add a static route

Code:

sudo ip route add destination_ip via gateway_ip dev eth0

Motivation: When you want to add a specific route to the routing table for a destination IP address, this use case becomes useful. The packets destined for the specified destination IP will be forwarded through the specified gateway IP and network interface.

Explanation: The ip route add command is used to add a specific route. The destination_ip argument specifies the destination IP address. The via gateway_ip argument specifies the gateway IP address through which the packets will be forwarded. The dev eth0 argument specifies the network interface through which the packets will be sent.

Example output: No output is displayed if the command is successful.

Use case 5: Delete a static route

Code:

sudo ip route del destination_ip dev eth0

Motivation: This use case is helpful when you want to remove a specific static route from the routing table.

Explanation: The ip route del command is used to delete a specific route. The destination_ip argument specifies the destination IP address. The dev eth0 argument specifies the network interface through which the packets were being sent.

Example output: No output is displayed if the command is successful.

Use case 6: Change or replace a static route

Code:

sudo ip route change|replace destination_ip via gateway_ip dev eth0

Motivation: This use case allows you to modify or replace an existing static route in the routing table with a new set of parameters.

Explanation: The ip route change or ip route replace command is used to modify or replace an existing route. The destination_ip argument specifies the destination IP address. The via gateway_ip argument specifies the new gateway IP address. The dev eth0 argument specifies the new network interface.

Example output: No output is displayed if the command is successful.

Use case 7: Show which route will be used by the kernel to reach an IP address

Code:

ip route get destination_ip

Motivation: This use case is helpful when you want to determine which route the kernel will use to reach a specific IP address.

Explanation: The ip route get command is used to find the route that the kernel will use to reach the specified destination IP address.

Example output:

destination_ip via gateway_ip dev eth0 src 192.168.1.10

Conclusion:

The ip route command provides a powerful set of options for managing the IP routing table in Linux. By using different arguments with the command, you can display the routing table, add or delete static routes, set default routes, and determine the route that the kernel will use to reach a specific IP address. Understanding and utilizing these use cases can help you effectively manage network routing in your Linux environment.

Related Posts

How to use the command ppmmix (with examples)

How to use the command ppmmix (with examples)

The ppmmix command is used to blend together two PPM (Portable Pixmap) images.

Read More
How to convert an XML document to PYX format (with examples)

How to convert an XML document to PYX format (with examples)

This article will guide you on how to use the xml pyx command to convert an XML document to PYX (ESIS - ISO 8879) format.

Read More
How to use the command sherlock (with examples)

How to use the command sherlock (with examples)

Sherlock is a command-line tool that allows users to search for a specific username across various social networks.

Read More