I have an ubuntu desktop 14.04 running on a vagrant VM (virtualbox).

I am trying to forward port 25 to port 2525 and I have executed the following commands:

sudo sysctl net.ipv4.ip_forward=1
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 25 -j REDIRECT --to-port 2525
sudo iptables -t nat -A POSTROUTING -j MASQUERADE

The command:

cat /proc/sys/net/ipv4/ip_forward

returns 1.

I can open a connection on port 2525:

vagrant@vagrant:~$ telnet localhost 2525
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 alfresco.com ESMTP SubEthaSMTP 3.1.6

but telnet on port 25 does not work:

vagrant@vagrant:~$ telnet localhost 25
Trying ::1...
telnet: Unable to connect to remote host: Connection refused

I tried also to connect to the local ip of the machine with same results.

telnet 2525

is ok, but

telnet 25

returns the same (connection refused).

The command:

iptables -t nat -L


target     prot opt source               destination         
REDIRECT   tcp  --  anywhere             anywhere             tcp dpt:smtp redir ports 2525
REDIRECT   tcp  --  anywhere             anywhere             tcp dpt:2526 redir ports 2525

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

target     prot opt source               destination         
MASQUERADE  all  --  anywhere             anywhere            


It would be enough for me if someone can suggest at least how to troubleshoot this kind of problems.

  • Did you mean connecting to fails, and :2525 succeds? Becuase that would be the exact opposite of what you posted right above it. – user508889 May 26 '16 at 12:37
  • @bc2946088 you are right. I amended my question. Thanks – Marco Altieri May 26 '16 at 13:01

You could use socat:

socat TCP-LISTEN:25,su=nobody,fork,reuseaddr TCP-CONNECT:

This tells socat to listen on TCP port 25 and connect it to local 2525. It runs as the nobody user, for better security. The fork option means that it will renegotiate the incoming port, so that it doesn't block.

This is based on the following post: https://superuser.com/a/536295/621970

|improve this answer|||||

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.