Thursday, February 7, 2008

Add Persistent Route to Linux

ip route add 172.16.0.0/24 via 192.168.0.3
ip route add 192.168.5.0/24 via 192.168.0.3

Explanation:

ip route is the command you are invoking. You are telling the command to add a route for network 172.16.0.0/24 (this is equivalent to having 24 bits for network id, i.e. netmask = 255.255.255.0 A single 255 represent 8 bits.) via the gateway address 192.168.0.3. Similarly for the second route.

To make these persistent. Simply execute the following line on you favorite shell (konsole, bash, etc.)

echo "ip route add 172.16.0.0/24 via 192.168.0.3" >> /etc/rc.local
echo "ip route add 192.168.5.0/24 via 192.168.0.3" >> /etc/rc.local

THis is a very good method of appending lines to the /etc/rc.local file in linux. This file is like a autoexec.bat file and is executed every time you reboot. So next time you reboot, these two lines (i.e. the text inside the quotes.) will be present in this file and thus will be executed and you will again have your routes setup.
---------------------------------------------------------------------------
---------------------------------------------------------------------------
If u want to assign route for NIC 1
 
route add -net 172.16.0.0 netmask 255.255.255.0 gw 192.168.0.3 dev eth0
route add -net 192.168.5.0 netmask 255.255.255.0 gw 192.168.0.3 dev eth0

if u restart the system the route may not exist.....

so edit this file /etc/rc.local file and add your static routes using the route statement. I mean add the above lines.
 

No comments: