Search This Blog

Thursday, November 22, 2018

Configuring Static Routes Using the Command Line

Static routes can be added to the routing table by means of the ip route add command and removed using the ip route del command. 

The ip route commands take the following form:

ip route [ add | del | change | append | replace ] destination-address
See the ip-route(8) man page for more details on the options and formats.
Use the ip route command without options to display the IP routing table. For example:
~]$ ip route
default via dev eth0  proto static  metric 1024 dev ens9  proto kernel  scope link  src dev eth0  proto kernel  scope link  src
To add a static route to a host address, in other words to a single IP address:
~]# ip route add via [dev ifname]
Where is the IP address of the host in dotted decimal notation, is the next hop address and ifname is the exit interface leading to the next hop.
To add a static route to a network, in other words to an IP address representing a range of IP addresses:
~]# ip route add via [dev ifname]
where is the IP address of the destination network in dotted decimal notation and /24 is the network prefix. The network prefix is the number of enabled bits in the subnet mask. This format of network address slash network prefix length is sometimes referred to as classless inter-domain routing (CIDR) notation.
Static route configuration can be stored per-interface in a /etc/sysconfig/network-scripts/route-interface file. For example, static routes for the eth0 interface would be stored in the /etc/sysconfig/network-scripts/route-eth0 file.

All the above route will be lost after a system restart. To add permanent Static route, 

Add Persistence Static Routes