Thursday 10 January 2013

Route Command

Route


Theory
 route command displays routing table resides in kernel and also used to modify the routing table.The tables which specifies how packets are routed to a host is called routing table.

Route command is used to show/manipulate the IP routing table. It is primarily used to setup static routes to specific host or networks via an interface.


To dispaly the routing table:

    route -n


To check the routing table in Linux (see netstat):

netstat -rn

Typical Operations
Adding and Removing a Network in Linux

route add -net 10.10.10.0/24 gw 192.168.0.1
route del -net 10.10.10.0/24 gw 192.168.0.1

Adding and Removing a specific host is Linux-flavour specific:

route add -host 10.10.10.45 gw 192.168.0.1
route del -host 10.10.10.45 gw 192.168.0.1

Adding a Default GW in Linux

route add default gw 192.168.0.1
route del default gw 192.168.0.1

List Kernel’s Routing Cache Information

Kernel maintains the routing cache information to route the packets faster. We can list the kernel’s routing cache information by using the -C flag.

$ route -Cn

Reject Routing to a Particular Host or Network

Sometimes we may want to reject routing the packets to a particular host/network. To do that, add the following entry.
 
$ route add -host 192.168.1.51 reject
 
If you want to reject an entire network ( 192.168.1.1 – 192.168.1.255 ), then add the following entry.

 $ route add -net 192.168.1.0 netmask 255.255.255.0 reject
 
$ route add -net 192.168.1.0 netmask 255.255.255.0 dev eth0

Note: Route added using route command exists until reboot. You can add them permantly using option -p of the route command. In this case these routes are stored in /etc/inet/static_routes, but Sun doesn't guarantee it will stay there.
Making routing entries permanent

In Red Hat, Fedora, Centos, and Oracle Linux this issue is an overengineered mess. There is no system wide table to store static route information. It is stored on interface basis. For each interface you need to define and maintain  /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.

There are two formats acceptable in this file

    New and bad
    Old and horrible

We will start with the new format as it cause less allergy. It is available since Red Hat 8, I think. In this case the route-interface  file has two types of directives: one for default router and the other for network/netmask directives. Here is an example from Centos deployment guide:


The following is a sample route-eth0 file using the IP command arguments format. The default gateway is 192.168.0.1, interface eth0. The two static routes are for the 10.10.10.0/24 and 172.16.1.0/24 networks:

    default 192.168.0.1 dev eth0
    10.10.10.0/24 via 192.168.0.1 dev eth0
    172.16.1.0/24 via 192.168.0.1 dev eth0

There is also older pretty stupid format that for compatibility is also accepted. You should never used it, but it might be useful to be aware about its existence:

    You can also use the network/netmask directives format for route-interface  files. The following is a template for the network/netmask format, with instructions following afterwards:

    ADDRESS0=X.X.X.X
    NETMASK0=X.X.X.X
    GATEWAY0=X.X.X.X

    Where:

        ADDRESS0=X.X.X.X   is the network number for the static route.
        NETMASK0=X.X.X.X   is the netmask for the network number defined with ADDRESS0=X.X.X.X.
        GATEWAY0=X.X.X.X   is the default gateway, or an IP address that can be used to reach ADDRESS0=X.X.X.X

    The following is a sample route-eth0 file using the network/netmask directives format. The default gateway is 192.168.0.1, interface eth0. The two static routes are for the 10.10.10.0/24 and 172.16.1.0/24 networks. However, as mentioned before, this example is not necessary as the 10.10.10.0/24 and 172.16.1.0/24 networks would use the default gateway anyway:

    ADDRESS0=10.10.10.0
    NETMASK0=255.255.255.0
    GATEWAY0=192.168.0.1
    ADDRESS1=172.16.1.0
    NETMASK1=255.255.255.0
    GATEWAY1=192.168.0.1

    Subsequent static routes must be numbered sequentially, and must not skip any values. For example, ADDRESS0, ADDRESS1, ADDRESS2, and so on.  --[That make deletion a labor intensive operation  --NNB;-)]

    ]Below is an example of setting static routes to a different subnet, on a machine in the 192.168.0.0/24 subnet. The example machine has an eth0 interface in the 192.168.0.0/24 subnet, and an eth1 interface (10.10.10.1) in the 10.10.10.0/24 subnet:

    ADDRESS0=10.10.10.0
    NETMASK0=255.255.255.0
    GATEWAY0=10.10.10.1

What's really funny is that this horrible way of specifying static routes was essentially a change from Suse-style that was used in Red Hat 7. Yes, Red Hat 7 was "normal"  way to define static routes using  /etc/sysconfig/static-routes table (Static Routes in Red Hat 8.0):

    As of Red Hat 8.0, Red Hat has changed the way in which non-default static routes are initialized and added to the routing table on startup. Since this process is not documented, I've made a few notes here.

    Traditionally, static routes were added in /etc/sysconfig/static-routes, in the form:

    iface type dest-addr netmask netmask gw gateway-addr ...

    such as this example, taken from a real system:

    eth0 net 192.168.170.0 netmask 255.255.255.0 gw 192.168.168.1

    This would cause the startup scripts to execute a command like this

    route add -type dest-addr netmask netmask gw gateway-addr ... iface

    Notice the ellipsis at the end of the line there - this means that other options for the route add command can be specified in static-routes, which is particularly useful for specifying metrics - something that is quite common in moderately complex intranets. Other options, such as maximum segment size, initial window size and initial round-trip time, may also be useful.

    In Red Hat 8.0, attempts to add interface-specific routes in static-routes will fail. Instead, static routes must be specified as multiple variables in multiple files in /etc/sysconfig/networking/devices. For example, a static route for the eth0 device must be specified in a file called eth0.route, like this:

    ADDRESS0=192.168.170.0
    NETMASK0=255.255.255.0
    GATEWAY0=192.168.168.1

    No other variables are supported, although additional routes can be specified as ADDRESS1, NETMASK1, etc. Clearly, this means that metrics and other parameters cannot be set at this point.

No comments:

Post a Comment