Skip to content

Instantly share code, notes, and snippets.

@erm3nda
Created March 12, 2021 08:30
Show Gist options
  • Save erm3nda/46ee9d22d416c2a65b87f1ee5d8ef4d7 to your computer and use it in GitHub Desktop.
Save erm3nda/46ee9d22d416c2a65b87f1ee5d8ef4d7 to your computer and use it in GitHub Desktop.
SSH permanent tunnel service
#Template SystemD service for a auto healing (reverse) SSH tunnel.
#Place this to /etc/systemd/system/ folder.
#Edit all values inside <>
#/etc/systemd/system/ssh-persistent-tunnel.service
#Edit the ports, remote user+host, and the key file.
#In the authorized_keys file on the other system, use something like this:
#<restrict,command="echo 'No Commands Allowed.'",port-forwarding,permitopen="localhost:22",permitlisten="localhost:5000">
[Unit]
Description=SSH Persistent Tunnel
After=network-online.target
[Service]
ExecStart=/usr/bin/ssh -T -N -o ServerAliveInterval=30 -o ServerAliveCountMax=3 -o ExitOnForwardFailure=yes -R <1234:localhost:22> <root@localhost> -o IdentityFile=</home/user/.ssh/id_rsa>
ExecStop=/bin/kill -HUP $MAINPID
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=60
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment