- Static routing
- Showing routes
- Adding a static route
- Adding a permanent static route
- See also
- External resources
- What is a Permanent or Persistent static route
- 1. Edit /etc/network/interfaces file
- 2. Create own file in /etc/network/if-up.d directory
- How to Add and Delete Static Route in Linux using IP Command
- Viewing the existing routing table
- Adding a static route using IP command
- Permanently adding static route (RHEL, Fedora, CentOS)
- Permanently adding static route (Ubuntu / Debian)
- Deleting a static route
Static routing
A route is a rule set in the kernel that is used to determine which physical network interface or gateway is needed in order to reach a particular network (or single host). There are many types of routed protocols; this article covers routing of the IP protocol in the Linux kernel.
Although IP routes are stored in the kernel, they are modifiable by userspace tools as described in the following examples.
Showing routes
Show the routing table with iproute2:
default via 192.168.1.1 dev wlan1 metric 1 192.168.50.0/24 dev lan proto kernel scope link src 192.168.50.1 127.0.0.0/8 via 127.0.0.1 dev lo 192.168.1.0/24 dev wlan1 proto kernel scope link src 192.168.1.1
Or show the routing table using sys-apps/net-tools:
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.50.0 0.0.0.0 255.255.255.0 U 0 0 0 lan 192.168.1.0 0.0.0.0 255.255.255.0 U 2000 0 0 wlan1 127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo 0.0.0.0 192.168.1.1 0.0.0.0 UG 2000 0 0 wlan1
Adding a static route
The IP address, subnet mask (CIDR), and gateway are necessary prerequisite information before adding a static route.
In this example the 10.10.10.0 network with a 255.255.255.0 subnet mask will be routed to the 192.168.1.50 gateway. CIDR style netmasks are required when adding routes using commands from the sys-apps/iproute2 package ( ip ). The following example will add the 10.10.10.0/24 route:
Show the routing table using the ip route command:
default via 192.168.1.1 dev wlan1 metric 1 10.10.10.0/24 dev wlan1 via 192.168.1.50 src 10.10.10.1 192.168.50.0/24 dev lan proto kernel scope link src 192.168.50.1 127.0.0.0/8 via 127.0.0.1 dev lo 192.168.1.0/24 dev wlan1 proto kernel scope link src 192.168.1.1
Older systems may possibly only have the netstat or route commands (via sys-apps/net-tools) instead of ip used in the above example.
Adding the same static route as described above using the route command:
Show routing table using netstat (sys-apps/net-tools):
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.10.10.0 192.168.1.50 255.255.255.0 UG 0 0 0 wlan1 192.168.50.0 0.0.0.0 255.255.255.0 U 0 0 0 lan 192.168.1.0 0.0.0.0 255.255.255.0 U 2000 0 0 wlan1 127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo 0.0.0.0 192.168.1.1 0.0.0.0 UG 2000 0 0 wlan1
The routing table is sorted from most specific routes to most general. This is how it is read by the routing process. Longest prefix match — means the the smallest network, or the network with the largest netmask, or the most specific route f.e. 255.255.255.255 is at first position in the routing table.
Adding a permanent static route
For users of the netifrc scripts (OpenRC’s standard network tools), permanent static routes can be added by opening a preferred text editor to /etc/conf.d/net and adjusting the file accordingly.
Reference the current routing table for help.
routes_wlan1="10.10.10.0/24 via 192.168.1.50 default via 192.168.1.1"With dhcpcd as network manager the static route goes into /etc/dhcpcd.conf instead.
Both statements above mean:
- IP packets destined to the 10.10.10.0/24 network are send to 192.168.1.50 .
- IP packets destined to all 0.0.0.0/0 other networks are send to 192.168.1.1 .
The default route 0.0.0.0/0 is used if:
- The host has no physical or logical IP interface in the target network segment.
- The host has to send IP packets outside of its own IP network segment, and there is no specific route found in the routing table for target IP network.
See also
- iproute2 — a tool developed to unify network interface configuration, routing, and tunneling for Linux systems.
- Network management — describes possibilities for managing the network stack.
External resources
What is a Permanent or Persistent static route
In Linux, permanent static routes also called as Persistent routes are the static route entries that will not be deleted when the network restart or when the system restart.
Typically in a Linux System, route add and ip route add commands are used to add static routes to the routing table. But those static route entries get deleted from the routing table when either network or system restart.
If you want to add a route to the network 192.168.1.0 through gateway 192.168.221.1 and print result, you can execute the following commands.
~] ip route add 192.168.1.0/24 via 192.168.221.1
And print result of previous command:
~] ip route show default via 84.244.68.1 dev ens192 onlink 84.244.68.0/24 dev ens192 proto kernel scope link src 84.244.68.206 192.168.1.0/24 via 192.168.221.1 dev ens192 192.168.221.0/24 dev ens192 proto kernel scope link src 192.168.221.206
Restart networking service
~] systemctl restart networking
After print result we can see that static rule is deleted:
~] ip route show default via 84.244.68.1 dev ens192 onlink 84.244.68.0/24 dev ens192 proto kernel scope link src 84.244.68.206 192.168.221.0/24 dev ens192 proto kernel scope link src 192.168.221.206
So how we can make static routes permanent? We have a several option how to do it.
1. Edit /etc/network/interfaces file
The first option is edit /etc/network/interfaces file.
Following is the sample Debian (Ubuntu) network interface configuration file with permanent static route entries.
# The primary network interface auto ens192 allow-hotplug ens192 iface ens192 inet static address 192.168.221.54/24 gateway 192.168.221.1 dns-nameservers 82.99.137.41 212.158.133.41 dns-search secar.cz up ip route del 192.168.0.0/24 via 192.168.221.1 dev ens192 up ip route add 192.168.0.0/24 via 192.168.221.1 dev ens192 up ip route del 192.168.1.0/24 via 192.168.221.1 dev ens192 up ip route add 192.168.1.0/24 via 192.168.221.1 dev ens192
When next hop (192.168.221.1) is in network subnet with direct attached interface, the dev [interface] in ip route command is optional.
Restart network with /etc/init.d/networking restart or with systemd restart networking command and print the result:
~] ip route show default via 192.168.221.1 dev ens192 onlink 192.168.221.0/24 dev ens192 proto kernel scope link src 192.168.221.54 192.168.0.0/24 via 192.168.221.1 dev ens192 192.168.1.0/24 via 192.168.221.1 dev ens192
2. Create own file in /etc/network/if-up.d directory
Another way to create a static network route is to create a script file in a directory /etc/network/if-up.d. For me, this is the preferred way to create static routes in debian.
Change working directory to /etc/network/if-up.d, create file my_route, change permissions to 751 with chmod 751 my_route and insert this content:
#!/bin/sh if [ "$IFACE" = "ens192" ]; then ip route add 192.168.0.0/24 via 192.168.221.1 ip route add 192.168.1.0/24 via 192.168.221.1 fi
~] ip route show default via 192.168.221.1 dev ens192 onlink 192.168.221.0/24 dev ens192 proto kernel scope link src 192.168.221.54 192.168.0.0/24 via 192.168.221.1 dev ens192 192.168.1.0/24 via 192.168.221.1 dev ens192
How to Add and Delete Static Route in Linux using 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. 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,
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.
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.
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:
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 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.
Also Read : 12 ip Command Examples for Linux Users