Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

route del -net netmask
route add -host eth1
route add -host 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
up vote 15 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 netmask
  post-up route add -host eth1
  post-up route add -host eth1
  pre-down route add -net netmask
  pre-down route add -host eth1
  pre-down route add -host 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
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
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
post-up and pre-down are aliases for up and down, respectively. And adding a || : at the end of a line allows the command to fail and not disturb ifup/ifdown. I usually add this to route del, in case the route does not exist yet and hence cannot be removed. – Tino Feb 22 '15 at 19:02

Your Answer


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.