Saturday, June 26, 2010

Static and Dynamic Routes in RedHat Linux

Static Routes redhatlogo

To add a persistent static route in Redhat Enterprise Linux, simply create a file called “route-<interface><x>” in the /etc/sysconfig/network-scripts/ directory.

Where <interface> is the interface and <x> is the interface number. As you would expect, these are specified in separate file for each of the available interface.

For Example:
/etc/sysconfig/network-scripts/route-eth0
/etc/sysconfig/network-scripts/route-eth1

Every entry or a route has three entities as follows:

GATEWAY<n>=xxx.xxx.xxx.xxx
NETMASK<n>=yyy.yyy.yyy.yyy
ADDRESS<N>=zzz.zzz.zzz.zzz

As the names implies, they are the Gateway IP, Subnet Mask(NETMASK) and the IP Network Address for each persistent route.

Note the <n> next to each of the three entities. This number defines the route entry number and should be the same on all the entities.

Example:
GATEWAY0=192.168.1.1
NETMASK0=255.255.255.0
ADDRESS0=10.10.10.0

GATEWAY1=192.168.1.1
NETMASK1=255.255.255.0
ADDRESS1=20.20.20.2

Note: A sample file /etc/sysconfig/static-routes is available for your reference.

Once the file is created, restart the network service as follows:

# service network restart

To view the routes type

# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     *               255.255.255.0   U     0      0        0 eth0
192.168.2.0     *               255.255.255.0   U     0      0        0 eth1
10.10.10.0      *               255.255.255.0   U     0      0        0 eth0
20.20.20.2      *               255.255.255.0   U     0      0        0 eth0
169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
default         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

OR

# ip route show
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.1
192.168.2.0/24 dev eth1  proto kernel  scope link  src 192.168.2.1
10.10.10.0/24 via 192.168.1.1 dev eth0
20.20.20.2/24 via 192.168.1.1 dev eth0
default via 192.168.1.1 dev eth0


Dynamic Routes

To dynamically add a route, try the following:

Syntax:
# ip route add <Net/IP>/<Mask> via <Gateway> dev <Interface><x>

Example:
# ip route add 10.10.10.0/24 via 192.168.1.1 dev eth0

OR

Syntax:
# route add [-net|-host] <IP/Net> netmask <Mask> gw <Gateway IP> dev <Interface><x>

Example:
# route add -net 10.10.10.0 netmask 255.255.255.0 gw 192.168.1.1 dev eth0

Reference:
It’s Your IP: HowTo: Add a Persistent Static Route in Redhat Enterprise Linux -
Link

No comments:

Post a Comment