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.

  1. View the Routing Table:

    route

    or

    route -n

    The -n option prevents the command from attempting to resolve IP addresses to hostnames, making the output quicker to generate and easier to read.

  2. Add a Default Gateway:

    route add default gw 192.168.1.1

    This command sets the default gateway to 192.168.1.1.

  3. Add a Route to a Specific Network:

    route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.1

    This command adds a route to the network 192.168.2.0 with a netmask of 255.255.255.0, routing through the gateway 192.168.1.1.

  4. Add a Host Route:

    route add -host 192.168.1.2 gw 192.168.1.1

    This command adds a route for a specific host 192.168.1.2 through the gateway 192.168.1.1.

  5. Delete a Route:

    route del -net 192.168.2.0 netmask 255.255.255.0

    This command removes the route to the network 192.168.2.0.

Example of route Command Output

When you run route -n, you might see output similar to this:

Using ip Command for Routing

The ip command provides more functionality and is the modern replacement for route. Here are equivalent commands using ip:

  1. View the Routing Table:

  2. Add a Default Gateway:

  3. Add a Route to a Specific Network:

  4. Add a Host Route:

  5. Delete a Route:

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

breakdown

Last updated