ip-route-add
Add routing table entries
TLDR
Add a default route using gateway forwarding
Add a default route using eth0
Add a static route
Add a route to a specific routing table
SYNOPSIS
ip route add [ TYPE PREFIX ] via GATEWAY [ dev IFACE ] [ src ADDRESS ] [ metric METRIC ]
PARAMETERS
TYPE
TYPE: The type of route. Common types include 'unicast' (standard route), 'broadcast', 'local', 'nat', 'anycast', 'multicast', 'blackhole' (drops packets silently), 'unreachable' (returns ICMP network unreachable), 'prohibit' (returns ICMP communication administratively prohibited), and 'throw' (performs policy routing lookup).
PREFIX
PREFIX: The destination network or host in CIDR notation (e.g., 192.168.1.0/24 for a network, 10.0.0.1/32 for a host). This specifies the network/host the route applies to.
via GATEWAY
GATEWAY: The IP address of the gateway (next hop) to use for the route. This is the router that will forward the traffic.
dev IFACE
IFACE: The network interface to use for the route (e.g., eth0, wlan0). This specifies which interface the traffic will exit the system on.
src ADDRESS
ADDRESS: The source IP address to use for traffic matching this route. It's useful in multi-homed systems to select outgoing interface based on source IP.
metric METRIC
METRIC: The metric (distance) of the route. Lower values indicate a more preferred route. Used for route selection when multiple routes to the same destination exist.
proto RTPROTO
RTPROTO: Route protocol identifier. e.g., static, boot, kernel, zebra, bird, etc.
scope SCOPE_VALUE
SCOPE_VALUE: The scope of the route. Specifies how 'far away' the destination is. Common values include 'host' (only this host), 'link' (local network), 'global' (anywhere).
table TABLE_ID
TABLE_ID: The routing table to add the route to. Common values include 'main' (default table, ID 254), 'local' (ID 255), and custom tables defined in `/etc/iproute2/rt_tables` or `/etc/sysconfig/network-scripts/route-*` files.
DESCRIPTION
The `ip route add` command is a powerful tool in Linux for manually manipulating the kernel's routing table. It allows administrators to define specific paths for network traffic to take, overriding the default routing behavior. This is often necessary for complex network configurations, VPNs, or when dealing with multiple network interfaces. You can specify the destination network or host, the gateway to use, the interface to send traffic through, and various other routing attributes. It's essential to understand your network topology before using this command, as incorrect routes can disrupt network connectivity. The command primarily focuses on setting up a route where traffic for a particular network or host should be directed, thereby ensuring optimal and controlled data flow within a network. Improper use can lead to network isolation, thus requires careful considerations and testing after route additions.
CAVEATS
Routes added with `ip route add` are typically not persistent across reboots unless saved to a configuration file (e.g., `/etc/network/interfaces` on Debian-based systems, `/etc/sysconfig/network-scripts/route-*` on Red Hat-based systems). Incorrectly configured routes can severely disrupt network connectivity. Ensure you understand the network topology before making changes. Use the `ip route` command without arguments to view the current routing table.
EXAMPLES
Adding a default route: ip route add default via 192.168.1.1
(sets the gateway for all traffic not matching other more specific routes).
Adding a route to a specific network: ip route add 10.0.0.0/24 via 192.168.1.2 dev eth0
(routes traffic to the 10.0.0.0/24 network through the gateway 192.168.1.2 using the eth0 interface).
Adding a host route: ip route add 172.16.1.100/32 via 192.168.1.3
(routes traffic specifically to the host 172.16.1.100 through the gateway 192.168.1.3).
ROUTING TABLES
Linux supports multiple routing tables. By default routes are added to the main routing table. The routing table id can be changed using the 'table' option, giving very specific routing capabilites. Routing tables are configured in `/etc/iproute2/rt_tables`
HISTORY
The `ip` command suite, including `ip route add`, replaced the older `ifconfig` and `route` commands. It's part of the `iproute2` package, which became the standard toolset for network configuration on Linux systems. It provides a more modern and flexible approach to network management compared to the legacy tools. The `iproute2` package and `ip route add` are actively maintained and widely used in modern Linux distributions. The increased control and enhanced functionalities provided by the `ip` command made it indispensable for system administrators and network engineers.
The iproute2 package's adoption marked a significant shift towards better management capabilities for IPv4 and IPv6 networking.
SEE ALSO
ip(8), ip-route(8), ip-route-list(8), route(8), netstat(1)