How To Add New Route In Ubuntu, Linux?

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

Display/List Existing Routing Table and Information By Using ip Command

First, we should display existing routing table. Our target network should not intersect existing routing information. The routing table contains different routes to the remote networks with the same or different gateways. In general, the same default gateway is used.

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

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

Display/List Existing Routing Table and Information By Using route Command

Linux provides alternative ways and commands to list the existing routing tables and information. We can use the command route in order to list the existing routing table with information like destination, gateway, netmask, flags, metric, interface. route command is provided by the net-tools in Ubuntu which is not installed by default. We can install the net-tools package with the following command to use route command.

$ sudo apt install net-tools
Install net-tools Package For route Command

We will only type the command route to the Linux shell.

$ route
List Route Information with route Command

Display/List Existing Routing Table and Information By Using netstat Command

Alternatively, the command netstat can be also used to list the routing table and information. In order to list routing information the options -n and -r should be provided to the command netstat.

$ netstat -nr

Add New Route with ip Command

In order to add a new route, we will use the command ip route add by providing related information. We will add a new route to the network.

$ sudo ip route add via dev ens3
  • ip route add  is our command to add a new route.
  • is a target network that is the destination range.
  • via specifies the next hoop which should be directly connected to our system network.
  • dev ens3 specifies our interface which is the same network with
LEARN MORE  How To Find My Router IP Address On Windows, Linux, MacOSX, Android, iOS, iPhone, ChromeOS Operating Systems?

Add New Route By Using route Command

Alternatively, we can add a new route by using the command route. We will use the option add of the route command where also provide other parameters like destination network, gateway, and interface name. In the following example, we will add a route to the network by using the -net option by setting as gateway for the interface eth0.

$ sudo route add -net gw eth0

Check New Route

A newly added route will be activated instantly after adding the routing table. We can also list and check the newly added route information by using the command ip route show like below. We will see that the new route is added successfully.

$ ip route show 
default via dev ens3 dev lxcbr0  proto kernel  scope link  src via dev ens3 dev ens3  proto kernel  scope link  src

Delete/Remove Existing Route By Using route Command

We can delete an existing route by using the command route. It is very similar to the adding route where we will replace the add options with the del option.

$ sudo route del -net gw eth0

You can use the following infographic in order to get brief information about adding new routes for Linux and Ubuntu.

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

Leave a Comment