LinuxCommandLibrary

ip-route

Manage the kernel's routing table

TLDR

Display the main routing table

$ ip [[r|route]]
copy

Add a default route using gateway forwarding
$ sudo ip [[r|route]] [[a|add]] default via [gateway_ip]
copy

Add a default route using eth0
$ sudo ip [[r|route]] [[a|add]] default dev [eth0]
copy

Add a static route
$ sudo ip [[r|route]] [[a|add]] [destination_ip] via [gateway_ip] dev [eth0]
copy

Delete a static route
$ sudo ip [[r|route]] [[d|delete]] [destination_ip] dev [eth0]
copy

Change or replace a static route
$ sudo ip [[r|route]] [change|replace] [destination_ip] via [gateway_ip] dev [eth0]
copy

Show which route will be used by the kernel to reach an IP address
$ ip [[r|route]] [[g|get]] [destination_ip]
copy

Display a specific routing table
$ ip [[r|route]] [[l|list]] [[t|table]] [table_number]
copy

SYNOPSIS

ip route { COMMAND | help }

PARAMETERS

add
    Adds a new route to the routing table.

delete
    Deletes an existing route from the routing table.

change
    Modifies an existing route.

replace
    Replaces an existing route, or creates it if it doesn't exist.

get
    Gets a single route to a destination.

list
    Lists the routing table.

flush
    Flushes the routing table.

monitor
    Continuously monitors routing table changes.

show
    Synonym for list.

-family {inet|inet6|link}
    Specifies the address family: IPv4 (inet), IPv6 (inet6), or link-layer (link).

to DEST
    Specifies the destination network or host.

via GATEWAY
    Specifies the gateway (next hop) IP address.

dev IFACE
    Specifies the output interface.

src ADDRESS
    Specifies the source address.

table TABLE_ID
    Specifies the routing table ID. Common values include main (254), default (253), and local (255).

proto RTPROTO
    Specifies the routing protocol identifier. ex: redirect, kernel, boot, static.

DESCRIPTION

The ip-route command is a powerful tool used in Linux to display and manipulate the system's IP routing table. This table is crucial for determining how network packets are forwarded. With ip-route, administrators can add new routes, delete existing ones, modify route attributes, and examine the current routing configuration. It allows for both simple routing setups and complex, policy-based routing scenarios. The command provides granular control over network traffic flow, enabling features like load balancing, source-based routing, and custom traffic shaping. It is part of the iproute2 package, a suite of tools that have largely replaced the older net-tools package (which includes commands like route). The command provides options for manipulating the kernel's routing table directly, influencing how the system directs network traffic. It's commonly used in network configuration scripts, troubleshooting network connectivity issues, and implementing advanced networking features on Linux servers and workstations.

CAVEATS

Modifying the routing table incorrectly can disrupt network connectivity. Requires root privileges to modify the routing table.

POLICY ROUTING

ip-route is a key component for implementing policy-based routing (PBR). PBR allows you to define routing rules based on various criteria beyond just the destination IP address, such as the source IP address, protocol, or even the application generating the traffic.
You can use multiple routing tables and rules to selectively route traffic based on these criteria.

SCOPE

Routes have an associated scope that defines how far away the destination can be. Common scopes are host (only local), link (on the same network segment), and global (reachable from anywhere). This is important for controlling the reachability of specific routes.

HISTORY

The ip-route command is part of the iproute2 suite, which was developed as a replacement for the older net-tools package.
The iproute2 suite aimed to provide a more modern, flexible, and extensible set of tools for network configuration on Linux systems.
It was primarily developed by Alexey Kuznetsov and others, and it has become the standard toolset for managing network interfaces, routing, and other networking aspects of Linux.

SEE ALSO

ip(8), netstat(1), ss(8), route(8)

Copied to clipboard