With SSH, you can use local forwarding to access a service that's available from a well-known host. For example:
localhost$ ssh -L 8080:remotehost:80 corporate
asks localhost to take requests to http://localhost:8080/ and relay them through corporate to http://remotehost:80/. This is handy if the host remotehost is accessible from corporate but not from localhost. Note that the client, localhost, can be anywhere (e.g. behind a NAT or a firewall) because it is the one initiating the SSH connection.
You can also do the reverse (also known as reverse SSH), forwarding in the other direction to allow a service behind a NAT/firewall to be accessible from a well-known host. For example:
localhost$ ssh -R 8080:localserver:80 corporate
asks corporate to take requests to http://corporate:8080/ and relay them through localhost to http://localserver:80/. This is handy if localserver is a host which is accessible from localhost but not from corporate. Note that the proxy and server, localhost and localserver, can be anywhere (e.g. behind a NAT or a firewall) because localhost is the one initiating the SSH connection. This setup can be used by trusted users inside a firewall to circumvent the firewall.
I frequently connect from my laptop to a desktop machine to do work. I've configured reverse SSH to allow SSHing from my desktop to my laptop so that I can, for example, save files to my laptop using a program on my desktop. Again, this works whenever I connect, even if my laptop connects from behind a NAT/firewall, or if I'm just on some network where I haven't bothered to look up my IP address.
To do this manually, add a remote port-forward and use that port to connect from the remote end:
laptop$ ssh -R 8889:localhost:22 desktop
desktop$ ssh -p 8889 localhost
laptop$
Now, this is sort of unwieldy, so I've configured my .ssh/config to fill in all the boring parts automatically. On laptop:
Host desktop
RemoteForward 8889 localhost:22
Then, on desktop:
Host laptop
HostName localhost
HostKeyAlias laptop
Port 8889
Now the login sequence is much more streamlined:
laptop$ ssh desktop
desktop$ ssh laptop
laptop$
Notice the HostKeyAlias field. Old versions of OpenSSH would barf if, on desktop, you SSH'd to both localhost:22 and to localhost:8889, because it would see two different host keys associated with the same host and think that there was a man-in-the-middle attack going on. This is a common problem whenever port-forwarding is used. The HostKeyAlias is your way of telling SSH, "no, this connection I've set up has its own host key which you should remember, but that host key is different from that of localhost". (I think newer versions of OpenSSH are smart enough to remember the host key on a per host/port basis, so they aren't subject to this problem.)
Great! it works!
ReplyDeleteExperienced Linux user:
ReplyDeleteThis was the best, clearest explanation I ever seen on ssh tunneling from both local and reverse forwarding perspective , and I have searched a lot on this topic, but many people make the simple to difficult.
Thanks a million! Real Great Work!!!