Skip to content

Instantly share code, notes, and snippets.

@erm3nda
Created March 12, 2021 08:30
Show Gist options
  • Select an option

  • Save erm3nda/46ee9d22d416c2a65b87f1ee5d8ef4d7 to your computer and use it in GitHub Desktop.

Select an option

Save erm3nda/46ee9d22d416c2a65b87f1ee5d8ef4d7 to your computer and use it in GitHub Desktop.

Revisions

  1. erm3nda created this gist Mar 12, 2021.
    21 changes: 21 additions & 0 deletions ssh-persistent-tunnel.service
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    #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