Κυριακή 23 Μαΐου 2010

Local And Remote Forwarding

There are two kinds of port forwarding: local and remote forwarding. They are also called outgoing and incoming tunnels, respectively.

Local port forwarding forwards traffic coming to a local port to a specified remote port. For example, all traffic coming to port 1234 on the client could be forwarded to port 23 on the server (host).

Note: The value of localhost is resolved after the Secure Shell connection has been established - so when defining local forwarding (outgoing tunnels), localhost refers to the server (remote host computer) you have connected to.

Remote port forwarding does the opposite: it forwards traffic coming to a remote port to a specified local port. For example, all traffic coming to port 1234 on the server (host) could be forwarded to port 23 on the client (localhost).

It is important to realize that if you have three hosts, client, sshdserver, and appserver, and you forward the traffic coming to the client's port x to the appserver's port y, only the connection between the client and sshdserver will be secured.

2 σχόλια:

  1. Local Forwarding Example:

    ssh -f user@personal-server.com -L 2000:personal-server.com:25 -N

    The -f tells ssh to go into the background just before it executes the command. This is followed by the username and server you are logging into. The -L 2000:personal-server.com:25 is in the form of -L local-port:host:remote-port. Finally the -N instructs OpenSSH to not execute a command on the remote system.

    This essentially forwards the local port 2000 to port 25 on personal-server.com over, with nice benefit of being encrypted. I then simply point my E-mail client to use localhost:2000 as the SMTP server and we're off to the races.

    ΑπάντησηΔιαγραφή
  2. ssh -R allows you to set up tunnels so that you can connect to a computer which doesn't allow incoming connections. This is done by using a "server" computer which does accept incoming connections. The "client" which doesn't accept incoming connections connects to the server and sets up a reverse tunnel.

    To set up the reverse tunnel, use:

    $ ssh -nNT -R 1100:local.mydomain.com:1100 remote.mydomain.com

    What this does is initiate a connection to remote.mydomain.com and forwards TCP port 1100 on remote.mydomain.com to TCP port 1100 on local.mydomain.com. The "-n" option tells ssh to associate standard input with /dev/null, "-N" tells ssh to just set up the tunnel and not to prepare a command stream, and "-T" tells ssh not to allocate a pseudo-tty on the remote system. These options are useful because all that is desired is the tunnel and no actual commands will be sent through the tunnel, unlike a normal SSH login session. The "-R" option tells ssh to set up the tunnel as a reverse tunnel.

    Now, if anything connects to port 1100 on the remote system, it will be transparently forwarded to port 1100 on the local system.

    Let's assume that Destination's IP is 192.168.20.55 (Linux box that you want to access).

    You want to access from Linux client with IP 138.47.99.99.

    Destination (192.168.20.55) <- |NAT| <- Source (138.47.99.99)

    SSH from the destination to the source (with public ip) using command below:

    ssh -R 19999:localhost:22 sourceuser@138.47.99.99

    ΑπάντησηΔιαγραφή