Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I try to redirect ssh-traffic from one machine to other with following bash sctipt:


   $IPTABLES -F        
   $IPTABLES -X        
   $IPTABLES -F -t nat 
   $IPTABLES -X -t nat 
   $IPTABLES -F -t mangle
   $IPTABLES -X -t mangle


   $IPTABLES -t nat -A POSTROUTING --out-interface ppp0 -j MASQUERADE
   $IPTABLES -t nat -A PREROUTING  -p tcp --dport 22 -j DNAT --to

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

But when I input

ssh user@localhost


ssh user@ # (address of local host)

I get error 'Connection refused'.

Please, help me.

share|improve this question

migrated from Dec 1 '11 at 10:20

This question came from our site for professional and enthusiast programmers.

ssh user@localhost won't work, at least not with the rules you've used.

The PREROUTING chain works on packets coming into the machine, whereas if you're connecting to localhost, packets are being generated locally.

The upside, though, is that your configuration should work for external connections. Have you tried it from somewhere else?

Edit: This is a useful diagram of the flows for netfilter:-

enter image description here

share|improve this answer
Thank You, Andy. Now it's clean for me. When I "tried it from somewhere else" these rules work well, according with the scheme. – Михаил Беляков Dec 3 '11 at 20:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.