Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

   IPTABLES="/usr/sbin/iptables"

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

   $IPTABLES --policy INPUT   ACCEPT
   $IPTABLES --policy FORWARD ACCEPT
   $IPTABLES --policy OUTPUT  ACCEPT

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

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

But when I input

ssh user@localhost

or

ssh user@192.168.2.1 # (address of local host)

I get error 'Connection refused'.

Please, help me.

share|improve this question

migrated from stackoverflow.com Dec 1 '11 at 10:20

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

1 Answer 1

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

 
discard

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.