February 9, 2017 - rob

Using autossh and x11vnc to access a computer

I used to access my linux desktop through a ssh tunnel through my home ip address. Comcast in my area though has made it harder to get ports open with their newer cable modems. I got tired of calling them, telling them what I wanted, to only get back answers of “huh” or “no”. One time they did allow the port open, was that was short lived. So instead, I started doing a tunnel to vps server. I already host this website on a vps server, so now I could just leverage that into a tunnel from my house. Here is how I got it to work:

Install autossh

sudo apt-get install autossh
Here is the help:
usage: autossh [-V] [-M monitor_port[:echo_port]] [-f] [SSH_OPTIONS]

-M specifies monitor port. Overrides the environment
variable AUTOSSH_PORT. 0 turns monitoring loop off.
Alternatively, a port for an echo service on the remote
machine may be specified. (Normally port 7.)
-f run in background (autossh handles this, and does not
pass it to ssh.)
-V print autossh version and exit.

Environment variables are:
AUTOSSH_GATETIME – how long must an ssh session be established
before we decide it really was established
(in seconds). Default is 30 seconds; use of -f
flag sets this to 0.
AUTOSSH_LOGFILE – file to log to (default is to use the syslog
AUTOSSH_LOGLEVEL – level of log verbosity
AUTOSSH_MAXLIFETIME – set the maximum time to live (seconds)
AUTOSSH_MAXSTART – max times to restart (default is no limit)
AUTOSSH_MESSAGE – message to append to echo string (max 64 bytes)
AUTOSSH_PATH – path to ssh if not default
AUTOSSH_PIDFILE – write pid to this file
AUTOSSH_POLL – how often to check the connection (seconds)
AUTOSSH_FIRST_POLL – time before first connection check (seconds)
AUTOSSH_PORT – port to use for monitor connection
AUTOSSH_DEBUG – turn logging to maximum verbosity and log to

For my use, I am using:

autossh -M 10001 -f -NXYR 10000: remote_host

That is saying port 10000 on the remote_host will forward to my local computer running port 22 (ssh).
The –M is the monitoring port for the connection.

Making autossh connect on startup

Now that we can run autossh, we might want it to run at startup. Just in case the power goes out at home or we install some updates and reboot. To do this, I created a new init script and a config script:

Vi /etc/init/autossh_host.conf
stop on stopping autossh


instance $N
export HOST=$N

ARGS=$(head -$N /etc/autossh.hosts | tail -1)
exec autossh $ARGS
end script

vi /etc/autossh.hosts
-M 10001 -f -NXYR 10000: remote_host

Install X11vnc

To install and configure, read this post:
autostart vnc

Connecting to the remote desktop

When I want to connect to my home desktop, I create a tunnel to my vps server. To make it easier to manage, I use a config file for my .ssh:
vi ~/.ssh/config
Host home
Hostname localhost
User username
Port 10000
ProxyCommand ssh -e none -W %h:%p remote_host
StrictHostKeyChecking no

Then to start the vnc tunnel, I use:
ssh home -L 5900:localhost:5900 “x11vnc -rfbauth ~/.vnc/passwd -display :0 -ncache 10 -noxdamage”

And last but not least, I connect via vnc:
vncviewer -quality 5 -compresslevel 0 localhost:5900

When I am done, I just close out of my vnc session and close the terminal session.