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
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
We will only type the command
route to the Linux shell.
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
-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 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 a new route.
- 172.16.0.0/24 is a target network that is the 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 the same network with 192.168.122.1
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 10.0.0.0 by using the
-net option by setting 192.168.1.1 as gateway for the interface
$ sudo route add -net 10.0.0.0/8 gw 192.168.1.1 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 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
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
$ sudo route del -net 10.0.0.0/8 gw 192.168.1.1 eth0
You can use the following infographic in order to get brief information about adding new routes for Linux and Ubuntu.