Setup SSH Tunnel for SytemD

The what:

There are several cases when you just need some SSH tunnel to another system with some port redirections.
If the tunnel fails for whatever reason, it should reset itself. Setting up a SSH tunnel with port redirection as a service is the answer!

The How:

Creating SystemD config

The setup is a “user defined” config. When the user is logged on to the system, the service will become active.
You need to setup SSH password less login setup (with keys) to be able to use this.

First you need to created some directories (if not present)

Then create a SystemD service definition file inside this location
Name the file:

The content….:

After creating the file, SystemD needs to know that this file is present. You need to do a reload of SystemD

Almost done…..

Define the tunnels

To define a tunnel, add the following lines to your ~/.ssh/config file. If not present, create the file.

More tunnels? just duplicate the block with other params.
If you need more option for SSH, take a look at man ssh_config

Running the tunnels

Lets get your hard work up and running!
Use the systemctl command to start/stop/status you’re tunnel

To set-up autostart, use the same command but replace start with enable
This will start the tunnel when you log-on to your system.

Thats it! you’re up and running!

Checking / Stopping the tunnel

For status checking, use the systemctl command

To stop the tunnel