Friday, December 2, 2011

Adding Persistent Static Routes in Debian

Stumbled upon this just a while ago...

Adding a static Route in Debian can be easily done by using the command

route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.2 dev eth1

Here, the network 192.168.2.0 is accessible through next hop 192.168.1.2 exit interface eth1. However, the problem is that the system forgets the route if the network service restarts. Here's how the route can be made permanent -


# The primary network interface
auto eth1
allow-hotplug eth1
iface eth1 inet static
    address 192.168.1.3
    netmask 255.255.255.0

up route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.2 dev eth1
up route add -net 192.168.10.0 netmask 255.255.255.0 gw 192.168.1.2 dev eth1

down route del -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.2 dev eth1
down route del -net 192.168.10.0 netmask 255.255.255.0 gw 192.168.1.2 dev eth1

The route is would now be updated every time the network service is restarted. Works like a charm :)

2 comments:

  1. This command don't exist's in debian 6. How to install post-up command??

    ReplyDelete
    Replies
    1. It works for me in Debain 6. However, sometimes, I need to restart the Debian box. When the systems starts up, the routes are present. However, when "networking restart" is issued, the routes may fail to show in the kernel table. Explanation is provided below for your reference.


      FROM: http://superuser.com/questions/208864/how-to-set-permanent-route-in-ubuntu-10-10

      When you comment out the stanza about eth1 in /etc/network/interfaces, NetworkManager takes over the interface. What could help is in fact the output from ifconfig and route -n when you boot with the /etc/network/interfaces in your original question and do not get any internet connection. Note that Network Manager and ifupdown (invoked when you “restart networking”) are two different components; ifupdown is the one that executes the instructions in /etc/network/interfaces, while Network Manager only reads it to prevent itself from taking over if you've set things up for ifupdown. – Gilles Nov 10 '10 at 18:23

      Delete