Add & Delete Route on CentOS 7

route in centos7

Viewing the existing routing table

Before we embark on adding or deleting routes, it’s prudent to check the existing default routes on a system. To do so, simply launch your terminal and issue the command:

ip route show 


ip route list

Similar statistics can be displayed using route command,

 route -n

Also, you can use the good old netstat command, which is usually used for printing interface statistics as well as the routing table to achieve the same result.

sudo netstat -nr

With the default routing statistics in mind, let’s now move a step further and add some routes to our system.

Adding a static route using IP command

Suppose you want to take a backup of a Linux machine and push the backup file to another backup server in the subnet However, for one reason or the other, you cannot reach the backup server  via the default gateway. In this case, you will have to create a new route for backup server subnet via another IP, say via the interface enp0s3.

The command for this will be

sudo ip route add via dev enp0s3
  •      ->             is the network you want to connect to
  • /24              ->             is the subnet mask
  • ->    is the IP through which we will reach the server
  • enp0s3       ->            is the network interface

You can confirm whether new static route add been in route table using “ip route show” command.

sudo ip route show 

To add the specific IP of the backup server, say run the command:

sudo ip route add via dev enp0s3

Once again, you can check the routing changes to see if the changes exist using the ip route show command:

ip route show
route -n

Permanently adding static route (RHEL, Fedora, CentOS)

The routes we have just added are temporary and will not survive a reboot. To make the routes persistent, you need to manually add them.

In the /etc/sysconfig/network-scripts/directory, create an interface file route-interface where the interface attribute is your network interface name. In our case, this will be route-enp0s3.

vim /etc/sysconfig/network-scripts/route-enps03

Next, we will add  the routes as shown: via via

Save the file and exit. Then restart NetworkManager Service

sudo systemctl restart NetworkManager

Permanently adding static route (Ubuntu / Debian)

For Debian distributions, edit the file /etc/network/interfaces

$ sudo vim /etc/network/interfaces

Append the following line:

up route add -net netmask gw dev enp0s3

Save and exit the file. Finally, for the changes to come into effect, run below commands

$ sudo ifdown enp0s3 && sudo ifup enp0s3

Deleting a static route

To delete a specific route, use the ip route del command.

For example, to remove the route address we just added, run the command:

$ sudo ip route del via dev enp0s3

To delete a single IP route in a subnet run the command

$ sudo ip route del via dev enp0s3

To delete default route run:

$ sudo ip route del default
To add a default route run below ‘ip route add’ command,
$ sudo ip route add default via <ip-address> dev interface
For example:
$ sudo ip route add default via dev eth0

We hope that this tutorial was informative and provided you with insights into how you can go about adding and deleting static route in Linux.

Tags : centosnetworkroute

Leave a Response

We would like to show you notifications for the latest news and updates.
Allow Notifications