March 14, 2014

Adding a Static Route to the Route Table

Prerequisite

Install the kernel documentation package, if you have not.

$ yum install kernel-doc -y

The package contains several documentation and you can list them all with 'rpm -ql kernel-doc'.

Display Current Routing

$ ip route show

Enabling Kernel Routing

To enable kernel paremeter ip_forward needs to be on.

$ sysctl -a | grep ip_forward
net.ipv4.ip_forward = 1

And the corresponding documentation.

$ less /usr/share/doc/kernel-doc-2.6.32/Documentation/networking/ip-sysctl.txt
...
ip_forward - BOOLEAN
        0 - disabled (default)
        not 0 - enabled

        Forward Packets between interfaces.

        This variable is special, its change resets all configuration
        parameters to their default state (RFC1122 for hosts, RFC1812
        for routers)
...

If you are not sure how to add search the system documentation.

$ find /usr/share/doc/ -name "*" | xargs grep -i "static route"
...
/usr/share/doc/initscripts-9.03.40/sysconfig.txt:  bring up static routes that depend on that device.  Calls
/usr/share/doc/initscripts-9.03.40/sysconfig.txt:  Set up static routes for a device.
...
$ less /usr/share/doc/initscripts-9.03.40/sysconfig.txt
...
/etc/sysconfig/network-scripts/route-<interface-name>

  Contains lines that specify additional routes that should be added when the
  associated interface is brought up.

  The files are processed by the ifup-routes script and uses the /sbin/ipcalc
  utility for all network masks and numbers. Routes are specified using the
  syntax:

    ADDRESSn=<network>
    NETMASKn=<network/prefix mask>
    GATEWAYn=<next-hop router/gateway IP address>

  The "n" is expected to be consecutive positive integers starting from 0.
  For example:

    ADDRESS0=192.168.2.0
    NETMASK0=255.255.255.0
    GATEWAY0=192.168.1.1

  adds a network route to the 192.168.2.0 network via the gateway at
  192.168.1.1. Since you must already have a route to the network of the
  gateway, there is no need to specify a device.

  Note: The ifup-routes script also supports an older syntax designed to be
  used directly as an argument to "/sbin/ip route add".
  If no "ADDRESSn" lines are found the following will still
  work:
  
  192.168.2.0/24 dev ppp0
  
  adds a network route to the 192.168.2.0 network through ppp0.
...

Or you can add via CLI, but this will not be permanent.

$ ip route add network/netmask via router_ip

No comments: