Iptables port forwarding to server with different port

iptablesport-forwardingsmtp

I am trying to port forward from one server to another. I have tried numerous iptables commands but they don't work. Am I doing something wrong?

Here is my setup:

  • VPS1: 1.1.1.1
    • SMTP Port : 25
    • UFW: OFF
    • iptables policy: Accept All
    • Hostname: mailbox.xxxxxx.com
    • OS: Ubuntu 14.04
  • Mail Server: 2.2.2.2
    • SMTP NAT Port: 15324

I have tried these commands:

iptables -t nat -A PREROUTING -p tcp --dport 25 -j DNAT --to-destination 2.2.2.2:15324
iptables -A FORWARD -d 2.2.2.2 -p tcp --dport 15324 -j ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

iptables -t nat -nvL

Chain PREROUTING (policy ACCEPT 459 packets, 17802 bytes)
pkts bytes target prot opt in out source destination
19 1088 DNAT tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:25 to:2.2.2.2:15324

Chain POSTROUTING (policy ACCEPT 2 packets, 120 bytes)
pkts bytes target prot opt in out source destination
39 2603 MASQUERADE all — * eth0 0.0.0.0/0 0.0.0.0/0

After running a telnet to port 25 on the external IP I get this. The packet count changes so it is getting something, but I don't know why it isn't going through PREROUTING

Chain PREROUTING (policy ACCEPT 469 packets, 18178 bytes)
pkts bytes target prot opt in out source destination
19 1088 DNAT tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:25 to:2.2.2.2:15324

Chain POSTROUTING (policy ACCEPT 3 packets, 180 bytes)
pkts bytes target prot opt in out source destination
42 2812 MASQUERADE all — * eth0 0.0.0.0/0 0.0.0.0/0


Trying to telnet shows me this.

telnet 2.2.2.2 15324
Trying 2.2.2.2…
Connected to 2.2.2.2.
Escape character is '^]'.
220 mailbox.xxxxxx.com ESMTP Hi, I'm a Mail-in-a-Box (Ubuntu/Postfix; see https://mailinabox.email/)

telnet mailbox.xxxxxx.com 25
Trying 1.1.1.1…
telnet: Unable to connect to remote host: Connection refused

Best Answer

Damn it. I fixed it like a min after posting this. Solution:

echo 1 > /proc/sys/net/ipv4/ip_forward