Home > Network Management > route

route: IP Routing Table Management

The route command is used to view and manipulate the IP routing table in the Linux kernel. It defines the paths that network packets should follow to reach their destinations and provides functionalities such as adding/deleting static routes and setting the default gateway.

Overview

The route command manages the system's routing table to ensure network traffic is sent along the correct paths. It allows for manual configuration of routes to specific destination networks or hosts.

Key Features

  • View current routing table
  • Add and delete static routes
  • Set default gateway
  • Specify routes per network interface

Key Options

The route command uses various options to manipulate or display the routing table.

Displaying the Routing Table

Adding/Deleting Routes

Generated command:

Try combining the commands.

Description:

`route` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Learn how to effectively manage your routing table through various examples of the route command.

View Current Routing Table

route -n

Displays the current system routing table in numeric format.

Add Default Gateway

sudo route add default gw 192.168.1.1

Adds a default route to send traffic for all unknown destinations to a specific gateway.

Add Route for a Specific Network

sudo route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.1

Adds a route to send traffic destined for the 192.168.2.0/24 network through the 192.168.1.1 gateway.

Add Route for a Specific Host

sudo route add -host 10.0.0.1 gw 192.168.1.1

Adds a route to send traffic destined for the host 10.0.0.1 through the 192.168.1.1 gateway.

Delete Default Gateway

sudo route del default gw 192.168.1.1

Deletes the configured default gateway route.

Delete Route for a Specific Network

sudo route del -net 192.168.2.0 netmask 255.255.255.0

Deletes the previously added route for the 192.168.2.0/24 network.

Installation

The route command is part of the `net-tools` package. On modern Linux distributions, the `ip` command from the `iproute2` package is provided by default, and `route` might need to be installed separately.

Debian/Ubuntu

sudo apt update
sudo apt install net-tools

Install `net-tools` using the apt package manager.

CentOS/RHEL/Fedora

sudo yum install net-tools
# or
sudo dnf install net-tools

Install `net-tools` using the yum or dnf package manager.

Tips & Considerations

Useful tips and points to consider when using the route command.

Recommendation to Use `ip` Command

The `route` command is considered a legacy tool. On modern Linux systems, it is recommended to use the `ip route` command from the `iproute2` package. `ip route` is more powerful, flexible, and allows for integrated management of all network-related settings.

  • `ip route show`: View current routing table
  • `sudo ip route add default via 192.168.1.1`: Add default gateway
  • `sudo ip route add 192.168.2.0/24 via 192.168.1.1`: Add network route

Persistent Route Configuration

Routes added with the `route` command will be lost upon system reboot. To make routing settings persistent, you need to modify network configuration files such as `/etc/network/interfaces` (Debian/Ubuntu) or `/etc/sysconfig/network-scripts/route-` (CentOS/RHEL).

Permissions

Commands that modify the routing table, such as `add` or `del`, require root privileges. You must use `sudo` to execute these commands.


Same category commands