“ip route add” – Add Route In Linux

Linux provides the ip route command in order to manage the routing table. The routing table is used to set routes for different destination IP addresses. Even there is a default route in most of the cases which are used to sent IP packets to the other networks we may need to add new routes via the command-line interface by using the ip route add command.

List Current Routes/Routing Table

Before starting to add new routes current routes or routing table can be checked to prevent collussions with exitsting routes. The ip route command can be used to list current routes.

$ ip route
default via 192.168.136.2 dev ens33 proto dhcp metric 100 
169.254.0.0/16 dev ens33 scope link metric 1000 
192.168.136.0/24 dev ens33 proto kernel scope link src 192.168.136.136 metric 100 

The first line starting with the default is the default route where all unmatched IP packets are sent through this route or default gateway.

Add New Default Gateway Route

Generally, Linux distributions use helper tools that add default gateway according to the DHCP server. But in some cases, this may not work or we may need to add a new route or default gateway. First of all, in order to change a Linux system routing table, we require root privileges that can be provided with the sudo command or by elevating to the root user. In the following example, we add the “192.168.1.1” as the default route.

$ sudo ip route add default via 192.168.1.1

Add New Route Specifying Destination IP Address

A new route can be added for the specified network range by specifying gateway address. In the following example we add the “192.168.1.1” as gateway for the network “10.0.0.0/8”. This means when a IP packate with the destiantion adress with the “10.0.0.0/8” is processed the first hop is “192.168.1.1”.

$ sudo ip route add 10.0.0.0/8 via 192.168.1.1

Add New Route Specifying Destination Network Interface/Device

The ip route add command can be used with a device name as destiantion gateway. The specified network interface or network device gateway address is used automatically as the gateway. In the following example we set the eth1 network interface as destiantion device for the network “10.0.0.0/8”. If an IP packet is processed the next hop is the “eth1” network device gateway.

$ sudo ip route add 10.0.0.0/8 dev eth1
See also  How To Install Wireshark In Linux (Ubuntu, Debian, Mint, CentOS)?

Leave a Comment