Skip to content

Instantly share code, notes, and snippets.

@happyincent
Last active January 27, 2019 14:04
Show Gist options
  • Save happyincent/e2c10f704bddfda57983052009d709f4 to your computer and use it in GitHub Desktop.
Save happyincent/e2c10f704bddfda57983052009d709f4 to your computer and use it in GitHub Desktop.
An autossh tunnel service
[Unit]
Description=An autossh tunnel service
After=network-online.target
[Service]
# -M 0 -> no monitoring
# -N -> Just open the connection and do nothing (not interactive)
# ServerAliveInterval, ServerAliveCountMax -> check per interval, reconnect if (fail > Max)
# ExitOnForwardFailure -> make sure forwardings have succeeded
# -L
# Forward Local
# localhost : 1111 : 140.116.x.x : 2222
# Connect to local’s port 1111 will forward to remote’s port 2222
# -R
# Forward remote
# localhost : 1111 : localhost : 2222
# Remote connect to its localhost’s port 1111 will forward to local’s 2222
# -i [ssh key]
# -p [PORT] [USER]@[HOST]
User=tunnel
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh -M 0 -N \
-o "ServerAliveInterval 60" \
-o "ServerAliveCountMax 3" \
-o "ExitOnForwardFailure=yes" \
-L localhost:[PORT]:localhost:[PORT] \
-i /home/tunnel/.ssh/id_rsa \
-p [PORT] [USER]@[HOST]
ExecStop=/usr/bin/killall autossh
Restart=always
RestartSec=30
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment