iptables, use alternate connection based on source ip or port

Posted on 2011-10-26
Last Modified: 2013-11-16
Hi All,
I have a linux firewall setup with 4 NIC's.

eth0 - (LAN)
eth1 - (Ethernet wireless corporate connection)
eth2 - (ADSL 2+) (ppp1)
eth3 - 333.333.333.333 (ADSL 2+) (ppp0)

Currently all traffic goes out through eth1, I have port forwards and other rules for inbound connections and they are working OK. But I'm trying to get all traffic from a couple of source IP's to use eth3 and be able to send all FTP (port 21) traffic out eth2.

I've tried using
iptables -t NAT -A POSTROUTING -s -d -p tcp -o eth3 -j MASQUERADE

I also tried quite a few other combinations, can't seem to get it working. I've tried using good old Google to find an answer, but can't.

Is it possible to do this with iptables?
If so, what should I be using?
If Not, what other solution is available?

Question by:BurnOut9
    LVL 16

    Accepted Solution

    This is possible with advanced routing and also in combination with iptables.

    In iptables you can use the "mangle" table to MARK packets and then use this marking to decide which routing table to use (and hence the default gateway to use).

    iptables -A PREROUTING -t mangle -i eth0 -p tcp --dport 21 -j MARK --set-mark 1

    echo 200 FTPTRAFFIC >> /etc/iproute2/rt_tables
    ip rule add from all fwmark 1 table FTPTRAFFIC
    ip route add default via dev eth2 table FTPTRAFFIC
    ip route flush cache

    LVL 67

    Expert Comment

    This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Wikipedia defines 'Script Kiddies' in this informal way: "In hacker culture, a script kiddie, occasionally script bunny, skiddie, script kitty, script-running juvenile (SRJ), or similar, is a derogatory term used to describe those who use scripts or…
    This article offers some helpful and general tips for safe browsing and online shopping. It offers simple and manageable procedures that help to ensure the safety of one's personal information and the security of any devices.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now