How To make SSH Tunnel

In this post I'll discuss no how to make ssh tunnel
this how to will show you the basic, and I hope you will take the good from it to your needs.

the main configuration is:
edit /etc/ssh/ssh_config with your favorite editor
# vi /etc/ssh/ssh_config
and add flowing lines in the end

Host test
HostName localhost
User your_user_name
LocalForward 2022 SERVER1_IP:22
LocalForward 2080 SERVER2_IP:80
LocalForwars 2025 SERVER3_IP:25

save the file.
and run: # ssh test
now if you'll try to access to localhost on port 2080 you'll go to SERVER2 on port 80, you can test it from your browser - http://localhost:2080/ or try to ssh to locahost with port 2022
# ssh -p 2080 localhost

and you'll see you go to SERVER1.

You can run the ssh tunnel from one command line instead of edit the ssh_config.
# ssh -N -f User_Name@SERVER2_IP -L 2080/localhost/80
this script tell the server to forward all the traffic to port 2080 to SERVER2 on port 80

Or if you want to make REVERSE tunnel you can run the same command but with -R instead of -L
# ssh -N -f User_Name@SERVER2_IP -R 2022/localhost/22
(with this script you can access from the remote machine (SERVER2) via ssh to your machine with port 2022)
run # ssh -p 2022 localhost from SERVER2

I'm hoping this post will help you to understand the basic of ssh tunneling
Please comment

1 comment: