Last active
October 9, 2024 05:30
-
-
Save linuxmalaysia/05228a52feea1b117236a8a0d2373ade to your computer and use it in GitHub Desktop.
Autossh using systemd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ==== |
Boleh bincang di Telegram https://t.me/linuxmalaysiamy/112
Nota saya tentang Monit - Automatik Restart PHP-FPM dan Nginx Dengan Monit
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
terbaik.