Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have modified my routing table by deleting one rule, and adding two new rules for my second interface eth1:

route del -net 10.1.2.0 netmask 255.255.255.0
route add -host 10.1.2.51 eth1
route add -host 10.1.2.52 eth1

I want the rules to survive reboot. I could write a simple rc script to remove 1 rule and insert 2 new, but I was wondering whether there is a way to configure ifconfig, not to add a route rule for the interface (eth1) when it is started, or even better, to specify which rules should be added.

What would be a clean way to do it? Does ifconfig allow me to specify custom rules for routing?

share|improve this question
add comment

1 Answer

up vote 8 down vote accepted

You can add the calls to the post-up hook when the interface comes up. The interface configuration sits in /etc/network/interfaces. Here an example:

auto eth1
iface eth1 inet dhcp
  post-up route del -net 10.1.2.0 netmask 255.255.255.0
  post-up route add -host 10.1.2.51 eth1
  post-up route add -host 10.1.2.52 eth1
  pre-down route add -net 10.1.2.0 netmask 255.255.255.0
  pre-down route add -host 10.1.2.51 eth1
  pre-down route add -host 10.1.2.52 eth1

From the man page - interfaces:

post-up command
Run command after bringing the interface up. If this command fails then ifup aborts, refraining from marking the interface as configured (even though it has really been configured), prints an error message, and exits with status 0.

pre-down command
Run command before taking the interface down. If this command fails then ifdown aborts, marks the interface as deconfigured (even though it has not really been deconfigured), and exits with status 0.

Furthermore, on Linux the “net-tools” route is deprecated and should be replaced by the tools from the iproute2 package.

Further reading:

share|improve this answer
    
You won't be able to add routes for an interface that isn't up. These commands should be changed to post-up and pre-down. –  bahamat Jul 26 '13 at 19:29
    
could I, instead of removing the first rule, prevent it from being created in the first place? How does a route rule get created, anyway? From the network settings in /etc/network/interfaces? And how do I know, that when post-up runs, the rule-to-be-removed already exists? The man page does not say at what point in time it's created. –  Martin Vegter Jul 26 '13 at 20:40
1  
Since I neither know your network nor your host configuration, I can't tell how this route made it's way into the routing table. Does it come with a DHCP lease? If not, check your services in /etc/init.d and investigate which ones possibly could tinker with the routing table. Also have a look into /etc/network including subdirectories. –  Marco Jul 26 '13 at 20:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.