jph00 / notes

Stuff I find (see issues)
Creative Commons Attribution Share Alike 4.0 International
38 stars 1 forks source link

SSH reverse tunnel service #4

Open jph00 opened 3 years ago

jph00 commented 3 years ago

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
Rahuketu86 commented 3 years ago

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]