Skip to content

Instantly share code, notes, and snippets.

@ttimasdf
Forked from thomasfr/autossh.service
Last active April 9, 2024 19:36
Show Gist options
  • Star 56 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save ttimasdf/ef739670ac5d627981c5695adf4c8f98 to your computer and use it in GitHub Desktop.
Save ttimasdf/ef739670ac5d627981c5695adf4c8f98 to your computer and use it in GitHub Desktop.
Systemd service for autossh

Usage

curl -sSL https://gist.githubusercontent.com/ttimasdf/ef739670ac5d627981c5695adf4c8f98/raw/autossh@host1 | \
  sudo tee /etc/default/autossh@example
curl -sSL https://gist.githubusercontent.com/ttimasdf/ef739670ac5d627981c5695adf4c8f98/raw/autossh@.service | \
  sudo tee /etc/systemd/system/autossh@.service

sudo useradd -g nogroup -s /bin/false -m tunnel
sudo -u tunnel mkdir -p ~tunnel/.ssh  # and copy your private key here
sudo -u tunnel nano ~tunnel/.ssh/config  # add host and key configs here
sudo -u tunnel ssh example.com  # just make sure to add your host to `known_hosts`

# Add the hostname and forwards to /etc/default/autossh@example
sudo nano /etc/default/autossh@example
# ready to go!
systemctl start autossh@example.service

systemctl status autossh@example.service
journalctl -fu autossh@example
[Unit]
Description=Keeps an ssh tunnel to %I open
After=network-online.target ssh.service
[Service]
User=tunnel
# no monitoring
Environment="AUTOSSH_PORT=0"
# Disable gatetime behaviour
Environment="AUTOSSH_GATETIME=0"
EnvironmentFile=/etc/default/autossh@%i
RestartSec=3
Restart=always
# -NT Just open the connection and do nothing (not interactive, no tty alloc)
# use /usr/bin/ssh instead of autossh is good as well
ExecStart=/usr/bin/autossh -NT -o "ExitOnForwardFailure=yes" $SSH_OPTIONS ${TARGET_HOST} $FORWARDS
TimeoutStopSec=10
[Install]
WantedBy=multi-user.target
# Options for autossh@host1.service
# Place it at /etc/default
# Save all your credential/user/port related config in ~/.ssh/config is strongly recommanded
# Leave hostname here only
TARGET_HOST=remote.example.com
# -L LOCALPORT:IP_ON_EXAMPLE_COM:PORT_ON_EXAMPLE_COM
# can set multiple forwardings here
FORWARDS=-L 7474:127.0.0.1:7474
# === Settings below for ADVANCED users only ===
SSH_OPTIONS=-o "ServerAliveInterval=10" -o "ServerAliveCountMax=3"
AUTOSSH_PORT=0
AUTOSSH_GATETIME=0
@arno01
Copy link

arno01 commented Sep 15, 2018

Useful, thank you!

@dotWee
Copy link

dotWee commented Apr 29, 2019

Basic and functional, thanks for sharing!

@VibroAxe
Copy link

@ttimasdf nice unit file, great improvements on the original with multiple hosts. Exactly what I was looking for!

I'm not so keen on setting the key / identity parameters with .ssh/config rather than etc/default as it just means another file to remember to edit. I tweaked yours slightly in https://gist.github.com/VibroAxe/80d55cc32b5c3290d7ee8b44b9d9a8cd to add SSH_USER and SSH_IDENTITY_FILE parameters to the env

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