Adding Persistent Static Routes in Debian 7 wheezy

Posted by Md. Mahidul Hasan on 6:21 AM with No comments
Basic Static Routing Part #2 (static route)
Adding Persistent Static Routes in Debian 7 wheezy

My network diagram:



First we have to enable port forwarding at Server#1. 
root@server1:~# apt-get install iproute

root@server1:~# vim /etc/sysctl.conf
net.ipv4.ip_forward=1

root@server1:~# sysctl -p
net.ipv4.ip_forward=1

Now, we will add ip address and a static route.  
root@server1:~# vim /etc/network/interfaces
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth4
allow-hotplug eth4
iface eth4 inet static
        address 202.94.76.71
        netmask 255.255.252.0
#      gateway 202.94.76.65

auto eth0
allow-hotplug eth0
iface eth0 inet static
        address 172.16.0.254
        netmask 255.255.0.0

auto eth1
allow-hotplug eth1
iface eth1 inet static
        address 192.168.1.1
        netmask 255.255.255.0

### add default gateway
up route add default gw 202.94.76.65

### ip route add (remote_network) via (remote_connecting_gatway) dev (local_outgoing_interface)
up ip route add 192.168.55.0/24 via 192.168.1.254  dev eth1
down ip route del 192.168.55.0/24 via 192.168.1.254  dev eth1

root@server1:~# /etc/init.d/networking restart
Running /etc/init.d/networking restart is deprecated because it may not enable again some interfaces ... (warning).
Reconfiguring network interfaces...RTNETLINK answers: No such process
done.

We will do the same thing in Server2.
root@server2:~# apt-get install iproute
root@server2:~# vim /etc/sysctl.confnet.ipv4.ip_forward=1

root@server2:~# sysctl -p
net.ipv4.ip_forward=1

root@server2:~# vim /etc/network/interface
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth2
allow-hotplug eth2
iface eth2 inet static
        address 192.168.1.254
        netmask 255.255.255.0

auto eth3
allow-hotplug eth3
iface eth3 inet static
        address 192.168.55.1
        netmask 255.255.255.0

### ip route add (remote_network) via (remote_connecting_gatway) dev (local_outgoing_interface)
up ip route add 172.16.0.0/16 via 192.168.1.1  dev eth2
down ip route del 172.16.0.0/16 via 192.168.1.1  dev eth2

root@server2:~# /etc/init.d/networking restart
Running /etc/init.d/networking restart is deprecated because it may not enable again some interfaces ... (warning).
Reconfiguring network interfaces...RTNETLINK answers: No such process
done.


Please note that in Server#1 we didn't add any route for 172.16.0.0/16 and 192.168.1.0/24 cause those networks are directly connected. That's why we don't need to add routes, just enabled port forwarding. We have done the same thing with Server#2 too. Another point is in server#2 we didn't use default gateway, if you want to use a gateway then add a default gate way like the below way-
up route add default gw  aaa.bbb.ccc.ddd

Now Lets see current routing table, run:

root@server2:~# route -n
Kernel IP routing table
Destination     Gateway                  Genmask        Flags Metric Ref    Use Iface
172.16.0.0     192.168.1.254        255.255.0.0   UG    0      0        0 eth2
192.168.55.0     0.0.0.0         255.255.255.0   U     0      0        0 eth3
192.168.1.0       0.0.0.0     255.255.255.0   U     0      0        0 eth3


Output Flags:
Possible flags include
    U - route is up
    H - target is a host
    G - use gateway
    R - reinstate route for dynamic routing
    D - dynamically installed by daemon or redirect
    M - modified from routing daemon or redirect
    A - installed by addrconf
    C - cache entry
    ! - reject route

Now 172.16.0.0/16 network can reach to the 192.168.55.0/24 network as well as 192.168.55.0/24 also can reach 172.16.0.0/24

Note: one last thing, here we have add permanent route at /etc/network/interface but if we like to add a temp route (which will disappear after a reboot) we had to do this in the command line,
root@server2:~# ip route add 192.168.55.0/24 via 192.168.1.254  dev eth1

Hope that helps. :-D