ip route add Tutorial for Linux
Thursday, Aug 8, 2024 | 5 minutes read | Update at Thursday, Aug 8, 2024
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.
-
Add a default gateway: The most popular usage with the
ip route addsetting the default network. With this command we will set gateway for all destinations those do not defined explicitly in the route table.defaultis used for default andvia 192.168.1.1for the gateway.sudo ip route add default via 192.168.1.1Sets the default gateway to
192.168.1.1. -
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.1for the destination network192.168.10.0/24sudo ip route add 192.168.10.0/24 via 192.168.1.1Routes traffic destined for the
192.168.10.0/24network through the gateway192.168.1.1. -
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.10and gateway is192.168.1.1sudo ip route add 192.168.10.10 via 192.168.1.1Routes traffic to the host
192.168.10.10via the gateway192.168.1.1. -
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 usedevinstead of theviaas the eth0 is a device.sudo ip route add 192.168.20.0/24 dev eth0Routes traffic to the
192.168.20.0/24network through theeth0interface. -
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
metricoption 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 100Adds a route to the
192.168.30.0/24network with a metric of100. -
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
srcoption 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.
-
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
blackholeoption as the destination like below.sudo ip route add 192.168.40.0/24 blackholeDiscards traffic destined for the
192.168.40.0/24network. -
Add a route with a prohibit (block traffic): We can block network traffic for a destination network with the
ip route addcommand. We should addprohibitoption as the gateway.sudo ip route add 192.168.50.0/24 prohibitBlocks traffic to the
192.168.50.0/24network. -
Add a route with a reject (return ICMP unreachable): We can simply reject for a destination network by adding
rejectas 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 rejectReturns an ICMP unreachable message for traffic to the
192.168.60.0/24network. -
Add a route using a gateway for IPv6: Until now we examined the ip route add command for the most popular IP version named
IPv4which is defacto. The ip route add command also supportsIPv6where we should useip -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::1Routes IPv6 traffic to the
2001:db8::/32network through the gateway2001:db8::1. -
Add a multicast route: We can also add multicast IP addresses for route table.
sudo ip route add 224.0.0.0/4 dev eth0Routes multicast traffic (224.0.0.0/4) through the
eth0interface. -
Add a route to a network using a link-local address:
sudo ip route add 10.0.0.0/8 via fe80::1 dev eth0Routes traffic to
10.0.0.0/8network using a link-local IPv6 address. -
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.1Directs traffic from
192.168.1.100to use a specific routing table. -
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.1Routes traffic to
192.168.70.0/24using both192.168.1.1and192.168.2.1gateways for load balancing. -
Add a route with a specific MTU size:
sudo ip route add 192.168.80.0/24 via 192.168.1.1 mtu 1400Sets the MTU to
1400for traffic to the192.168.80.0/24network. -
Add a route to a VPN network via a specific gateway:
sudo ip route add 10.8.0.0/24 via 192.168.1.1Routes traffic to a VPN network (
10.8.0.0/24) through the gateway192.168.1.1. -
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 loEnsures local traffic (127.0.0.0/8) is routed through the loopback interface.
-
Add a route using the
srckeyword for policy routing:sudo ip route add 192.168.90.0/24 via 192.168.1.1 src 192.168.1.100Specifies the source IP
192.168.1.100for traffic to the192.168.90.0/24network. -
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 staticAdds a route with a custom protocol identifier (
staticin this case) for192.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.