SSH Tunneling

Hi Folks,

From now on I will need to use the SSH by tunneling to connect from my Home machine (Client) to my Office machine (Server) through a SSH Gateway because my Office machine is not directly connected to the Internet anymore, it seems a little bit complicated but I will try to simplify the things.

First of all the scheme to guide those who still may not have understood:

To access/connect the server I want, I can do by 2 ways:

    1. Connect to the SSH Gateway and then, connect to the desired server, it is the simplest solution but become quite costly and annoying when you need the SSH or SCP often, especially the SCP because you must copy to a tmp directory of the SSH Gateway all the files you want and then copy to desired server.

 

    1. Open a port in the client machine that connects to the desired server directly by tunneling, may seem more complicated but worth it. I will show how to do it bellow.

 

How to open the tunnel:

ssh -p <SSH_port_in_SSH_gateway> -fN -L <Port_to_use_in_client_machine>:<Server_IP>:<SSH_port_in_Server> <user>@<SSH_Gateway_IP>

 

example:

ssh -p 22 -fN -L 6666:199.199.199.199:22 my_user@200.200.200.200

 

How to use the tunnel – SSH:

ssh -p <Port_to_openned_in_client_machine> <user>@localhost

 

example:

ssh -p 6666 my_user@localhost

 

How to use the tunnel – SCP:

scp -P <Port_openned_in_client> <user>@localhost:<file_to_be_coppied_in_server> <file_destination_in_client>

 

example 1 (copy from server):

scp -P 6666 my_user@localhost:/home/my_user/Desktop/foo /home/my_user/Desktop/

 

example 2 (copy to server):

scp -P 6666 /home/my_user/Desktop/foo my_user@localhost:/home/my_user/Desktop/

 

That’s it folks

Bye

NOTE:
Thanks to my friend Marcelo Gomes, who gave me the tips: Marcelo’s Blog (Blog in portugueses).