The entire purpose of networking is to get traffic from point A to point B. When a computer requests information from another, packets are routed to the destination and then back. Sometimes, computers need a little guidance on how to get packets to the destination. This is known as routing. To assist with this, nodes utilize the concept of a routing table to help decide where packets should be sent given specific destinations.
Let us see how to configure a static route in CentOS/RHEL 6,7 using 2 methods:
1. Using NetworkManager
2. Using /etc/sysconfig/network-scripts configuration files
CentOS/RHEL 7 with NetworkManager
1. Use nmcli, nmtui, or the GUI tools to modify connection routes.
2. To add a route with nmcli, modify the connection’s ipv4.routes property. For example, to add a route to the 10.20.30.0/24 network via 192.168.100.10 for a connection named “external”:
# nmcli connection modify external ipv4.routes "10.20.30.0/24 192.168.100.10"
3. To add additional routes, use the +(plus) modifier on the connection’s ipv4.routes property:
# nmcli connection modify external +ipv4.routes "10.0.1.0/24 192.168.100.20"
4. Multiple routes can be added at once, separated with a comma:
# nmcli connection modify external ipv4.routes "10.20.30.0/24 192.168.100.10, 10.0.1.0/24 192.168.100.20"
Removing routes with nmcli
1. To remove a route, use the -(minus) modifier on the connection’s ipv4.routes property, specifying the route to be removed:
# nmcli connection modify external -ipv4.routes "10.0.1.0/24 192.168.100.20"
2. To remove all routes, set the ipv4.routes property value to “”(empty):
# nmcli connection modify external ipv4.routes ""
3. After any of the modifications above, bring the connection up to put the changes into place. For example, after having modified the properties of a connection named “external”:
# nmcli connection up external
CentOS/RHEL 6,7 without NetworkManager
1. To add static routes, create or modify a route-[interface] file in the /etc/sysconfig/network-scripts/ directory where [interface] is the name of the interface the routes are related to. Each route should appear on a single line and take the basic form [network/prefix] via [gateway] where [network/prefix] is the remote network’s address with prefix and [gateway] is the IP address of the next hop. For example, to add a route to the 10.20.30.0/24 network via 192.168.100.10 and a route to the 10.0.1.0/24 network via 192.168.10.20 to both be active when eth0 is up:
# cat /etc/sysconfig/network-scripts/route-eth0 10.20.30.0/24 via 192.168.100.10 10.0.1.0/24 via 192.168.10.20
2. The interface must be brought up again for changes to take effect:
# ifup eth0
3. All versions of RHEL not using NetworkManager also recognize an older syntax for route-[interface] files:
ADDRESS[N]=X.X.X.X NETMASK[N]=Y.Y.Y.Y GATEWAY[N]=Z.Z.Z.Z
ADDRESS0=10.10.10.0 NETMASK0=255.255.255.0 GATEWAY0=192.168.1.2 ADDRESS1=220.127.116.11 NETMASK1=255.255.255.0 GATEWAY1=192.168.1.2
This format deals with three fields: GATEWAY, NETMASK, and ADDRESS. Each field should have a number appended to it indicating which route it relates to.
In the above example, Z.Z.Z.Z is the gateway IP address. Subsequent entries must be numbered sequentially (for example ADDRESS1=, NETMASK1=, GATEWAY1=). Note, that multiple entries must be sequentially numbered and must not skip a value (0 must be followed by 1, not a number greater than 1).