Auto start an SSH tunnel and keep alive

Hold on Cowboy

This blog post is pretty old. Be careful with the information you find in here. It's likely dead, dying, or wildly inaccurate.

I have a program that needs to talk to another server, but to secure the traffic I’ve set up a port forwarding SSH tunnel. The only problem is that this tunnel needs to be kept alive and started when the server boots up. Here is how, using


For the server you want to make connections from follow these instructions.

Open up

and insert this code somewhere near the bottom `

Keeps an SSH port forwarding connect between serverA <—> serverB for mysql 

sm:345:respawn:/usr/bin/ssh -N -L 3307: -l admin ` Let’s break it down

  • sm This is just an random two letter code that distinguishes it from other processes inside inittab
  • 345 These are run-levels that you want the process to run.
  • respawn What to do if the process dies, respawn it
  • /usr/bin/ssh ssh binary
  • -N Tells SSH not to run any remote command after the connection has been established
  • -L 3307: This tells SSH to set up a tunnel with local port being 3307, remote host, remote port 3306
  • -l admin What user to log in by
  • Remote host to SSH into
  • For those familiar with SSH, it should go without saying that you need to set up pre-shared keys to automatically log into the remote server

Did this help you out? It took me a few days to piece together all this information together, I hope this saves you some time (who knows, maybe the future me will be thankful I wrote this down). Let me know your thoughts. [email protected]