How To Add New Route In Ubuntu, Linux?

I have a box with Ubuntu Linux and I want to add new route to my box. Because I want to access external network from a different interface and network. How can I add new route to my Ubuntu, Debian, Fedora, Cent-OS Linux box? Because ip-tools is the same for all of this Linux distributions.

Display Existing Routing Table

First we should display existing routing table. Our target network should not intersect existing routing information.

$ ip route show
Display Existing Routing Table
Display Existing Routing Table

We can see that there is a default route which is listed in the first line. It also named as default . The default gateway IP address is 192.168.142.2

Add New Route

In order to add new route we will  use ip route add command by providing related information. We will add new route to the 172.16.0.0/16 network.

$ sudo ip route add 172.16.0.0/24 via 192.168.122.1 dev ens3
  • ip route add  is our command to add new route
  • 172.16.0.0/24 is target network which is destination range
  • via 192.168.122.1 specifies the next hoop which should be directly connected to our system network.
  • dev ens3 specifies our interface which is same network with 192.168.122.1

Check New Route

This is a route action but we should check the status of the route

$ ip route show 
default via 192.168.122.1 dev ens3  
10.0.3.0/24 dev lxcbr0  proto kernel  scope link  src 10.0.3.1  
172.16.0.0/24 via 192.168.122.1 dev ens3  
192.168.122.0/24 dev ens3  proto kernel  scope link  src 192.168.122.211

 

How To Add New Route In Ubuntu, Linux? Infografic

How To Add New Route In Ubuntu, Linux? Infografic
How To Add New Route In Ubuntu, Linux? Infografic

Leave a Comment