Iptables output redirect quest

Posted on 2009-02-24
Last Modified: 2012-05-06
I have an application that sends output out on a sample port which must be redirected at the server level back to a second port in order for the application to process the information correctly.

For example, we will use:
Redirect from: 443
Redirect to: 8443
2nd IP:

I have the following rule which works fine running by itself, but I need to introduce a second IP into the system and each IP needs to redirect their output back to themselves on the new port.

/sbin/iptables -t nat -A OUTPUT -j REDIRECT -p tcp -d --dport 443 --to-ports 8443

/sbin/iptables -t nat -A OUTPUT -j REDIRECT -p tcp -d --dport 443 --to-ports 8443

Does anyone know how I could accomplish this?  Using PREROUTING will not work as it isn't redirecting the output like the rule above does.  
Question by:xiaoyunwu
    LVL 27

    Accepted Solution

    > each IP needs to redirect their output back to themselves on the new port.

    according to manual page
    'It redirects the packet to the machine itself by changing the  destination IP  to  the  primary  address  of  the  incoming  interface'

    so that in modified IP packet, the source IP address will always be regardless of original source IP.
    Try to use DNAT instead:
    /sbin/iptables -t nat -A OUTPUT  -p tcp -d --dport 443 -j REDIRECT --to-destination :8443
    /sbin/iptables -t nat -A OUTPUT  -p tcp -d --dport 443 -j REDIRECT --to-destination :8443

    P.S. '-to-destination :8443' is not misspelled (without IP), use it as is :-)

    Author Comment

    What version of iptables are you using?  I'm using 1.3.5 and it doesn't support the --to-destination flag.  This is for a Java based application run under Tomcat that posts information back to itself which is why I need to redirect the output to port 8443.  I am already NAT'ing:
    /sbin/iptables -t nat -A PREROUTING -p tcp -d --dport 443 -j DNAT --to
    /sbin/iptables -t nat -A PREROUTING -p tcp -d --dport 443 -j DNAT --to

    But this won't handle redirecting the output back to itself. I need to find a way to redirect the output in a similar manner to the rules above.

    /sbin/iptables -t nat -A OUTPUT  -p tcp -d --dport 443 -j REDIRECT --to-destination :8443
    iptables v1.3.5: Unknown arg `--to-destination'
    Try `iptables -h' or 'iptables --help' for more information.

    Do you have any other ideas?

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    I have seen several blogs and forum entries elsewhere state that because NTFS volumes do not support linux ownership or permissions, they cannot be used for anonymous ftp upload through the vsftpd program.   IT can be done and here's how to get i…
    Note: for this to work properly you need to use a Cross-Over network cable. 1. Connect both servers S1 and S2 on the second network slots respectively. Note that you can use the 1st slots but usually these would be occupied by the Service Provide…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    728 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

    21 Experts available now in Live!

    Get 1:1 Help Now