Skip to content

Instantly share code, notes, and snippets.

@linuxmalaysia
Last active April 3, 2024 16:33
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save linuxmalaysia/05228a52feea1b117236a8a0d2373ade to your computer and use it in GitHub Desktop.
Save linuxmalaysia/05228a52feea1b117236a8a0d2373ade 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/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 (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 ====
@nazrimazlan
Copy link

terbaik.

@linuxmalaysia
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment