ip-route
manages the kernel routing table
TLDR
Show routing table
$ ip route
Add default gateway$ sudo ip route add default via [gateway_ip]
Add default via interface$ sudo ip route add default dev [eth0]
Add static route$ sudo ip route add [10.0.0.0/24] via [gateway_ip] dev [eth0]
Add a route with a metric and preferred source address$ sudo ip route add [10.0.0.0/24] via [gateway_ip] metric [100] src [local_ip]
Replace a route, adding it if it does not exist (idempotent)$ sudo ip route replace default via [gateway_ip] dev [eth0]
Delete route$ sudo ip route delete [10.0.0.0/24] dev [eth0]
Change route$ sudo ip route change [10.0.0.0/24] via [gateway_ip] dev [eth0]
Add a blackhole route that silently drops traffic$ sudo ip route add blackhole [203.0.113.0/24]
Add a multipath (ECMP) default route$ sudo ip route add default nexthop via [gw1] weight 1 nexthop via [gw2] weight 1
Get route to destination$ ip route get [destination_ip]
Show specific table$ ip route list table [100]
Flush all routes of a device$ sudo ip route flush dev [eth0]
Save and restore the routing table$ ip route save > [routes.bin] && sudo ip route restore < [routes.bin]
SYNOPSIS
ip [options] route { command | help }ip route { show | flush } SELECTORip route { add | del | change | append | replace } ROUTEip route get ADDRESS [options]ip route { save | restore }
DESCRIPTION
ip route manages the kernel routing table. It can add, delete, and modify routes, as well as query which route the kernel will use for a specific destination.
PARAMETERS
list (or no command)
Display the routing tableadd
Add a new routedelete
Remove a routechange
Modify an existing routereplace
Change or add if not existsappend
Add a route even if one with the same key exists (for IPv6, adds a nexthop to a multipath route)flush SELECTOR
Remove all routes matching the selectorsave, restore
Dump routes as raw netlink data to stdout, or restore them from stdinget address
Show route for a specific destinationdefault
Default gateway routevia gateway
Specify next-hop gatewaydev interface
Specify output interfacetable id
Work with a specific routing tablesrc address
Preferred source address for traffic to this destinationmetric number
Route priority; lower values are preferredproto rtproto
Routing protocol identifier: kernel, boot, static, dhcp, or a numberscope scope
Route scope: global, link, or hostonlink
Pretend the next hop is directly attached even if it does not match any prefixnexthop NH
Define a next hop of a multipath route (can be repeated, with optional weight)mtu number
Set the path MTU for the routetype
Route type: unicast (default), local, broadcast, multicast, blackhole, unreachable, prohibit, throw
INSTALL
sudo apt install iproute2
sudo pacman -S iproute2
sudo apk add iproute2-minimal
sudo zypper install iproute2
brew install iproute2
nix profile install nixpkgs#iproute2
CAVEATS
Routes added are not persistent; use network configuration files for persistence. Multiple routing tables can be used with policy routing. The default table is "main".
HISTORY
ip route is part of iproute2, written by Alexey Kuznetsov in the late 1990s, replacing the deprecated route command from net-tools.
SEE ALSO
ip(8), ip-route-get(8), ip-route-list(8), ip-rule(8), ip-address(8), routel(8), route(8)
