I have a Linux box with 8 network interfaces:

  • 3 interfaces are connected to 3 different ISPs, one interface is the default gw interface.

  • Use NAT for outgoing packages.

  • Internally, I use the dynamic routing protocol.

When I receive external traffic from a specific interface, I want the outgoing packet to follow and exit the correct interface.

How can I accomplish that?

  • I would like any traffic entering from an external interface to go to the destination from the same interface regardless of what is indicated in the routing table. Jan 7 '20 at 11:56

You can use iptables to do so. The specific command will of course vary on exactly what you want to do, but this answers the broad question as you have it!


You can accomplish that using iptables and iproute2. But in a case such as yours, with that amount of network interfaces and OSPF, I would rather invest some money in a network router or gateway appliance for the sake of manageability on the long run.

Here's an article that shows an example of how doing so, although it doesn't contemplate OSPF:


(It is in Portuguese, but focus on the script)

Basically, you should mark packets when they enter your network in iptables, and route them using that mark using iproute2.

Best regards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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