Skip to content

Instantly share code, notes, and snippets.

@airone01
Created January 1, 2023 23:57
Show Gist options
  • Save airone01/eb1f669579f309be48a530e61f8c3290 to your computer and use it in GitHub Desktop.
Save airone01/eb1f669579f309be48a530e61f8c3290 to your computer and use it in GitHub Desktop.
[Unit]
Description=AutoSSH service
After=network.target
[Service]
Type=simple
User=user
Group=group
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh -N -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -L %i:localhost:%i user@remote
[Install]
WantedBy=multi-user.target
@airone01
Copy link
Author

airone01 commented Jan 2, 2023

A systemd service script for autossh port tunelling.

Run with systemctl start autossh@yourport

Don't forget to edit the file (user, group, remote host and remote user). This script assumes that you use public/private ssh key pairs, so that you don't have to handle passwords and this can be used automatically. Read about ssh key pairs here

Field-tested by me, but don't take my word for it and double-check the script and your configuration.

More info

Notes

  • This script uses the same port in and out. Change this to your needs.
  • Read about securing your ssh environment here
  • You can force ssh to use IPV4 by adding -- -4 at the end of the ExecStart command

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