5

I am having trouble getting a static routing ip command to become permanent by way of the post-up hook in /etc/network/interfaces. Here are the relevant details:

/etc/network/interfaces

# The loopback network interface
auto lo
iface lo inet loopback

# Network A
allow-hotplug eth0
iface eth0 inet static
address 10.1.10.100
netmask 255.255.255.0
gateway 10.1.10.1

# Network B
allow-hotplug eth1
iface eth1 inet static
address 192.168.100.1
netmask 255.255.0.0
gateway 127.0.0.1

The relevant ip command and routing table

ip route add 192.168.50.0/24 via 10.1.10.100 dev eth0
# This command makes things happy...

$ ip route show
default via 10.40.16.1 dev eth0
10.1.10.0/24 dev eth0  proto kernel  scope link  src 10.1.10.100
192.168.0.0/16 dev eth1  proto kernel  scope link  src 192.168.100.1
192.168.50.0/24 via 10.1.10.100 dev eth0

The (not working) post-up / post-down hooks

#/etc/network/interfaces

# Network A
allow-hotplug eth0
iface eth0 inet static
address 10.1.10.100
netmask 255.255.255.0
gateway 10.1.10.1
post-up route add -net 192.168.50.0 netmask 255.255.255.0 eth0
post-down route del -net 192.168.50.0 netmask 255.255.255.0 eth0

# Network B
allow-hotplug eth1
iface eth1 inet static
address 192.168.100.1
netmask 255.255.0.0
gateway 127.0.0.1

Disclaimers

The ip route add... command does the trick, but I need this to be a permanent static route so hence the post-up stuff. I just can't seem to get the syntax right. When I run try the version of the /etc/network/interfaces on a local test network the route does not show up in the routing table after a reboot, so I think it is failing to run the post-up hook due to bad syntax.

Update

The manage for /etc/network/interfaces isn't much help on giving me a clue on the syntax for the post-up commands either here is all I found:

post-up

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. This behavior may change in the future.

1 Answer 1

7

Simply add the ip command as an up command in /etc/network/interfaces (no need to translate to route, post-up is an alias for up):

allow-hotplug eth0
iface eth0 inet static
   address 10.1.10.100
   netmask 255.255.255.0
   gateway 10.1.10.1
   up ip route add 192.168.50.0/24 via 10.1.10.100 dev eth0

No need for a down, as when the interface goes down, any routes via that interface will be removed automatically.

Aside: you're adding a route to another network via the local IP address that was just brought up? Is this system being used as a gateway?

EDIT:

The up, down, pre-up etc. labels are just that: labels to indicate what commands need to be run at that time. Those commands can be anything, e.g. send an email or whatever. No special syntax...

3
  • Excellent suggestion. Worked like a charm! Thanks for the 'EDIT' info - really helpful. As for your aside question, there is a router that is routing between two private networks ( a 10.0.0.0 and a 192.168.50.0) that was added that is screwing up the network. The network attached to interface eth1 is a physically isolated network and this device it the only point of contact with the main corporate network on eth0. When they added the new router, traffic coming from another subnet is now getting routed to the eth1 network interface :/ This is a bandaid...
    – 111---
    Commented Apr 29, 2015 at 14:21
  • For Debian GNU/Linux 11 (bullseye)" sadly this not work Commented Nov 12, 2022 at 11:02
  • The iface eth0 inet static is a must, otherwise it will fail Commented Nov 12, 2022 at 11:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .