In “Different Methods of Configuring Static Routes in Linux” in this guide, we will learn different methods of configuring static routes on Linux systems, whether it be Ubuntu, Kali Linux, CentOS, Fedora, Linux Mint or any other Linux system . If you have a newly installed Linux system, it is recommended that you read any of the following articles based on the Linux distribution you are running:
Main things to do after a fresh installation of CentOS 7 Minimum
Things to do after a fresh install of Fedora 23
What to do after installing Ubuntu 14.04, 12.04, 13.0
A static route can be defined as a predetermined path that network information must follow to reach a particular host or network. Static routing is often important for traffic that must pass through an encrypted VPN tunnel or traffic that should be routed for cost or security reasons.
In most cases, the default gateway is used for all non-local networks and the preferred route is not specified in the routing table. Traditionally, the default gateway is a private network router.
You can configure static routes in several ways, one of which is to manually specify the route in the network configuration script. Other methods include using the following command:
# route add # ip route
One thing to keep in mind when using the “route add” and “ip route” commands is that they configure routing at runtime and do not retain the configuration after a restart. We will discuss different methods of setting up static routes. Both methods are temporary and set up permanent static routes. Use the route add command to add a static route. The syntax is:
route add -net
Let’s add routes to the interface eth1,The internet 192.168.1.0/24.
# route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.1.1 dev eth1
Use the ip route command to add a static route. grammar:
ip route add
/ via dev
# ip route add 192.168.0.0/24 via 192.168.1.1 dev eth1
Adding static persistent routes
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 access the networks 192.168.0.0/24 and 172.16.0.0/16, use the default gw 192.168.1.1.
Static routing configuration can be stored per interface in / etc / sysconfig / network-scripts / route-interface file. For example, the static route for the eth1 interface will be stored in / 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.
Restart the network:
service network restart
You can also use:
ifdown eth1;ifup eth1
Note: Subsequent static routes must be numbered sequentially and no value can be skipped. For example, ADDRESS0, ADDRESS1, ADDRESS2, and so on.
Check static routes:
ip route show netstat -nr route -n
We have summarized the different ways to configure static routing on Linux. Please let me know if you run into any issues and we will be happy to assist you.