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. In this guide, we explore exactly how you can go about adding and deleting routes on a Linux system.
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 Or $ 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 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
- 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
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
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:
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 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 10.0.2.0/24 via 192.168.43.223 dev enp0s3
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
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
$ 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.