Skip to content

Instantly share code, notes, and snippets.

@luisehk
Last active March 5, 2023 20:08
Show Gist options
  • Save luisehk/a2f1349a001b2cbc919e9b3b059c6917 to your computer and use it in GitHub Desktop.
Save luisehk/a2f1349a001b2cbc919e9b3b059c6917 to your computer and use it in GitHub Desktop.
Reverse SSH tunnel as a systemd service
#!/bin/bash
echo 'Starting reverse SSH tunnel'
/usr/bin/autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -o "ExitOnForwardFailure=yes" -tt -R \*:2222:localhost:22 user@your.server
[Unit]
Description=Phone home using SSH
ConditionPathExists=|/usr/bin
After=network.target
[Service]
User=user
ExecStart=/home/user/bin/autossh.sh
RestartSec=3
Restart=always
[Install]
WantedBy=multi-user.target
@luisehk
Copy link
Author

luisehk commented Aug 8, 2017

Also requires you to:

  • Add "GatewayPorts yes" to /etc/ssh/sshd_config in your server
  • Add your local SSH public key to ~/.ssh/authorized_keys in your server
  • Install autossh
  • Install your systemd unit
chmod +x /home/user/bin/autossh.sh
cp phone-home.service /etc/systemd/system/phone-home.service
systemctl daemon-reload
systemctl enable phone-home.service
systemctl start phone-home.service

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