This answer belongs to brandonchecketts.
Assume You have two hosts named as Host-A and Host-B. Now we are going to create a SSH Tunnel between these two and make sure that Tunnel is up & live for all the time.
Configuration Need to be done for Host-A:
Open your terminal , turn into root and paste the code one after one
useradd -d /home/tunnel tunnel
passwd tunnel
su - tunnel
next step is about creating a SSH key
In the terminal paste as
ssh-keygen
and then choose default choice for all prompt and copy the key with
cat /.ssh/id_rsa.pub
Now this time we have to configuration for Host-B
Open your terminal and execute these commands
useradd -d /home/tunnel tunnel
passwd tunnel
su - tunnel
and in terminal type as
mkdir .ssh
vi .ssh/authorized_keys
It will open a file in terminal and paste the above copied key from Host-A.
Now in the terminal type as
vi /home/tunnel/check_ssh_tunnel.sh
and paste as
createTunnel() {
/usr/bin/ssh -f -N -L13306:hostb:3306 -L19922:hostb:22 tunnel@hostb
if [[ $? -eq 0 ]]; then
echo Tunnel to hostb created successfully
else
echo An error occurred creating a tunnel to hostb RC was $?
fi
}
## Run the 'ls' command remotely. If it returns non-zero, then create a new connection
/usr/bin/ssh -p 19922 tunnel@localhost ls
if [[ $? -ne 0 ]]; then
echo Creating new tunnel connection
createTunnel
fi
save and close and make it executable with
chmod 700 /home/tunnel/check_ssh_tunnel.sh
and then run the script, it will start a Tunnel with remote PC.
Read that above link, it is must.