SSH as Virtual Private Network
You can use ssh to tunnel protocols needed for other applications through a safe, encrypted passage over the internet. In this way, you can still use a simple ftp- or mail-client, which otherwise would send your password in clear text. Because ssh encrypts every packet entering the tunnel, and decrypts it at the other side, the application in question never even notices that its traffic is being encrypted; it works as if no encryption is taking place.If you already know about tunneling and are only interested in the exact details of how to use tunneling, have a look at this explanation of how some specific services can be tunneled.
The key to the tunneling capabilities of ssh is the way tcp/ip ports on the local and the remote machine are connected by ssh in behalf of the protocol/application to be tunneled. Because the connection is made by ssh and the application connects to a port on which ssh is listening, ssh is capable of encrypting everything it receives from the application and decrypting it at the other end, where it then feeds the packets to the port the application normally connects to. In this way, ssh provides for a secure, encrypted tunnel, over an otherwise insecure medium (the internet).
Ssh is capable of tunneling as well a client connection as a server connection. In tunneling a client connection, ssh is started from the same end of the normally insecure medium as the (client) application that is to be tunneled. In tunneling a server connection, ssh is started from the same end of that insecure medium as the server is sitting, waiting for clients to connect.
So in tunneling a client connection, you assume that the communication channel between your local machine and the remote machine is insecure, but that the connection between the remote machine and the server you are trying to use is secure. You also expect the client application to connect in a secure way to the local machine, by either being executed on the local machine itself, or from a machine that has a secure connection to the local machine.
In tunneling a server connection on the other hand, you assume that the communication channel between your local machine and the server is secure, but that the connection from your local machine to the remote machine is insecure. You also expect that the clients will connect to the remote machine in a secure way, by either being executed on the remote machine itself, or from a machine that has a secure connection to the remote machine.
Local port forwarding
Tunneling a client connection is done by way of local port forwarding. A typical example is using an ftp-client from a machine connected to an insecure network, to a server behind a firewall. By forwarding some local port to the remote port 21, and connecting the ftp-client to that port on the local machine, every communication of the ftp-client goes, via that local port, through the ssh connection to the remote machine, where it is forwarded to port 21, on which the ftp server is listening.
The above image might explain tunneling a client connection better than words can do. In case of the ftp-client, following commands establish the tunnel and use it in an ftp session:
-
(in one terminal)
$ ssh -L 1234:ftp:21 ftp
(in another terminal, since the first one is still occupied by
the ssh connection)
$ ftp
ftp> open localhost 1234
...
(from here on, you are connected to the ftp server on the
machine `ftp')
- the local machine from which the ssh connection is started,
- the remote machine at which the ssh connection is targeted,
- the machine on which the server you are trying to use is running,
- the machine on which you will execute the client application.
- the local machine from which the ssh connection is started and on which the client application will be run,
- the remote machine at which the ssh connection is targeted and on which the server you are trying to use is running.
Notice also that the local port you are tunneling can be freely chosen. The only restrictions you have to obey are:
- your client application must be capable of connection to that port. In this (ftp) example, if you use a ftp client that cannot connect to another port than 21, you must use 21 as the local port. Otherwise, as is demonstrated here, you can choose any port, provided of course that it is not already used on the local machine !
- you must have sufficient rights to control that port. On most Unix machines, an ordinary user cannot control the ports with a number lower than 1024, as is demonstrated in this example.
Remote port forwarding
Tunneling a server connection is done by way of remote port forwarding. For the rest it is very similar to local port forwarding.
Restrictions with port forwarding
Please make sure that your SSH tunnels are only accessible from your own machine: any configuration items such as Allow remote hosts to connect to tunnel should be configured such that only localhost can connect to any tunnel you set up ! If your tunnels are accessible for other machines as well, you create huge holes in the departmental security ... so please take care.
The ftp-client example is also well suited to point out an
important restriction with port forwarding. Ftp uses 2
connections: one stream carries the control of the flow (the
command you give), the other stream carries the data you
transfer. With the port forwarding as explained above, you only
encrypt the control stream. The data stream still travels
unencrypted over the internet.
More generally, you must know which ports to forward to be able
to use the feature to its full potential and to be sure your
communication cannot be eaves-dropped upon.
Moreover, the ftp server you are trying to contact must allow the data connection to be established to a different machine than the command connection. This might be a security problem because this opens the door to hijacking and/or spoofing your ftp connection and therefore ftp servers might choose not to allow this. Tunneling the command connection does exactly this : the command connection comes from one end of the tunnel and the data connection from the other end. Usually the two ends of the tunnel are different machines (if not, why use a tunnel ;-) and therefore, the ftp server might not allow any data being exchanged. So you would be allowed to login, but you would not be allowed to get a listing of the available files, let alone the files themselves.
And another ftp specific restriction is that the tunneled ftp-connection only works if you use passive mode ftp. Most ftp clients nowadays allow to specify that you want to use passive mode; if not, look for another ftp client.

