In this guide of Different ways of Configuring Static routes in Linux, we’re going to learn on different ways to configure static routes on a Linux system, be it Ubuntu, Kali Linux, CentOS, Fedora, Linux Mint or any other Linux system. If you have a new installation of Linux System, I recommend you read any of below articles depending on the Linux distribution you are running:
A static route can be defined as a pre-determined path that network information must follow to reach a specific host or network. The static route is normally important for traffic that must pass through an encrypted VPN tunnel or traffic that should take a specific route for reasons of cost or security.
Mostly default gateway is for any and all traffic which is not destined for the local network and for which no preferred route is specified in the routing table. The default gateway is traditionally a dedicated network router.
You can configure static route by various methods, one of them being manually specifying the route on network configuration script.Other methods include using commands such as:
# route add # ip route
A point to note on the use of both “route add” and “ip route” commands is that they configure routing on the runtime and doesn’t persist the configuration after a reboot. We are going to talk about different ways to set static route.Both temporarily and setting up the permanent static route. Adding static route with route add command. The syntax is:
route add -net <IP>/<MASK> <GW> dev <ethX>
Let’s add a route for interface eth1, network 192.168.1.0/24.
# route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.1.1 dev eth1
Adding static route with ip route command. The syntax:
ip route add <IP>/<MASK> via <GW> dev <ethX>
# ip route add 192.168.0.0/24 via 192.168.1.1 dev eth1
Adding Static persistent route
Ubuntu/Debian based Systems:
auto eth1 iface eth1 inet static address 192.168.1.10 netmask 255.255.255.0 up route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1 up route add -net 172.16.0.0 netmask 255.255.0.0 gw 192.168.1.1
auto eth1 iface eth1 inet static address 192.168.1.10 netmask 255.255.255.0 up ip route add 172.16.0.0/24 via 192.168.1.1 || true
To get to network 192.168.0.0/24 and 172.16.0.0/16, use default gw 192.168.1.1.
Static route configuration can be stored per-interface in a /etc/sysconfig/network-scripts/route-interface file. For example, static routes for the eth1 interface would be stored in the /etc/sysconfig/network-scripts/route-eth1
# vim /etc/sysconfig/network-scripts/route-eth1
GATEWAY0=192.168.1.1 NETMASK0=255.255.255.0 ADDRESS0=192.168.0.0 GATEWAY1=192.168.1.1 NETMASK1= 255.255.0.0 ADDRESS1=172.16.0.0
Save and close the file.
service network restart
You can also use:
ifdown eth1;ifup eth1
NOTE: Subsequent static routes must be numbered sequentially, and must not skip any values. For example, ADDRESS0, ADDRESS1, ADDRESS2, and so on.
Checking static routes:
ip route show netstat -nr route -n
We have concluded Different ways of Configuring Static routes on Linux. Let me know if you encounter any issues and I’ll be happy to assist.