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 ethX
$ sudo ip [[r|route]] [[a|add]] default dev [ethX]
copy

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

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

Change or replace a static route
$ sudo ip [[r|route]] [change|replace] [destination_ip] via [gateway_ip] dev [ethX]
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 [OPTIONS] route { add|change|prepend|append|replace|delete|del|get|show|list|flush|save } [SELECTOR] [ACTIONS]

PARAMETERS

add
    Add new route to table

change
    Change existing route

prepend
    Prepend new route to multipath list

append
    Append new route to multipath list

replace
    Replace existing route

delete/del
    Delete route matching selector

get
    Lookup route for destination

show/list
    List routes (default action)

flush
    Flush routes matching selector

save
    Save routing table to stderr

-v/-verbose
    Verbose output

-s/-stats
    Show statistics

-f/-force
    Force flush even if active

-4
    IPv4 only

-6
    IPv6 only

-A/-table TABLEID
    Operate on specific table

from PREFIX
    Source prefix selector

to PREFIX
    Destination prefix selector

via ADDRESS
    Gateway address

dev STRING
    Output interface

metric NUMBER
    Route metric

scope SCOPE
    Route scope (global, link, host)

protocol NAME
    Route protocol (kernel, static, etc.)

type TYPE
    Route type (unicast, local, broadcast, etc.)

nexthop NEXTHOP_SPEC
    Multipath nexthop specification

DESCRIPTION

The ip route command, part of the iproute2 utilities, provides advanced control over the kernel's IP routing tables. It supersedes the older route command from net-tools, offering greater flexibility for adding, modifying, deleting, listing, and flushing routes. ip route supports IPv4 and IPv6, multipath routing, policy routing via multiple tables, and integration with namespaces.

Common use cases include configuring default gateways, static routes for specific networks, or blackhole routes for traffic filtering. For example, adding a route sends traffic through a gateway or interface. The command interacts directly with the kernel via netlink sockets, ensuring efficient and atomic operations.

Output from show or list displays routes with details like destination, gateway, interface, metric, and protocol source (e.g., kernel, static). It supports selectors like from, to, via, dev, metric, and more for precise matching. Policy-based routing uses table IDs (main, default, local, or numeric).

This tool is essential for network administrators, routers, and container orchestration, enabling dynamic routing in complex environments without restarting services.

CAVEATS

Requires root privileges for modifications; changes are not persistent across reboots unless saved (e.g., via ip route save to file and restored); use with caution in production to avoid routing loops.

SELECTORS

Key selectors include from, to, via, src, dev, mtu, window, rtt, table, mark, tos, priority, realms for fine-grained route matching.

MULTIPATH

Use multiple nexthop specs like via 192.168.1.1 dev eth0 weight 1 via 192.168.1.2 dev eth0 weight 1 for load balancing.

HISTORY

Developed by Alexey Kuznetsov as part of iproute2 suite in the late 1990s to replace legacy net-tools (ifconfig, route). Gained prominence with Linux 2.2+ kernels; actively maintained, with major enhancements for policy routing in 2.4 kernels and IPv6/multipath support in subsequent versions.

SEE ALSO

ip(8), route(8), ss(8), tc(8), rdisc6(8)

Copied to clipboard