Ssh – Openbsd init script for ssh VPN tunnel

bsdopenbsdsshssh-tunnel

I have a server hosting SSH tunnels and Openbsd 4.5 clients connecting to it.

Things work just fine but I am in the need of automating the connection from the client to the server. So that if the client is accidentally rebooted, then the connection initiates unattended.

So it should be as straight forward as to include the ssh connection in an init script. However I have miserably failed to do so by including it to /etc/rc.local, which is the file I usually do this sort of things in.

Right now I am using autossh to also restart the connection if necessary and the script that I put on /etc/rc.local follows:

#!/bin/sh
#
# Example script to start up tunnel with autossh.
#
# This script will tunnel 2200 from the remote host
# to 22 on the local host. On remote host do:
#     ssh -p 2200 localhost
#
# $Id: autossh.host,v 1.6 2004/01/24 05:53:09 harding Exp $
#

ID=root
HOST=example.com

#AUTOSSH_POLL=600
#AUTOSSH_PORT=20000
#AUTOSSH_GATETIME=30
#AUTOSSH_LOGFILE=$HOST.log
#AUTOSSH_DEBUG=yes 
#AUTOSSH_PATH=/usr/local/bin/ssh
export AUTOSSH_POLL AUTOSSH_LOGFILE AUTOSSH_DEBUG AUTOSSH_PATH AUTOSSH_GATETIME AUTOSSH_PORT

autossh -2 -f -M 20000 ${ID}@${HOST}

The script detaches just fine when run manually so I just include it on /etc/rc.local as

echo -n 'starting local daemons:'

if [ -x /usr/local/sbin/autossh.sh ]; then
   echo -n 'ssh tunnel'
   /usr/local/sbin/autossh.sh
fi

echo '.'

I have also tried calling it from /etc/hostname.tun0 in case there may be issues with /etc/rc.local not being called at the right time when network connections are ready, so I would use:

inet 10.254.254.2 255.255.255.252 10.254.254.1
!/usr/local/sbin/autossh.sh

Your input is highly appreciated.

Best Answer

I failed to complete understand your question: are you sure your autossh is run? If you don't find the tunnel open upon restart, it might be that it is not even started or that it has completed immediately because of an error. If order to check it, you may insert a call to "logger" command and log a message via syslog before and after running autossh, so you are sure that it is run. Please include the shell $rc variable in the second call to "logger" command.

Another option would be to run ssh (without autossh) from /etc/inittab as explained in this other question: runuser in rc.local