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

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

The ‘route’ command in Linux is used to manipulate the IP routing table. It allows you to view, add, and delete entries in the routing table. The routing table is used by the operating system to determine the path that network packets should take to reach their destination.

Use case 1: Display the information of route table

Code:

route -n

Motivation: Running this command allows you to view the current routing table on your system. It provides details about the network destinations and the gateways associated with them.

Explanation:

  • -n: Displays the numerical values of IP addresses and network masks instead of resolving them to hostnames and network names.

Example output:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    100    0        0 eth0
192.168.0.0     0.0.0.0         255.255.255.0   U     100    0        0 eth0

Use case 2: Add route rule

Code:

sudo route add -net ip_address netmask netmask_address gw gw_address

Motivation: You would use this command to add a specific route to the routing table. This is useful when you want to define a custom path for packets to specific destinations.

Explanation:

  • add: Adds a new route to the routing table.
  • -net ip_address: Specifies the network IP address or the destination network prefix.
  • netmask netmask_address: Specifies the subnet mask of the destination network.
  • gw gw_address: Specifies the IP address of the gateway to reach the destination network.

Example:

sudo route add -net 10.0.0.0 netmask 255.255.255.0 gw 192.168.0.1

This command adds a route to the network 10.0.0.0/24 with a gateway at 192.168.0.1.

Use case 3: Delete route rule

Code:

sudo route del -net ip_address netmask netmask_address dev gw_address

Motivation: Sometimes, you may need to remove a specific route from the routing table. This command allows you to delete a route that is no longer required.

Explanation:

  • del: Deletes an existing route from the routing table.
  • -net ip_address: Specifies the network IP address or the destination network prefix.
  • netmask netmask_address: Specifies the subnet mask of the destination network.
  • dev gw_address: Deletes the route associated with the specified gateway and device combination.

Example:

sudo route del -net 10.0.0.0 netmask 255.255.255.0 dev eth0

This command removes the route to the network 10.0.0.0/24 that was previously associated with the eth0 device.

Conclusion:

The ‘route’ command is a powerful tool for managing the IP routing table in Linux. It allows you to view, add, and delete routes, providing flexibility in defining network paths and optimizing network traffic. By familiarizing yourself with the various use cases of the ‘route’ command, you gain control over the network routing on your system.

Related Posts

Using httprobe (with examples)

Using httprobe (with examples)

1: Probe a list of domains from a text file cat input_file | httprobe Motivation: This use case is helpful when you have a list of domains stored in a text file and you want to check if there are any working HTTP or HTTPS servers for each domain.

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

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

The ‘info’ command is used to read documentation stored in the info format.

Read More
How to use the command 'electron-packager' (with examples)

How to use the command 'electron-packager' (with examples)

The ’electron-packager’ command is a tool used to build Electron app executables for Windows, Linux, and macOS.

Read More