How to access a remote machine at work from home using SSH

I just found out how to mount the home directory of my work PC at home, bypassing the company firewall.

First of all password authentication is tedious and won’t work in batch scripts so we need to create a RSA key on the work pc:

ssh-keygen
scp ~/.ssh/id_rsa.pub [myhomeuser]@[homehost]

Now log at home and add the work pc public key to your authorized keys:

cat id_rsa.pub >> .ssh/authorized_keys2

Now let’s connect to the home pc from the machine at work using ssh.

ssh  -f -C -o BatchMode=yes -R [anyport]:localhost:22 -l [myhomeuser] -N [homehost]

The -R argument does the trick by forwarding to the work pc (port 22) all traffic on the loopback interface (on a given port) of your home computer. Refer to the SSH man for info.
-o BatchMode=yes makes the ssh client at work try to contact the remote server at home every 300 seconds in order to prevent the firewall from closing the connection. If you get a “Connection reset by peer” after a period of inactivity, try -o ServerAliveInterval=[seconds].

Let’s move to the home pc. In order to mount the remote pc on the filesystem you’ll need SSHFS, if you’re using Ubuntu follow this great How-to.

First let’s create a directory for the mount point:
mkdir /home/[myhomeuser]/workpc

Make sure the FUSE kernel module has been loaded:

sudo modprobe fuse

Now mount the work pc home directory:

sshfs -C -p [anyport] localhost:/home/[myworkuser] /home/[myhomeuser]/workpc

Make sure that the port number is the same specified in the ssh command at work.

If you need to unmount just type:

sudo umount /home/[myhomeuser]/workpc

Done. Now cd to ~/workpc and type one of the most satisfactory ls of your life.

UPDATE: What happens if any of the two machines reboots or the network goes down? The tunnel dies.
You may use this simple Bash script as a cronjob installed on the work pc. Thanks to Fabrizio for pointing this out!

#!/bin/bash

HOMEPC=[homehost]
HOMEUSER=[homeuser]
HOMEPORT=[homeport]
# Path of a file used to test the connection
HOMEFILE=[homefile]

ssh $HOMEUSER@$HOMEPC scp -P $HOMEPORT $HOMEFILE localhost:/tmp/
if [ $? != 0 ]; then
  echo Starting SSH tunnel at `date`
  ssh -f -C -N -o BatchMode=yes -R $HOMEPORT:localhost:22 -l $HOMEUSER $HOMEPC
fi

Type crontab -e and add something like this:

0,20,40 * * * * /home/[user]/tunnel.sh
  1. Prasanta Shee says:

    Instead of using SSH, you can use tools like on premise R-HUB remote support servers for remotely accessing your work machine from home. It works from behind the firewall hence better security.