11.7 Configuring Network Routing

A system uses its routing table to determine which network interface to use when sending packets to remote systems. If a system has only a single interface, it is sufficient to configure the IP address of a gateway system on the local network that routes packets to other networks.

To create a default route for IPv4 network packets, include an entry for GATEWAY in the /etc/sysconfig/network file. For example, the following entry configures the IP address of the gateway system:

GATEWAY=192.0.2.1

If your system has more than one network interface, you can specify which interface should be used:

GATEWAY=192.0.2.1
GATEWAYDEV=eth0

A single statement is usually sufficient to define the gateway for IPv6 packets, for example:

IPV6_DEFAULTGW="2001:db8:1e10:115b::2%eth0"

Any changes that you make to /etc/sysconfig/network do not take effect until you restart the network service:

# service network restart

To display the routing table, use the ip route show command, for example:

# ip route show
10.0.2.0/24 dev eth0  proto kernel  scope link  src 10.0.2.15 
default via 10.0.2.2 dev eth0  proto static 

This example shows that packets destined for the local network (10.0.2.0/24) do not use the gateway. The default entry means that any packets destined for addresses outside the local network are routed via the gateway 10.0.2.2.

Note

You might be used to using the route command to configure routing. However, route is considered obsolete and will eventually be replaced altogether by the ip command.

You can also use the netstat -rn command to display this information:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
10.0.2.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0
0.0.0.0         10.0.2.2        0.0.0.0         UG        0 0          0 eth0

To add or delete a route from the table, use the ip route add or ip route del commands. For example, to replace the entry for the static default route:

# ip route del default
# ip route show
10.0.2.0/24 dev eth0  proto kernel  scope link  src 10.0.2.15 
# ip ro add default via 10.0.2.1 dev eth0 proto static
# ip route show
10.0.2.0/24 dev eth0  proto kernel  scope link  src 10.0.2.15  
default via 10.0.2.1 dev eth0  proto static 

To add a route to the network 10.0.3.0/24 via 10.0.3.1 over interface eth1, and then delete that route:

# ip route add 10.0.4.0/24 via 10.0.2.1 dev eth1
# ip route show
10.0.2.0/24 dev eth0  proto kernel  scope link  src 10.0.2.15 
10.0.3.0/24 via 10.0.3.1 dev eth1
default via 10.0.2.2 dev eth0  proto static 
# ip route del 10.0.3.0/24
# ip route show
10.0.2.0/24 dev eth0  proto kernel  scope link  src 10.0.2.15 
default via 10.0.2.2 dev eth0  proto static 

The ip route get command is a useful feature that allows you to query the route on which the system will send packets to reach a specified IP address, for example:

# ip route get 23.6.118.140
23.6.118.140 via 10.0.2.2 dev eth0  src 10.0.2.15 
    cache  mtu 1500 advmss 1460 hoplimit 64

In this example, packets to 23.6.118.140 are sent out of the eth0 interface via the gateway 10.0.2.2.

Any changes that you make to the routing table using ip route do not persist across system reboots. To permanently configure static routes, you can configure them by creating a route-interface file in/etc/sysconfig/network-scripts for the interface. For example, you would configure a static route for the eth0 interface in a file named route-eth0. An entry in these files can take the same format as the arguments to the ip route add command.

For example, to define a default gateway entry for eth0, create an entry such as the following in route-eth0:

default via 10.0.2.1 dev eth0

The following entry in route-eth1 would define a route to 10.0.3.0/24 via 10.0.3.1 over eth1:

10.0.3.0/24 via 10.0.3.1 dev eth1

Any changes that you make to a route-interface file do not take effect until you restart either the network service or the interface.

For more information, see the ip(8) and netstat(8) manual pages.