Systemd is ideally suited for this.
Create a service file /etc/systemd/system/sshtunnel.service
containing:
[Unit]
Description=SSH Tunnel
After=network.target
[Service]
Restart=always
RestartSec=20
User=sshtunnel
ExecStart=/bin/ssh -NT -o ServerAliveInterval=60 -L 5900:localhost:5900 user@otherserver
[Install]
WantedBy=multi-user.target
(Modify the ssh command to suit)
- this will run as user
sshtunnel
so make sure that user exists first - issue
systemctl enable sshtunnel
to set it to start at boot time - issue
systemctl start sshtunnel
to start immediately
Update Jan 2018: some distros (e.g. Fedora 27) may use SELinux policy to prevent the use of SSH from systemd init, in which case a custom policy will need to be created to provide the necessary exemptions.