title: redirect traffic with iptables to local port
body: <p>I have a machine IP1 that sends packets to port 54321 of IP2. I want to redirect this requests to a port 8080 of IP1 before these packets leave machine IP1 and go to IP2. This means I need to filter outcoming traffic.</p>
  
  <p>I tried answers from related questions but this does not help:</p>
  
  <pre><code>iptables -t nat -A OUTPUT -p tcp -d IP1 --dport 54321 -j REDIRECT --to-ports 8080
  
  iptables -t nat -A OUTPUT -p tcp --dport 54321 -j DNAT --to-destination 127.0.0.1:8080
  </code></pre>
  
  <p>Reading definitions of DNAT and REDIRECT still leave me confused what should work here.</p>