Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pnsinha/0781300a9126fe4a33e1f2609ab62d53 to your computer and use it in GitHub Desktop.
Save pnsinha/0781300a9126fe4a33e1f2609ab62d53 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
# port that will be use to ssh at remote server
REMOTE_PORT=54322
# change user as per remote server
USERNAME=user
# change SSH port used at jump server
SSH_TARGET_PORT=22
4) ==== /etc/systemd/system/[email protected]
[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 (systemd running as root)
ssh-copy-id user@yourjumpsshserver -p 443
You may need to run ssh-keygen. Make sure ls /root/.ssh is empty
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