Autossh

Installation
Installing autossh is easy because it is included in portage:

Add to boot
To configure your system to automatically start autossh you can add the following lines to your file.

If you do not have a network connection during boot, autossh fails to start (Network unreachable, Could not resolve hostname). If you set the environment variable AUTOSSH_GATETIME=0, autossh does not abort in such a case but keeps trying to connect.

Maintaining a tunnel
The following will create a port forwarding ssh tunnel between localhost and my.linuxbox.at.home.

When connecting to my.linuxbox.at.home:8081, the request will be sent to localhost:80. This is good for making a webpage behind a corporate firewall viewable to the outside world.

autossh -M29001 -f -R 8081:localhost:80 my.linuxbox.at.home |______||_||_||___||________|__|__________________|           |     |  |   |      |      |          `---The box we are connecting to. Must be running sshd. |    |  |   |      |      |            |     |  |   |      |      `--The port on localhost we want to access from my.linuxbox.at.home |    |  |   |      |            |     |  |   |      `-The host where the forward will go, relative |    |  |   |                            to the box initiating the tunnel; i.e., localhost |    |  |   |                            means the box where this command is run. |    |  |   |            |     |  |   `This is the port my.linuxbox.at.home listens |    |  |                                on to forward through the tunnel |    |  |             |     |  `Tells ssh to forward from the remote |    |                                   host to the local host |    |            |     `---Causes autossh to drop into the background before execution `-Use this monitoring port (must be above 1024 and not currently in use)

You may need to add a to the ssh options. You may also want to add.

Without them, the option complains that it needs a command to fork and will just quit. says no command and says be quiet. Also if you would like to have a special key with no passphrase you can generate one and then use it via the option for ssh.

In order for this to work without user interaction should be using Public Key Authentication.

Using OpenSSH's built in connection monitoring
The newer versions of OpenSSH have their own method of checking if the connection is still alive. You can enable this by setting the ServerAliveInterval and ServerAliveCountMax options (either in your ssh_config file or on the command line). For example

The above command will make ssh send a keep-alive request if no other data has been sent for 60 seconds, if it doesn't receive a reply after 3 attempts it will close the connection. autossh will then detect its been closed and attempt re-establish it.

The "-M 0" option disables autossh's own monitoring which uses separate ports and is less reliable.