ip route add Tutorial for Linux

Thursday, Aug 8, 2024 | 5 minutes read | Update at Thursday, Aug 8, 2024

@ İsmail Baydan

Linux provides the ip route add command inorder to add new routes to the network configuration. Routes are used to specify the network paths to reach destination hosts and networks. Every network packet is redirected to the relevant network interface (ethernet or wifi) according to the specified routes or route table. Here are 20 examples of using the ip route add command to manipulate and configure routing tables in Linux:

ip route add Syntax The syntax of the ip route add command is like below.

ip route add DESTINATION_NETWORK GATEWAY
  • DESTINATION_NETWORK is the destination network or host with subnetmask.
  • GATEWAY is the next network to redirect the packet which is generally a connected network with an interface.
  1. Add a default gateway: The most popular usage with the ip route add setting the default network. With this command we will set gateway for all destinations those do not defined explicitly in the route table. default is used for default and via 192.168.1.1 for the gateway.

    sudo ip route add default via 192.168.1.1
    

    Sets the default gateway to 192.168.1.1.

  2. Add a route to a specific network: We can use the ip route add to add new routes for specific destination networks. In the following example we set gateway 192.168.1.1 for the destination network 192.168.10.0/24

    sudo ip route add 192.168.10.0/24 via 192.168.1.1
    

    Routes traffic destined for the 192.168.10.0/24 network through the gateway 192.168.1.1.

  3. Add a route to a specific host: Even ip route add generally used for networks we can also use it to define route to the specific host. We just put the destination host address and the gateway address. The destination address is 192.168.10.10 and gateway is 192.168.1.1

    sudo ip route add 192.168.10.10 via 192.168.1.1
    

    Routes traffic to the host 192.168.10.10 via the gateway 192.168.1.1.

  4. Add a route with a specific interface: We can specifiy the gateway as the interface instead of the gateway IP address. In the following example we set default gateway as eth0. But be carefull we use dev instead of the via as the eth0 is a device.

    sudo ip route add 192.168.20.0/24 dev eth0
    

    Routes traffic to the 192.168.20.0/24 network through the eth0 interface.

  5. Add a route with a specific metric: Multiple routes can be defined for a single destination network or host. We can prioritize some route over others by using metrics. We can use the metric option with the metric value. Lower metric values have precedence over higher values.

    sudo ip route add 192.168.30.0/24 via 192.168.1.1 metric 100
    

    Adds a route to the 192.168.30.0/24 network with a metric of 100.

  6. Add a route to a host using a specific source IP: We can create a new route by setting specific source IP address. Single interface may have multiple IP addresses and when setting a route we can specify a specific source IP address. The src option can be used to specific source IP address.

sudo ip route add 172.16.0.10 via 192.168.1.1 src 192.168.1.100

Routes traffic to the host 172.16.0.10 using 192.168.1.100 as the source IP.

  1. Add a route with a blackhole (discard traffic): Some times we may need to create a black hole in order to drop packets for a specific network. We can use the blackhole option as the destination like below.

    sudo ip route add 192.168.40.0/24 blackhole
    

    Discards traffic destined for the 192.168.40.0/24 network.

  2. Add a route with a prohibit (block traffic): We can block network traffic for a destination network with the ip route add command. We should add prohibit option as the gateway.

    sudo ip route add 192.168.50.0/24 prohibit
    

    Blocks traffic to the 192.168.50.0/24 network.

  3. Add a route with a reject (return ICMP unreachable): We can simply reject for a destination network by adding reject as the gateway. This will return ICMP Unreachable packets when network packets are redirected to the specified destinatio network.

    sudo ip route add 192.168.60.0/24 reject
    

    Returns an ICMP unreachable message for traffic to the 192.168.60.0/24 network.

  4. Add a route using a gateway for IPv6: Until now we examined the ip route add command for the most popular IP version named IPv4 which is defacto. The ip route add command also supports IPv6 where we should use ip -6 route add. As you gues the destination network and gateway is provided as IPv6.

    sudo ip -6 route add 2001:db8::/32 via 2001:db8::1
    

    Routes IPv6 traffic to the 2001:db8::/32 network through the gateway 2001:db8::1.

  5. Add a multicast route: We can also add multicast IP addresses for route table.

    sudo ip route add 224.0.0.0/4 dev eth0
    

    Routes multicast traffic (224.0.0.0/4) through the eth0 interface.

  6. Add a route to a network using a link-local address:

    sudo ip route add 10.0.0.0/8 via fe80::1 dev eth0
    

    Routes traffic to 10.0.0.0/8 network using a link-local IPv6 address.

  7. Add a policy routing rule (example: based on source IP):

    sudo ip rule add from 192.168.1.100 lookup 100
    sudo ip route add table 100 default via 192.168.1.1
    

    Directs traffic from 192.168.1.100 to use a specific routing table.

  8. Add a route with a multipath configuration (ECMP):

    sudo ip route add 192.168.70.0/24 nexthop via 192.168.1.1 nexthop via 192.168.2.1
    

    Routes traffic to 192.168.70.0/24 using both 192.168.1.1 and 192.168.2.1 gateways for load balancing.

  9. Add a route with a specific MTU size:

    sudo ip route add 192.168.80.0/24 via 192.168.1.1 mtu 1400
    

    Sets the MTU to 1400 for traffic to the 192.168.80.0/24 network.

  10. Add a route to a VPN network via a specific gateway:

    sudo ip route add 10.8.0.0/24 via 192.168.1.1
    

    Routes traffic to a VPN network (10.8.0.0/24) through the gateway 192.168.1.1.

  11. Add a local route for a loopback interface: We can create route for the loopback interface.

    sudo ip route add 127.0.0.0/8 dev lo
    

    Ensures local traffic (127.0.0.0/8) is routed through the loopback interface.

  12. Add a route using the src keyword for policy routing:

    sudo ip route add 192.168.90.0/24 via 192.168.1.1 src 192.168.1.100
    

    Specifies the source IP 192.168.1.100 for traffic to the 192.168.90.0/24 network.

  13. Add a route to a specific network with a custom protocol:

    sudo ip route add 192.168.100.0/24 via 192.168.1.1 proto static
    

    Adds a route with a custom protocol identifier (static in this case) for 192.168.100.0/24.

These examples cover various scenarios for managing routing on a Linux system, using the ip route add command with different options and configurations.

© 2024 Linux and Python Tutorials

🌱 Powered by Hugo with theme Dream.