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 route { add | change | replace | delete | show | get | flush | save | restore | monitor } { SELECTOR | ARGUMENTS }

Common usage examples:
ip route add { default | PREFIX } [ via ADDRESS ] [ dev NAME ] [ metric METRIC ] ...
ip route delete { default | PREFIX } ...
ip route show [ dev NAME ] [ scope SCOPE ] [ table TABLE_ID ]

PARAMETERS

add
    Adds a new route entry to the routing table.

change
    Changes attributes of an existing route entry.

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

delete
    Deletes a route entry from the routing table.

show
    Displays the current routing table entries. This is the default command if none is specified.

get SELECTOR
    Performs a route lookup to a specific destination and displays the route that would be used.

flush
    Flushes (deletes) all entries or a specific set of entries from a routing table or the cache.

save
    Saves the current routing table to a file, typically in a format suitable for restore.

restore
    Restores routing table entries from a saved file.

monitor
    Continuously monitors for changes in the routing table and prints updates.

default
    Used with add, delete, change, replace to refer to the default route (0.0.0.0/0 for IPv4, ::/0 for IPv6).

PREFIX
    The destination IP address or network (e.g., 192.168.1.0/24, 10.0.0.1). Can be omitted for default routes.

via ADDRESS
    Specifies the IP address of the gateway through which traffic for the route should be forwarded.

dev NAME
    Specifies the outgoing network interface name (e.g., eth0, enp3s0) through which packets for the route will be sent.

src ADDRESS
    Specifies the preferred source IP address to be used for packets matching this route.

metric VALUE
    Assigns a preference value (cost) to the route. Lower values indicate higher preference when multiple routes to the same destination exist.

scope { global | link | host | SCOPE_ID }
    Defines the scope of the route's validity: global (reachable anywhere), link (reachable on this link), host (reachable only on this host).

table { main | local | default | TABLE_ID }
    Specifies the routing table to operate on. The main table is the default. Others are used for policy routing.

onlink
    Indicates that the gateway address specified by via is directly reachable on the local link, even if it's not on the same subnet as the local interface.

proto { kernel | boot | static | dhcp | ... }
    Specifies the protocol by which the route was installed. Used primarily for informational purposes with show, indicating how the route was learned.

cached
    Used with show to display routes from the kernel's routing cache.

DESCRIPTION

The ip route command is a fundamental utility within the iproute2 suite, designed to manage the Linux kernel's IP routing tables. It provides comprehensive functionalities to view, add, change, or delete routing entries for both IPv4 and IPv6 protocols. Unlike the older route command from the net-tools package, ip route offers superior capabilities for advanced routing policies, multiple routing tables, and better integration with modern networking concepts. It's essential for configuring network connectivity, directing traffic through specific interfaces or gateways, and setting up complex network topologies, such as multi-homed servers, VPNs, or load balancing scenarios. Understanding and utilizing ip route is crucial for network administrators and anyone managing Linux-based network infrastructure.

CAVEATS

The ip route command requires root privileges to modify the routing table, and typically for most show operations unless the information is publicly available. Incorrect manipulation of routing tables can lead to network connectivity loss or unexpected traffic routing, potentially impacting system or network operations. For complex policy routing, it's often used in conjunction with ip rule, which adds another layer of complexity. It's crucial to understand the implications of changes before applying them.

ROUTING TABLES

The Linux kernel can maintain multiple routing tables, not just a single one. By default, most routing operations interact with the main table. Other standard tables include local (for routes to local and broadcast addresses) and default (an empty table used as a placeholder). Users can define additional custom tables (identified by numbers or names in /etc/iproute2/rt_tables) for advanced scenarios like policy-based routing, where different traffic flows use different routing decisions based on various criteria.

POLICY ROUTING

While ip route manages the routing table entries, policy routing allows the system to select which routing table to use based on source address, destination address, incoming interface, or other packet attributes. This is achieved using the ip rule command (part of iproute2). For example, traffic originating from a specific source IP might use a different routing table than general outbound traffic, enabling complex network configurations like multi-WAN setups or specific traffic engineering.

DEFAULT GATEWAY

The 'default gateway' is a crucial routing entry that specifies where to send packets when there is no more specific route defined in the routing table. In ip route terminology, this is represented by a destination of 0.0.0.0/0 (for IPv4) or ::/0 (for IPv6). A common command to add a default gateway would be ip route add default via GATEWAY_IP dev INTERFACE, while ip route show will display the currently configured default route if one exists.

HISTORY

The ip route command is a core component of the iproute2 suite, which began development in the late 1990s as a modern replacement for the older net-tools utilities (such as route, ifconfig, netstat). The primary motivation for iproute2 was to provide a more robust and feature-rich interface to the Linux kernel's advanced networking capabilities, including support for IPv6, policy routing, traffic control, and multiple routing tables. ip route specifically took over the role of managing kernel routing tables, offering capabilities that the legacy route command lacked, making it the standard tool for network configuration on modern Linux systems.

SEE ALSO

ip(8), ip-link(8), ip-addr(8), ip-rule(8), route(8) (legacy command)

Copied to clipboard