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 Or simply, $ route
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
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
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