Linux route command


The route command in Linux is used to display and manipulate the IP routing table, which defines the paths that packets take when they are sent across the network. It is used to add, delete, or modify routing entries in the system's routing table, as well as to display the current routing information.

While route has been deprecated on many modern Linux distributions in favor of the ip route command, it is still available on many systems and can be useful for legacy scripts or troubleshooting.

Basic Syntax of route:

route [options]

Commonly Used route Commands and Options

  1. Display the Routing Table:

    • To view the current IP routing table, you can use the route command without any options:
    route

    Sample Output:

    Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 192.168.2.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth1
    • Explanation:
      • Destination: The destination network or IP address.
      • Gateway: The gateway through which packets are routed.
      • Genmask: The subnet mask.
      • Flags: Routing flags such as U for Up (route is in use) and G for Gateway.
      • Metric: The "cost" of using this route (lower is better).
      • Iface: The network interface through which packets are sent (e.g., eth0, eth1).
  2. Add a Route:

    • You can add a route to a specific destination network or IP address via a gateway using the route add command.
    route add -net [destination_network] netmask [netmask] gw [gateway]
    • Example: Add a route to the network 192.168.3.0/24 via the gateway 192.168.1.1.
    route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.1.1

    Sample Output:

    [No output, the route has been added]
    • This command will add a route that sends packets destined for the 192.168.3.0/24 network through the gateway 192.168.1.1.
  3. Delete a Route:

    • To delete a specific route, use the route del command.
    route del -net [destination_network] netmask [netmask] gw [gateway]
    • Example: Delete the route to the 192.168.3.0/24 network via the gateway 192.168.1.1.
    route del -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.1.1

    Sample Output:

    [No output, the route has been deleted]
  4. Add a Default Route:

    • A default route is used when there is no specific route for a destination. To add a default route, use the following command:
    route add default gw [gateway]
    • Example: Add a default route via the gateway 192.168.1.1.
    route add default gw 192.168.1.1

    Sample Output:

    [No output, the default route has been added]
    • This command will add a default route that directs all traffic with no specific route to the gateway 192.168.1.1.
  5. Change the Gateway for a Route:

    • To change the gateway for an existing route, you first delete the old route and then add the new one.
    route del -net [destination_network] netmask [netmask] gw [old_gateway] route add -net [destination_network] netmask [netmask] gw [new_gateway]
    • Example: Change the gateway for the 192.168.3.0/24 network from 192.168.1.1 to 192.168.1.2.
    route del -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.1.1 route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.1.2

    Sample Output:

    [No output, the route has been updated]
  6. Show the Routing Table in Numeric Format:

    • You can display the routing table with numerical addresses (no DNS lookup) by using the -n option:
    route -n

    Sample Output:

    Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 192.168.2.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth1
    • Explanation: The numerical addresses will be displayed without attempting to resolve them to hostnames.

Example Scenarios:

  1. Display the current routing table:

    route
  2. Add a new route to the 192.168.4.0/24 network via the gateway 192.168.1.1:

    route add -net 192.168.4.0 netmask 255.255.255.0 gw 192.168.1.1
  3. Delete a specific route to the 192.168.4.0/24 network:

    route del -net 192.168.4.0 netmask 255.255.255.0 gw 192.168.1.1
  4. Add a default route via the gateway 192.168.1.1:

    route add default gw 192.168.1.1
  5. Show the routing table in numeric format (without DNS resolution):

    route -n

Summary of Common route Options:

OptionDescription
routeDisplay the current routing table
route addAdd a new route to the routing table
route delDelete a route from the routing table
route add defaultAdd a default route (for all unknown destinations)
-nDisplay the routing table with numeric addresses (no DNS lookup)
-netAdd or delete a route to a network (use with netmask)
gwSpecify the gateway for the route
-hostAdd or delete a route to a specific host (not a network)

Conclusion:

The route command is a powerful tool for managing the routing table in Linux. It allows you to add, remove, and modify routes to control how traffic is routed across the network. Although it has been largely replaced by ip route on modern systems, route is still commonly used for troubleshooting and basic route management. Understanding how to use it is essential for network administrators and those working with network configurations.