Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamsky/f4586dd96103ad9f1ba82bd16d6204ad to your computer and use it in GitHub Desktop.
Save tamsky/f4586dd96103ad9f1ba82bd16d6204ad to your computer and use it in GitHub Desktop.
Autossh using systemd
1) ==== Autossh using systemd ====
Example from
https://gist.github.com/drmalex07/c0f9304deea566842490
2) =============
Install autossh
3) ==== /etc/default/secure-tunnel@yourjumpsshserver == change yourjumpsshserver to your ip or dnsname
TARGET=yourjumpsshserver
LOCAL_ADDR=0.0.0.0
LOCAL_PORT=22
REMOTE_PORT=54322
USERNAME=user
SSH_TARGET_PORT=443
4) ==== /etc/systemd/system/secure-tunnel@.service
[Unit]
Description=Setup a secure tunnel to %I
After=network.target
[Service]
Environment="LOCAL_ADDR=localhost"
EnvironmentFile=/etc/default/secure-tunnel@%i
###ExecStart=/usr/bin/ssh -NT -o ServerAliveInterval=60 -o ExitOnForwardFailure=yes -L ${LOCAL_ADDR}:${LOCAL_PORT}:localhost:${REMOTE_PORT} ${USERNAME}@${TARGET}
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh -M 0 -o "ExitOnForwardFailure=yes" -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -NR ${REMOTE_PORT}:${LOCAL_ADDR}:${LOCAL_PORT} -p ${SSH_TARGET_PORT} ${USERNAME}@${TARGET}
# Restart every >2 seconds to avoid StartLimitInterval failure
RestartSec=5
Restart=always
[Install]
WantedBy=multi-user.target
6) ===== SSH key copy to yourjumpsshserver
As root
ssh-copy-id user@yourjumpsshserver -p 443
5) ============ systemctl
systemctl daemon-reload
systemctl status secure-tunnel@yourjumpsshserver
systemctl enable secure-tunnel@yourjumpsshserver
systemctl start secure-tunnel@yourjumpsshserver
systemctl stop secure-tunnel@yourjumpsshserver
==== Autossh using systemd ====
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment