notes icon indicating copy to clipboard operation
notes copied to clipboard

SSH reverse tunnel service

Open jph00 opened this issue 5 years ago • 1 comments

This lets you ssh login to a box that's stuck behind a firewall. The basic idea is described here. The following has been testing on Ubuntu 20.04.

Server setup

Do this step on SSH server that you'll tunnel through:

cat > /etc/ssh/sshd_config << EOF
GatewayPorts yes
ClientAliveInterval 30
EOF

sudo systemctl reload sshd
sudo ufw allow 10022

Do all following steps on box you're tunneling to:

Setup

sudo apt install autossh

Test

HOST=host.name.here
USER=`whoami`
autossh -M 0 -N $HOST -R 10022:localhost:22

Try logging into host on port 10022 from your PC (ssh -p 10022 your.server), and you should get to tunneled machine. If it's working, hit Ctrl-C to stop tunnel.

Create service file

cat > autossh.service << EOF
[Unit]
Description=Autossh
Wants=network-online.target
After=network-online.target
StartLimitIntervalSec=0

[Service]
ExecStart=/usr/bin/autossh -M 0 -N -o "ServerAliveInterval 15" -o "ServerAliveCountMax 3" -o "ConnectTimeout 10" -o "ExitOnForwardFailure yes" $HOST -R 10022:localhost:22
Restart=always
RestartSec=10
User=$USER

[Install]
WantedBy=multi-user.target
EOF

sudo chown root:root autossh.service
sudo mv autossh.service /etc/systemd/system/
sudo systemctl start autossh

Test logging in to port 10022 again. If it's working, enable on boot:

sudo systemctl enable autossh

jph00 avatar Sep 22 '20 12:09 jph00

I stole some ideas from abhishek thakur and created a package for this purpose using ngrok.
https://remoteconnect.netlify.app/

I am planning to combine some of the ideas from https://github.com/pete88b/nbdev_colab_helper to help around git issues on colab. [currently using both of them together].

I have limited knowledge on sshd and tunneling but integrating this script in a wrapper like above could be very useful[ since ngrok is paid it only allows a single tunnel]

Rahuketu86 avatar Oct 23 '20 18:10 Rahuketu86