How to Add and Delete Static Route in Linux with IP Command

In this post, we will show you how add and delete static route in Linux system with IP command.

Part of the skill set for any Linux user, and particularly a systems administrator, is the ability to perform some network tweaks on a Linux system. This includes adding and deleting routes to enable the system to communicate with other systems o a local network.

View 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
Or
$ ip route list

ip-route-show-command-output-linux

Similar statistics can be displayed using route command,

$ route -n

route-n-command-output-linux

Or simply,

$ route

route-command-output-linux

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

netstat-nr-command-output-linux

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

Add Static Route in Linux with 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 10.0.2.0/24. 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 192.168.43.223 via the interface enp0s3.

The command for this will be

$ sudo ip route add 10.0.2.0/24 via 192.168.43.223 dev enp0s3

Add Static Route in Linux with ip command

Where:

  • 10.0.2.0      ->             is the network you want to connect to
  • /24              ->             is the subnet mask
  • 192.168.43.223 ->    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

ip-route-command-output-linux

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

$ sudo ip route add 10.0.2.15 via 192.168.43.223 dev enp0s3

ip-route-add-single-ip-linux

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

$ ip route show

ip-route-show-linux-command

OR

$ route -n

route-n-linux-command-ouput

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:

10.0.2.0/32 via 192.168.43.1
10.0.2.15  via 192.168.43.1

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 10.0.2.0 netmask 255.255.255.0 gw 192.168.43.1 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 Static Route in Linux

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 10.0.2.0/24 via 192.168.43.223 dev enp0s3

Delete Static Route with ip command

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

$ sudo ip route del 10.0.2.15 via 192.168.43.223 dev enp0s3

ip-route-del-ip-linux-command

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 192.168.43.1 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.

Also Read : 16 IP Command Examples in Linux

Leave a Comment

Your email address will not be published. Required fields are marked *