In this tutorial, we will see how to add static route on CentOS 7 and RHEL 7. We will also know what is static route? And why we need to add static route? So first of all we should know what is static route?
What is static route?
Static routing is a form of routing that occurs when a router uses a manually-configured routing entry, rather than information from a dynamic routing traffic. Network administrator can manually configured static routes by adding in entries into a routing table. Static routes are fixed and do not change if the network is changed or reconfigured. Static routing and dynamic routing are not mutually exclusive.
Dynamic routing and static routing are usually used on a router to maximize routing efficiency and to provide backups in the event that dynamic routing information fails to be exchanged.
Why we use static route?
- Static routing can be used to define an exit point from a router when there is no any other routes are available or necessary. This is called a default route.
- Static routing can be used for small networks which require only one or two routes.
- Static routing is often used as a complement to dynamic routing to provide a failsafe backup in the event that a dynamic route is unavailable.
Now let’s start static route configuration. We can check our IP table with below commands like below:-
[[email protected] ~]# ip route show default via 192.168.43.1 dev enp0s3 169.254.0.0/16 dev enp0s3 scope link metric 1002 192.168.43.0/24 dev enp0s3 proto kernel scope link src 192.168.43.15 192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 [[email protected] ~]#
In the above screen shot you can see that any packets to reach a destination network ID 192.168.43.0/24 should travel via enp0s3 interface with 192.168.43.15 and any other destination network not implicitly defined should use a default gateway 192.168.43.1
Now I am going to add one static route where we can define Network as well as specify via which IP address and interface the packet should travel through in order to reach its destination.
For example, let’s add a static route to destination network 10.10.20.0/24 via 192.168.43.15 ip address and enp0s3 interface.
To do this we need to create a new file route-enp0s3 in a /etc/sysconfig/network-scripts/ directory.
We can define an above mentioned persistent static route add a following line into our route-enp0s3 file. Like below:-
[[email protected] network-scripts]# cat route-enp0s3 10.10.20.0/24 via 192.168.43.15 dev enp0s3 [[email protected] network-scripts]#
After entry into the file we need to restart network service. Like below:-
[[email protected] ~]# service network restart Restarting network (via systemctl): [ OK ] [[email protected] ~]#
we need to check our new static route has been correctly defined or not.
[[email protected] ~]# ip route show default via 192.168.43.1 dev enp0s3 10.10.20.0/24 via 192.168.43.15 dev enp0s3 169.254.0.0/16 dev enp0s3 scope link metric 1002 192.168.43.0/24 dev enp0s3 proto kernel scope link src 192.168.43.15 192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 [[email protected] ~]#
That’s all you can see we have successfully added one static network.
You can also see Network configuration after CentOS 7 minimal installation.