Persistent network tunnel with autossh

Purpose an explanation:

This guide will help setup a reverse tunnel from one host behind a firewall, where you can not open a port to. With a little help from autossh connection will be persistant, even if connection breaks, autossh will make sure tunnel is reconnected.You will be able to connect to the normally unreachable server, with the usage of the remotehost jump-server.

In this example, you can connect to a remotehost, that has a open or connectable ssh server on port 22. After logging in to the remote host, you can ssh from that to using the localhost port 25000 which will route the port to the system behind the firewall on port 22 ssh-server.

To pull of this trick, I have set up the following:

Install autossh and ssh-server
create ssh-key,
copy ssh-key.pub,
configure config file.
configure crontab

 

Install autossh and server-ssh-server

On Ubuntu, log in as root, and enter the command:

apt install ssh autossh
systemctl enable sshd
systemctl start sshd

On FreeBSD log in as root, and enter the command:

pkg install autossh

sysrc sshd_enable="YES"

service sshd start

Create SSH-key

ssh-keygen -t ed25519

Copy ssh-key.pub to remotehost

ssh-copy-id -i .ssh/id_ed25519.pub remotehost
Configure config file
nano /root/.ssh/config

Host remote-tunnel-home
HostName remotehost
User remotehostuser
Port 22
IdentityFile ~/.ssh/id_ed25519
RemoteForward 25000 localhost:22
ServerAliveInterval 30
ServerAliveCountMax 3
Configure Crontab
crontab -e
@reboot /usr/local/bin/autossh -M 0 -N remote-tunnel-home &