ip route Command in Linux: Syntax, Examples, Cheat Sheet & Routing Guide

ip route Command in Linux: Syntax, Examples, Cheat Sheet & Routing Guide

The ip route command in Linux is used to display, add, delete, and modify entries in the system routing table. It is part of the iproute2 package and replaces legacy networking tools like route and netstat.


Common ip route Syntax Patterns

Basic ip route syntax explained

The general syntax of the command is:

bash
ip route { add | del | change | replace | show } DESTINATION OPTIONS

Where:

  • add → Adds a new route
  • del → Deletes a route
  • change → Modifies an existing route
  • replace → Updates or creates a route
  • show → Displays the routing table

Example:

bash
ip route add 192.168.2.0/24 via 192.168.1.1

Route management commands (add, del, change)

CommandDescription
ip route showDisplay routing table
ip route addAdd a new route
ip route delDelete an existing route
ip route changeModify an existing route
ip route replaceReplace or create a route
ip route flushRemove routes from table

Route attributes and parameters

ParameterDescription
viaNext-hop gateway
devOutgoing network interface
srcPreferred source IP address
metricRoute priority
tableRouting table ID
protoRouting protocol identifier
scopeRoute scope

Example:

bash
ip route add 192.168.10.0/24 via 192.168.1.1 dev eth0 metric 50

Routing table identifiers

Linux systems can maintain multiple routing tables.

Common table identifiers include:

TableDescription
mainDefault routing table
localLocal interface routes
defaultDefault table used when no policy rule matches
custom tablesDefined in /etc/iproute2/rt_tables

Example command:

bash
ip route show table main

Special route types

Special route types control how packets are handled rather than forwarding them normally.

Route TypeDescription
blackholeSilently drops packets
unreachableReturns ICMP destination unreachable
prohibitReturns ICMP communication prohibited

Example:

bash
ip route add blackhole 192.168.200.0/24

This configuration discards all packets destined for the specified network.


Quick ip route Command Cheat Sheet

The following quick reference table summarizes the most commonly used ip route command syntax and options used by Linux administrators.

Task / DescriptionCommand
Display routing tableip route show
Display routing table (short form)ip route
Display routing table in specific tableip route show table main
Display routes for specific interfaceip route show dev eth0
Display routes to specific networkip route show 192.168.1.0/24
Show default gatewayip route show default
Show routing decision for destinationip route get 8.8.8.8
Show routing decision using specific source IPip route get 8.8.8.8 from 192.168.1.10
Add default gatewayip route add default via 192.168.1.1
Add default route via interfaceip route add default dev eth0
Add route to network via gatewayip route add 192.168.2.0/24 via 192.168.1.1
Add route via specific interfaceip route add 192.168.2.0/24 dev eth1
Add route with metricip route add 192.168.2.0/24 via 192.168.1.1 metric 100
Add route with source addressip route add 192.168.2.0/24 via 192.168.1.1 src 192.168.1.10
Add route to specific routing tableip route add 192.168.2.0/24 via 192.168.1.1 table 100
Replace existing routeip route replace 192.168.2.0/24 via 192.168.1.2
Change gateway for existing routeip route change 192.168.2.0/24 via 192.168.1.5
Delete route to networkip route del 192.168.2.0/24
Delete default routeip route del default
Delete route via gatewayip route del 192.168.2.0/24 via 192.168.1.1
Flush all routes in main tableip route flush table main
Flush all routesip route flush cache
Add blackhole route (drop packets silently)ip route add blackhole 192.168.3.0/24
Add unreachable route (return error)ip route add unreachable 192.168.4.0/24
Add prohibit route (administratively blocked)ip route add prohibit 192.168.5.0/24
Add route with preferred sourceip route add 192.168.10.0/24 via 192.168.1.1 src 192.168.1.100
Add multipath route (load balancing)ip route add 10.0.0.0/24 nexthop via 192.168.1.1 nexthop via 192.168.1.2
Add route with MTU settingip route add 192.168.20.0/24 via 192.168.1.1 mtu 1400
Add route with protocol identifierip route add 192.168.30.0/24 via 192.168.1.1 proto static
Add route with scopeip route add 192.168.40.0/24 dev eth0 scope link
Show all routing tablesip route show table all

View and Understand Routing Table in Linux

Understanding the routing table is the first step when troubleshooting network connectivity in Linux. The routing table determines how packets are forwarded between interfaces and gateways. Using the ip route command, administrators can quickly inspect the current routing configuration of the system.

Display routing table using ip route

To display the routing table in Linux:

bash
ip route

Example output:

text
default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10
10.0.0.0/8 via 192.168.1.2 dev eth1

Explanation:

  • default → Default route used when no other route matches
  • via → Next-hop gateway
  • dev → Network interface used for the route
  • src → Preferred source IP address

This command is commonly used when verifying network configuration or connectivity issues.

Show routing table in detailed format

To display routes along with additional attributes:

bash
ip route show

You can also display routes from the main routing table explicitly:

bash
ip route show table main

This helps when working with multiple routing tables or policy routing configurations.

Filter routes for specific interface

To view routes associated with a particular network interface:

bash
ip route show dev eth0

Example output:

text
default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10

This command is useful when diagnosing multi-interface servers or containers.

Show routes from specific routing table

Linux supports multiple routing tables used for advanced routing configurations.

To view routes from a specific table:

bash
ip route show table 100

To list all routing tables:

bash
ip route show table all

This is commonly used in policy-based routing setups.

Understand default route in Linux

The default route defines where packets should be sent when no other route matches the destination network.

To display the default route:

bash
ip route show default

Example output:

text
default via 192.168.1.1 dev eth0

Explanation:

  • default → Catch-all route
  • via 192.168.1.1 → Gateway used to reach external networks
  • dev eth0 → Interface used for outgoing traffic

Without a default route, the system cannot reach networks outside its local subnet.


Find Which Route a Packet Will Use

Sometimes multiple routes exist for the same destination. Linux decides which route to use based on metrics, routing tables, and policies. The ip route get command helps determine exactly which route the kernel will use.

Use ip route get to trace packet path

To check how the system routes packets to a destination:

bash
ip route get 8.8.8.8

Example output:

text
8.8.8.8 via 192.168.1.1 dev eth0 src 192.168.1.10

Explanation:

  • via → Gateway used
  • dev → Interface used
  • src → Source IP chosen by the kernel

This command is extremely useful for debugging routing issues.

Determine gateway used for a destination

You can verify which gateway will be used to reach a host:

bash
ip route get 1.1.1.1

Example output:

text
1.1.1.1 via 192.168.1.1 dev eth0 src 192.168.1.10

This confirms the next-hop gateway used by the system.

Check routing decision for a specific source IP

If a system has multiple IP addresses, Linux can choose different routes depending on the source IP.

To test this behavior:

bash
ip route get 8.8.8.8 from 192.168.1.20

This command simulates routing decisions for packets originating from the specified source address.

Troubleshoot asymmetric routing

Asymmetric routing occurs when incoming and outgoing traffic follow different paths.

Use the following command to analyze the outgoing route:

bash
ip route get <destination-ip>

Then verify incoming routes on the gateway or firewall.
This technique helps troubleshoot firewall drops and connection failures.


Add Routes in Linux

Adding routes allows administrators to control how traffic flows between networks. Static routes are often used in environments with multiple routers, VLANs, or isolated networks.

Add default gateway using ip route

To configure a default gateway:

bash
ip route add default via 192.168.1.1

This ensures all unknown network traffic is forwarded through the specified gateway.

Add route to a specific subnet

To route traffic to another network through a gateway:

bash
ip route add 192.168.2.0/24 via 192.168.1.2

Explanation:

  • 192.168.2.0/24 → Destination network
  • 192.168.1.2 → Next-hop gateway

Add route through a specific network interface

If the network is directly reachable through an interface:

bash
ip route add 10.10.0.0/16 dev eth1

This route sends traffic directly through eth1.

Add route with custom metric value

Metrics determine route priority when multiple routes exist.

bash
ip route add 192.168.3.0/24 via 192.168.1.2 metric 50

Lower metric values are preferred over higher ones.

Example:

text
metric 10 → preferred
metric 50 → backup route

Add persistent static route (distribution specific)

Routes added using ip route are temporary and will disappear after a reboot.

To make routes persistent, configure them in your network configuration:

Common locations:

DistributionConfiguration File
Ubuntu/etc/netplan/*.yaml
RHEL / Rocky Linux/etc/sysconfig/network-scripts/route-eth0
NetworkManagernmcli connection modify
systemd-networkd/etc/systemd/network/*.network

Delete and Modify Existing Routes

Routing tables often need updates when network topology changes. The ip route command allows administrators to remove or modify routes dynamically.

Remove a route using ip route del

To delete a route:

bash
ip route del 192.168.2.0/24

If the route includes a gateway:

bash
ip route del 192.168.2.0/24 via 192.168.1.2

Replace existing route using ip route replace

To replace a route without manually deleting it first:

bash
ip route replace 192.168.2.0/24 via 192.168.1.5

This command safely updates the routing table.

Modify route gateway

If a gateway changes, update the route using:

bash
ip route change 192.168.2.0/24 via 192.168.1.10

This updates the next-hop gateway for the destination network.

Change route metric priority

To change the route priority:

bash
ip route change 192.168.2.0/24 via 192.168.1.2 metric 100

Routes with lower metric values are preferred by the Linux kernel.

Example scenario:

text
Primary route  → metric 10
Backup route   → metric 100

If the primary route fails, Linux automatically uses the backup route.


Configure Multiple Default Routes

In environments with multiple internet connections or gateways, Linux can maintain more than one default route. This is commonly used for failover, redundancy, or load balancing. The ip route command allows administrators to configure multiple default routes with different priorities using route metrics.

Configure failover internet gateway

You can configure a primary and secondary gateway so that traffic automatically switches if the primary gateway becomes unreachable.

Example:

bash
ip route add default via 192.168.1.1 metric 10
ip route add default via 192.168.2.1 metric 20

Explanation:

  • 192.168.1.1 → Primary gateway
  • 192.168.2.1 → Backup gateway
  • Lower metric routes are preferred

If the first gateway becomes unavailable, the system will automatically use the second route.

Configure backup default route with higher metric

Linux selects routes based on metric values. Lower metric values have higher priority.

Example configuration:

bash
ip route add default via 10.0.0.1 metric 5
ip route add default via 10.0.1.1 metric 50

In this setup:

  • Traffic normally uses gateway 10.0.0.1
  • Gateway 10.0.1.1 acts as a backup path

Load balancing using multiple routes

Linux can distribute traffic across multiple gateways using Equal Cost Multi Path (ECMP) routing.

Example:

bash
ip route add default \
nexthop via 192.168.1.1 dev eth0 \
nexthop via 192.168.2.1 dev eth1

This configuration allows outgoing traffic to be balanced between the two gateways.


Configure Static Routes for Internal Networks

Static routes are used when networks must communicate through a specific router or interface. They are commonly used in multi-subnet environments, VLAN networks, and internal infrastructure networks.

Route traffic to internal subnets

To route traffic to another internal network through a gateway:

bash
ip route add 10.10.0.0/16 via 192.168.1.2

Explanation:

  • 10.10.0.0/16 → Destination internal network
  • 192.168.1.2 → Router responsible for reaching that network

Configure routes between multiple network interfaces

If the destination network is reachable through another interface directly:

bash
ip route add 172.16.0.0/16 dev eth1

This route instructs Linux to send packets to the 172.16.0.0/16 network through the eth1 interface.

Route traffic between VLAN networks

When VLANs are separated into different networks, routing may be required between them.

Example:

bash
ip route add 192.168.50.0/24 via 192.168.10.1

This allows traffic to reach VLAN 192.168.50.0/24 through router 192.168.10.1.


Block or Control Traffic Using Special Routes

The ip route command supports special route types that can control or block network traffic. These routes are often used for security policies, traffic filtering, or preventing access to specific networks.

Drop packets using blackhole route

A blackhole route silently discards packets without notifying the sender.

Example:

bash
ip route add blackhole 192.168.100.0/24

Any traffic destined for 192.168.100.0/24 will be dropped.

Reject traffic using unreachable route

An unreachable route drops packets and returns an ICMP destination unreachable message.

Example:

bash
ip route add unreachable 192.168.101.0/24

The sender receives a notification that the destination cannot be reached.

Deny traffic using prohibit route

A prohibit route blocks access and returns an ICMP communication prohibited error.

Example:

bash
ip route add prohibit 192.168.102.0/24

Troubleshoot Network Connectivity Using ip route

When a system cannot reach another network or the internet, examining the routing table is often the first troubleshooting step. The ip route command provides essential insights into how packets are being forwarded.

Detect missing default gateway

If the system cannot reach external networks, verify the default route:

bash
ip route show default

If no default route appears, external connectivity will fail.

Example fix:

bash
ip route add default via 192.168.1.1

Identify wrong gateway configuration

To verify which route the kernel will use:

bash
ip route get 8.8.8.8

Example output:

text
8.8.8.8 via 192.168.1.1 dev eth0 src 192.168.1.10

If the gateway is incorrect, update the route accordingly.

Detect routing loops

Routing loops can occur when traffic repeatedly cycles between routers.

Inspect the routing table:

bash
ip route show

Look for routes pointing to gateways that may redirect traffic back to the originating host.

Verify routing changes after network restart

Routes configured using ip route are temporary. After a reboot or network restart, they may disappear.

Verify routes again:

bash
ip route

If routes are missing, configure them in the system’s persistent network configuration files.


Policy Based Routing (Advanced)

Policy-based routing (PBR) allows Linux to route traffic based on criteria such as source IP address, incoming interface, or routing tables, instead of relying only on the destination network. This approach is useful in complex networking environments where different traffic flows must use different gateways or network paths.

Create custom routing table

Linux supports multiple routing tables. To create a custom routing table, add an entry to the routing tables configuration file.

bash
echo "100 customrt" >> /etc/iproute2/rt_tables

In this example:

  • 100 is the table ID
  • customrt is the name of the routing table

You can now reference this routing table when adding routes.

Example:

bash
ip route add default via 192.168.2.1 table customrt

Add routing rules using ip rule

Routing rules determine which routing table should be used based on defined conditions.

Example rule:

bash
ip rule add from 192.168.1.100 table customrt

Explanation:

  • Traffic originating from 192.168.1.100 will use the customrt routing table.

To view active rules:

bash
ip rule show

Route traffic based on source IP

Policy-based routing allows different hosts or services to use different gateways.

Example:

bash
ip rule add from 192.168.10.50 table customrt
ip route add default via 192.168.2.1 table customrt

This configuration forces traffic from 192.168.10.50 to use the gateway 192.168.2.1.

Route traffic based on network interface

Traffic can also be routed depending on which interface it arrives on.

Example:

bash
ip rule add iif eth1 table customrt

Explanation:

  • iif represents the incoming interface
  • Traffic entering through eth1 will follow routes defined in customrt.

This technique is commonly used in firewall gateways and advanced routing environments.


Multi-homed Linux Server Configuration

A multi-homed server has multiple network interfaces connected to different networks or ISPs. Linux can manage multiple routes to control how traffic flows through these interfaces.

Configure multiple internet connections

To configure two internet gateways:

bash
ip route add default via 192.168.1.1 metric 10
ip route add default via 192.168.2.1 metric 20

Explanation:

  • 192.168.1.1 → Primary ISP
  • 192.168.2.1 → Secondary ISP

The lower metric route is preferred.

Control outbound traffic per interface

Policy routing can force traffic from a specific interface or subnet to use a particular gateway.

Example:

bash
ip rule add from 192.168.2.0/24 table isp2
ip route add default via 192.168.2.1 table isp2

This configuration ensures that traffic from the 192.168.2.0/24 network uses the second ISP.

Use metrics to prioritize ISPs

Metrics determine which route the kernel prefers when multiple paths exist.

Example:

bash
ip route add default via 10.0.0.1 metric 5
ip route add default via 10.0.1.1 metric 50

In this setup:

  • 10.0.0.1 is the preferred ISP
  • 10.0.1.1 acts as a backup connection

This configuration provides automatic failover.


Advanced Routing Features in Linux

Linux supports several advanced routing features that improve network redundancy, performance, and flexibility.

Equal Cost Multi Path (ECMP) routing

ECMP allows traffic to be distributed across multiple gateways with the same cost.

Example:

bash
ip route add default \
nexthop via 192.168.1.1 dev eth0 \
nexthop via 192.168.2.1 dev eth1

With ECMP, the system balances outgoing traffic across both gateways.

Route traffic through tunnel interface

When using VPN tunnels, traffic may need to be routed through a tunnel interface.

Example:

bash
ip route add 10.10.0.0/16 dev tun0

Explanation:

  • tun0 represents a tunnel interface created by VPN software.

Traffic destined for 10.10.0.0/16 will now pass through the VPN tunnel.

Route traffic through VPN gateway

To route traffic through a VPN gateway:

bash
ip route add 172.16.0.0/16 via 10.8.0.1 dev tun0

This configuration ensures that traffic destined for the remote network uses the VPN gateway.


Make ip route Changes Persistent

Routes configured with ip route are temporary and will disappear after a reboot or network restart. To make routing changes persistent, they must be configured in the system’s network configuration files.

Persist routes in NetworkManager

Using NetworkManager CLI:

bash
nmcli connection modify eth0 +ipv4.routes "192.168.50.0/24 192.168.1.1"
nmcli connection up eth0

This permanently adds the route to the NetworkManager configuration.

Persist routes in Netplan (Ubuntu)

Edit the Netplan configuration file:

text
/etc/netplan/*.yaml

Example configuration:

yaml
network:
  version: 2
  ethernets:
    eth0:
      routes:
        - to: 192.168.50.0/24
          via: 192.168.1.1

Apply the configuration:

bash
netplan apply

Persist routes in RHEL / Rocky Linux

Edit the route configuration file for the interface:

text
/etc/sysconfig/network-scripts/route-eth0

Example:

text
192.168.50.0/24 via 192.168.1.1

Restart networking:

bash
systemctl restart network

Persist routes using systemd-networkd

Create or edit the interface configuration file:

text
/etc/systemd/network/10-eth0.network

Example configuration:

text
[Route]
Destination=192.168.50.0/24
Gateway=192.168.1.1

Restart the service:

bash
systemctl restart systemd-networkd

This ensures the routing configuration is automatically applied after reboot.


Why route command is deprecated

The route command belongs to the legacy net-tools package, which is no longer actively maintained. Modern Linux distributions recommend using iproute2 utilities instead because they support newer networking features and kernel capabilities.

Limitations of the route command include:

  • Limited support for advanced routing features
  • No support for policy-based routing
  • Less structured output compared to ip commands
  • Missing support for modern networking technologies

Because of these limitations, administrators are encouraged to use the ip route command for managing routing tables.

Example of legacy route command:

bash
route -n

Modern equivalent:

bash
ip route

Differences between ip route and route

The following table highlights the key differences between the two tools.

Featureip routeroute
Packageiproute2net-tools
Maintenance statusActively maintainedDeprecated
Policy routing supportYesNo
Multipath routing supportYesNo
Output formatStructured and script-friendlyLess structured
Modern kernel featuresSupportedLimited

Example comparison:

Display routing table:

bash
ip route

Legacy command:

bash
route -n

When legacy route command may still appear

Although deprecated, the route command may still exist in some environments:

  • Older Linux distributions
  • Systems where the net-tools package is manually installed
  • Legacy scripts written before iproute2 became standard

In such cases, administrators may still encounter commands like:

bash
route add default gw 192.168.1.1

Modern equivalent:

bash
ip route add default via 192.168.1.1

For new configurations and automation scripts, the ip command suite should always be preferred.


Frequently Asked Questions

1. What is the ip route command used for in Linux?

The ip route command is used to display and modify the routing table in Linux systems. It allows administrators to view routes, add static routes, delete routes, and control how packets are forwarded between networks.

2. How do I display the routing table in Linux?

You can display the routing table using the command ip route or ip route show. This command lists all routes, gateways, interfaces, and routing priorities configured on the system.

3. How do I add a route using ip route command?

To add a static route in Linux, use a command like ip route add 192.168.2.0/24 via 192.168.1.1. This instructs the system to send traffic destined for the specified network through the given gateway.

4. What is the difference between ip route and route command?

The ip route command is part of the modern iproute2 networking suite and supports advanced routing features such as policy-based routing and multipath routing. The route command belongs to the older net-tools package and is considered deprecated on modern Linux systems.

Summary

The ip route command is the modern and recommended tool for managing routing tables in Linux systems. It provides administrators with powerful capabilities to inspect routing behavior, configure static routes, manage multiple gateways, and implement advanced routing policies.

Key takeaways:

  • ip route is part of the iproute2 suite and replaces legacy tools like route.
  • It allows administrators to view, add, modify, and delete routes dynamically.
  • Advanced features such as policy-based routing, multipath routing, and special routes make it suitable for modern networking environments.
  • Routes configured using ip route are typically temporary, so persistent configurations must be defined in system network configuration files.

Understanding how to use the ip route command effectively helps administrators troubleshoot connectivity problems and manage complex network environments with confidence.


Official Documentation

For more detailed information about Linux routing and the ip route command, refer to the official resources:

Rohan Timalsina

Rohan Timalsina

is a technical writer and Linux enthusiast who writes practical guides on Linux commands and system administration. He focuses on simplifying complex topics through clear explanations.