16.4. Static Routes and the Default Gateway
Static routes are for traffic that must not, or should not, go through the default gateway. Routing is often handled by devices on the network dedicated to routing (although any device can be configured to perform routing). Therefore, it is often not necessary to configure static routes on Red Hat Enterprise Linux servers or clients. Exceptions include traffic that must pass through an encrypted VPN tunnel or traffic that should take a specific route for reasons of cost or security. The 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.
Configuring Static Routes Using the Command Line
If static routes are required, they can be added to the routing table by means of the
ip route addcommand and removed using the
ip route delcommand. The more frequently used
ip routecommands take the following form:
ip route [ add | del | change | append | replace ] destination-addressSee the
ip-route(8)man page for more details on the options and formats.
ip routecommand without options to display the
IProuting table. For example:
~]$ ip route default via 192.168.122.1 dev eth0 proto static metric 1024 192.168.122.0/24 dev ens9 proto kernel scope link src 192.168.122.107 192.168.122.0/24 dev eth0 proto kernel scope link src 192.168.122.126
To add a static route to a host address, in other words to a single
IPaddress, issue a command as
~]# ip route add 192.0.2.1 via 10.0.0.1 [Where 192.0.2.1 is the
IPaddress of the host in dotted decimal notation, 10.0.0.1 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
IPaddress representing a range of
IPaddresses, issue the following command as
~]# ip route add 192.0.2.0/24 via 10.0.0.1 [where 192.0.2.0 is the
IPaddress 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-interfacefile. For example, static routes for the eth0 interface would be stored in the
route-interfacefile has two formats: ip command arguments and network/netmask directives. These are described below.
ip-route(8)man page for more information on the
Configuring The Default Gateway
The default gateway is determined by the network scripts which parse the
/etc/sysconfig/networkfile first and then the network interface
ifcfgfiles for interfaces that are “up”. The
ifcfgfiles are parsed in numerically ascending order, and the last GATEWAY directive to be read is used to compose a default route in the routing table.
The default route can thus be indicated by means of the GATEWAY directive and can be specified either globally or in interface-specific configuration files. Specifying the gateway globally has certain advantages in static networking environments, especially if more than one network interface is present. It can make fault finding simpler if applied consistently. There is also the GATEWAYDEV directive, which is a global option. If multiple devices specify GATEWAY, and one interface uses the GATEWAYDEV directive, that directive will take precedence. This option is not recommend as it can have unexpected consequences if an interface goes down and it can complicate fault finding.
Global default gateway configuration is stored in the
/etc/sysconfig/networkfile. This file specifies gateway and host information for all network interfaces. For more information about this file and the directives it accepts, refer to Section 32.1.22, “