Reply to topic  [ 1 post ] 
HOWTO: Reverse SSH tunnel 
Author Message
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
Before doing this please make sure that you have permission to do this on all of the machines involved. Just because you can actually do it doesn't mean you are allowed to!

OK, you've got a system sitting at home and you want to access it over SSH while your out on the road? You can use reverse SSH to achieve this. We will assume the following:
  • The home system is called A
  • On the home machine (called A) is the user called Auser
  • There is some middle system you can SSH to (perhaps a web server?), this is called B
  • The machine called B has the user Buser
  • Your laptop your taking on the road is called C

So from machine A we will do the following:
Code:
A$ ssh -R 10002:localhost:22 Buser@B

That will connect you to the middle computer. When you want to connect to your home machine (called A), just do this from your laptop (machine C):
Code:
C$ ssh Buser@B
B$ ssh Auser@localhost -p 10002

You are now connected! You may want to write some sort of script to reconnect your home machine should it go down. You can use any number, it doesn't have to be 10002 :)

P.S.
If your connection keeps dropping, try the following:
Code:
A$ ssh -N -f -R 10002:localhost:22 Buser@B


I have a script that I run from my home system, the one I want to access when I'm on the road, it looks something like this:
Code:
#!/bin/bash
#So we can see how many times our connection has been dropped
DROPPED=0
#Initial connection
ssh -N -f -R 10002:localhost:22 Buser@B.com

while true; do

   #See if the connection is still active
   CONN_UP=`ps -ef | grep "ssh -N -f -R 10002:localhost:22 Buser@B.com" | grep -v grep`

   #If connection is down, enter IF
   if [[ -z $CONN_UP ]]; then
      #Reconnect
      ssh -N -f -R 10002:localhost:22 Buser@B.com
      #Increment DROPPED and print to file on Desktop
      DROPPED=$(( $DROPPED + 1 ))
      echo $DROPPED > ~/Desktop/DROPPED.txt
   fi
   
   #Sleep for 1 minute, stops a loop doing not a lot continuously running
   sleep 60
   
done


Fri Apr 02, 2010 10:46 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.